<?php
$servername = "103.117.212.232";
$username = "ijoiners_liveuser";
$password = "N01UlYZmw*L@";
$dbname = "ijoiners_live";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// check total candidates if it runs second time
$totalRun=1;
if(isset($_GET['run']))
	$totalRun=$_GET['run'];
	if($totalRun>1){
		$sql="select count(id) as totalCount from candidate_users where status=1";
		$totalCanResult=mysqli_query($conn, $sql) or die('Sql error '. mysqli_error($conn));
		$row=mysqli_fetch_array($totalCanResult);
		if($row['totalCount']<30){
			echo '<br>count is less';
			exit;
		}
	}
}
$companyId =0;
$companyName='';
if($totalRun>1){
	$sql="select cac.lastCompanySend, cc.companyName from candidate_campaign cac left join comapny_campaign cc on cac.lastCompanySend=cc.comapnyId where dateUpdated ='".date('Y-m-d')."'";
	$companyResult=mysqli_query($conn, $sql) or die('Sql error '. mysqli_error($conn));
	if(mysqli_num_rows>0){
		
		$row=mysqli_fetch_array($companyResult);
		$comapnyId=$row['lastCompanySend'];
		$companyName=$row['companyName'];
	}
	else{
		$sql="select companyId, companyName from company_campaign where industry in ('IT Software, Hardware, Network','Technology','Animation, Gaming')  and  companyId not in (select lastCompanySend from candidate_campaign where dateUpdated between '".date("Y-m-d", strtotime("sunday -1 week"))."' and '".date("Y-m-d", strtotime("saturday 0 week"))."') order by rand() limit 0,1";
	$companyResult=mysqli_query($conn, $sql) or die('Sql error '. mysqli_error($conn));
	$row=mysqli_fetch_array($companyResult);
		$comapnyId=$row['companyId'];
		$companyName=$row['companyName'];
		
	}
}
else{
	$sql="delete from daily_mail_counters";
	$delResult=mysqli_query($conn, $sql) or die('Sql error '. mysqli_error($conn));
	$sql="select companyId, companyName from company_campaign where industry in ('IT Software, Hardware, Network','Technology','Animation, Gaming')  and  companyId not in (select lastCompanySend from candidate_campaign where dateUpdated between '".date("Y-m-d", strtotime("sunday -1 week"))."' and '".date("Y-m-d", strtotime("saturday 0 week"))."') order by rand() limit 0,1";
	$companyResult=mysqli_query($conn, $sql) or die('Sql error '. mysqli_error($conn));
	$row=mysqli_fetch_array($companyResult);
		$comapnyId=$row['companyId'];
		$companyName=$row['companyName'];
}
$sql="select id, userEmail, firstName, lastName from candidate_users where status=1 and id not in (select candidateId from daily_mail_counters) order by rand() limit 0,30";
$candidateResult=mysqli_query($conn, $sql) or die('Sql error '. mysqli_error($conn));
	while($row=mysqli_fetch_array($candidateResult)){
		$sql="insert into shorlist_history set candidateId='".$row['id']."', companyId='".$companyId."', companyName='".$companyName."', action='Viewed'";
		$canViewedResult=mysqli_query($conn, $sql) or die('Sql error '. mysqli_error($conn));
		
		$to = $row['userEmail'];
		$subject = "ImmediateJoiners.com > A company as just viewed your profile";

		$message = "
		Hi ".$row['firstName'].' '.$row['lastName'].",
		<br>
		Your Profile has been just viewed by the company ".$companyName;

		// Always set content-type when sending HTML email
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

		// More headers
		$headers .= 'From: <customer.service@immediatejoiners.com>' . "\r\n";
		//$headers .= 'Cc: myboss@example.com' . "\r\n";

		mail($to,$subject,$message,$headers);
	
	}
	echo '<br>finished module';
?>