<?php
 session_start(); 

	if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}
	
	include("connect.php");
	include("connectp.php");
	include("mylibrary.php");
	date_default_timezone_set("Asia/Kolkata");
	$mdate= date('Y-m-d H:i:s');
	$mdate2= date('d-m-Y H:i');
	
	
	
	
	
	$umid=$_SESSION['umid'];
	$taskstatus=checkvalidstring($_POST['taskstatus']);
	$taremark=checkvalidstring($_POST['taremark']);
	$tadid =$_POST['tadid'];
	
	
	 $eve2 = "update res_taskallocation_detail set  taupdatedon='$mdate',taskstatus='$taskstatus',taupdatedby='$umid',taremark='$taremark' where tadid=$tadid ";
	
	
	if ($conn->query($eve2) === TRUE)
	{
		
		
		
		
		$eve23 = "select  ufullname,uemail from res_user_master where umid =$umid  ";
				 $re23 = mysqli_query($conn, $eve23);
		if(mysqli_num_rows($re23) > 0)
		{
			while($rt23 = mysqli_fetch_assoc($re23))
			{
				$ufullname1=$rt23['ufullname'];
				$uemail1=$rt23['uemail'];
				
				
			}
		}
		
		$eve23 = "select  taskname,taskmid,taskduedate from res_task_master where  taskmid in (select taskmid from res_taskallocation_detail where tadid=$tadid) ";
				 $re23 = mysqli_query($conn, $eve23);
		if(mysqli_num_rows($re23) > 0)
		{
			while($rt23 = mysqli_fetch_assoc($re23))
			{
				$taskname=$rt23['taskname'];
				$taskmid=$rt23['taskmid'];
				$taskduedate1 = date("d-m-Y H:i:s",strtotime($rt23['taskduedate']));
	
				
			}
		}
		
		
		
		 $sql23="select umid  from res_user_master where  umid in (select umid from res_notifyto_detail where referenceid = $taskmid and referencetype='Task')";
			$stmt23= $db7->prepare($sql23);
			
		    $stmt23->execute();
			if ($stmt23->rowCount() > 0)
				{
					$umid2 = array();
					while($rt23 = $stmt23->fetch(PDO::FETCH_ASSOC))
					{
						  $umid1=$rt23['umid'];
						
						
						array_push($umid2,$umid1);
						
						
					}
				}
		
		array_push($umid2,$umid);
				
				foreach ($umid2 as $one_by_one_sid) 
			{
				
				$eveag = "select ufullname,uemail from res_user_master where umid=$one_by_one_sid";
				$reag = mysqli_query($conn, $eveag);
				if(mysqli_num_rows($reag) > 0)
				{
					while($rtag = mysqli_fetch_assoc($reag))
					{
						$ufullname=$rtag['ufullname'];
						$uemail=$rtag['uemail'];
						
					}
				}
				else
				{
					$ufullname="";
					$uemail="";
					
				}
				
		$eve3 = "select * from res_emailacc_master LIMIT 0,1";
				$re3 = mysqli_query($conn, $eve3);
				
				if(mysqli_num_rows($re3) > 0)
				{
					while($rt3 = mysqli_fetch_assoc($re3))
					{
						$providername=$rt3['providername'];
						$email=$rt3['email'];
						$epassword=$rt3['epassword'];
						$esmtp=$rt3['esmtp'];
						$eport=$rt3['eport'];
						$essltls=$rt3['essltls'];
						
					}
				}
				
				$uu = "$email";
				$pp = "$epassword";

				ob_start();
				require_once('class.phpmailer.php');
				
			
		
	
	
	
				
				$message="Dear $ufullname <br><br>$taskname status has been changed by $ufullname1 <br>Due Status: $taskstatus <br><br>Due Datetime: $taskduedate1 <br><br>Regards,<br> $providername";

$subject = "Task Completed".$mdate2;



				$mail = new PHPMailer();
				$mail->IsSMTP();
				$mail->CharSet="UTF-8";
				$mail->SMTPSecure = "$essltls";
				$mail->Host = "$esmtp";
				$mail->Port = "$eport";
				$mail->Username = $uu;
				$mail->Password = $pp;
				$mail->SMTPAuth = true;
				$mail->SMTPDebug = 1;
				$mail->From = $uu;
				$mail->FromName = "$providername";
			if($uemail=='')
					{					
					$uemail='at.zeelsheth@gmail.com';
					}
				if($uemail!='')
					{
					$mail->AddAddress("$uemail");
				
					}
					
				
					
				
				
				//$mail->addBCC("$alertemailid");
				$mail->IsHTML(true);
				$mail->Subject    = $subject;
				$mail->AltBody    = ".";
				$mail->Body    = $message;


				if(!$mail->Send())
				{
					$err=$mail->ErrorInfo;
					echo $err;
					//return;
				}
				else
				{
					//header("location:contact-us.php");
				}
	}
	}
	else
	{
		$_SESSION['msg1'] = "Error";
	}
	
	
		header("Location: mytask.php");
	
?>	