<?php
 	session_start(); 

	if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}

$usermid = $_SESSION['umid'];
date_default_timezone_set("Asia/Kolkata");
	$mdate1= date('Y-m-d H:i:s');

$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	
	include("connect.php");
include("mylibrary.php");
	
	//$tmdocument=$_POST['tmdocument'];
	
	
	$taskname = checkvalidstring($_POST['taskname']);
	
	$taskpriority = checkvalidstring($_POST['taskpriority']);
	$tasknote = checkvalidstring($_POST['tasknote']);
	$taskurl = checkvalidstring($_POST['taskurl']);
	
	
	$departmentmid = $_POST['departmentmid'];
	$taskmid = $_POST['taskmid'];
	
	$branchmid	 = $_POST['branchmid'];
	 $taskduedate = date("Y-m-d H:i:s",strtotime($_POST['taskduedate']));
	
	$umid = $_POST['umid'];
	$numid = $_POST['numid'];
	
	
	
		 $eve1 = "update  res_task_master set taskname='$taskname',taskpriority='$taskpriority',tasknote='$tasknote',taskduedate='$taskduedate',tupdatedon='$mdate1',tupdatedby=$usermid,taskurl=$taskurl where taskmid='$taskmid'";
		if ($conn->query($eve1) === TRUE)
	{
		$_SESSION['msg'] = "Record saved successfully";
		$query="delete from res_taskallocation_detail where taskmid=$taskmid";
	$result= mysqli_query($conn, $query) or die("Error");
	
	$query1="delete from res_notifyto_detail where referenceid=$taskmid and referencetype='Task'";
	$result1= mysqli_query($conn, $query1) or die("Error");
	
						if(!in_array('All',$umid))
{
	$umid = $_POST['umid'];
}else{
		$branchmid = $_POST['branchmid'];

//$branchmid = array('1','2');
$placeholders = implode(',', $branchmid);
$aa1 = '';
if(!in_array('All',$branchmid))
{
	$aa1 = "and branchmid in ($placeholders)";
}

//$departmentmid = 'All';
if(isset($_POST['departmentmid']))
{
$departmentmid = $_POST['departmentmid'];


 //$departmentmid = array("1", "2");
 $placeholders1 = implode(',', $departmentmid);
$aa2 = '';
if(!in_array('All',$departmentmid))
{
	 $aa2 = "and departmentmid in ($placeholders1)";
}
}else{
	$rolemid = $_POST['rolemid'];


 //$departmentmid = array("1", "2");
 $placeholders1 = implode(',', $rolemid);
$aa2 = '';
if(!in_array('All',$rolemid))
{
	 $aa2 = "and umid in (select umid from res_userrole_detail where rolemid in ($placeholders1) )";
}
}

 $sql23="select umid  from res_user_master where  ustatus = 'Approved' $aa1 $aa2";
			$stmt23= $db7->prepare($sql23);
			
		    $stmt23->execute();
			if ($stmt23->rowCount() > 0)
				{
					$umid = array();
					while($rt23 = $stmt23->fetch(PDO::FETCH_ASSOC))
					{
						  $umid1=$rt23['umid'];
						
						
						array_push($umid,$umid1);
						
						
					}
				}


}
		
			foreach ($umid as $one_by_one_sid) 
			{
			
			
			$sql1="Insert into res_taskallocation_detail (taskmid,umid,taskstatus,taaddedon,taaddedby,taupdatedon,taupdatedby) values ('$taskmid','$one_by_one_sid','Pending','$mdate1','$usermid','$mdate1','$usermid')";
			$re4 = mysqli_query($conn, $sql1);
			}
			
					if(!in_array('All',$numid))
{
	$numid = $_POST['numid'];
}else{
	 $sql23="select umid  from res_user_master where  ustatus = 'Approved' $aa1 $aa2";
			$stmt23= $db7->prepare($sql23);
			
		    $stmt23->execute();
			if ($stmt23->rowCount() > 0)
				{
					$numid = array();
					while($rt23 = $stmt23->fetch(PDO::FETCH_ASSOC))
					{
						  $umid1=$rt23['umid'];
						
						
						array_push($numid,$umid1);
						
						
					}
				}
				
}
		
			foreach ($numid as $one_by_one_sid) 
			{
			
			
			$sql1="Insert into res_notifyto_detail (referenceid,umid,referencetype) values ('$taskmid','$one_by_one_sid','Task')";
			$re4 = mysqli_query($conn, $sql1);
			}
	
	}else{
		$_SESSION['msg1'] = "Error";
	}
		
		
	header('Location: task.php');
	
	
			
	
			
	
?>