<?php
 	session_start(); 

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



$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	
	include("connect.php");
include("mylibrary.php");
	$usermid = $_SESSION['umid'];
date_default_timezone_set("Asia/Kolkata");
	$mdate1= date('Y-m-d H:i:s');
	//$tmdocument=$_POST['tmdocument'];
	
	
	$meetingplacetype = checkvalidstring($_POST['meetingplacetype']);
	$meetinglink = checkvalidstring($_POST['meetinglink']);
	$meetingagenda = checkvalidstring($_POST['meetingagenda']);
	$meetingminutes = checkvalidstring($_POST['meetingminutes']);
	$departmentmid = $_POST['departmentmid'];
	$meetingmid = $_POST['meetingmid'];
	
	$branchmid	 = $_POST['branchmid'];
	$meetingdate = date("Y-m-d",strtotime($_POST['meetingdate']));
	$placeholders = implode(',', $branchmid);
	if(!in_array('All',$branchmid))
{
	$aa1 = "and branchmid in ($placeholders)";
}

//$departmentmid = 'All';
$departmentmid = $_POST['departmentmid'];


 //$departmentmid = array("1", "2");
 $placeholders1 = implode(',', $departmentmid);
$aa2 = '';
if(!in_array('All',$departmentmid))
{
	 $aa2 = "and departmentmid in ($placeholders1)";
}
	$umid = $_POST['umid'];
	$numid = $_POST['numid'];
	
	
		 $eve1 = "update  res_meeting_master set meetingplacetype='$meetingplacetype',meetinglink='$meetinglink',meetingagenda='$meetingagenda',meetingminutes='$meetingminutes',meetingdate='$meetingdate',mupdatedon='$mdate1',mupdatedby='$usermid' where meetingmid='$meetingmid'";
		
		if ($conn->query($eve1) === TRUE)
	{
		$_SESSION['msg'] = "Record saved successfully";
		$query="delete from res_meetingallocation_detail where meetingmid=$meetingmid";
	$result= mysqli_query($conn, $query) or die("Error");
	$query1="delete from res_notifyto_detail where referenceid=$meetingmid and referencetype='Meeting'";
	$result1= mysqli_query($conn, $query1) or die("Error");
	
			if(!in_array('All',$umid))
{
	$umid = $_POST['umid'];
}
else if( in_array('All',$branchmid) && in_array('All',$departmentmid))
	{
		 // $sql23="select umid  from res_user_master where  departmentmid='$departmentmid' and branchmid=$branchmid";
		  $sql23="select umid  from res_user_master where  ustatus = 'Approved'";
			$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);
						
						
					}
				}
				
				
			
	}else if( in_array('All',$branchmid) && !in_array('All',$departmentmid)){
		
		// $sql23="select umid  from res_user_master where  departmentmid='$departmentmid' and branchmid=$branchmid";
		  $sql23="select umid  from res_user_master where  ustatus = 'Approved' and departmentmid in ('$placeholders1')";
			$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);
						
						
					}
				}
	}else if( !in_array('All',$branchmid) && !in_array('All',$departmentmid)){
		
		// $sql23="select umid  from res_user_master where  departmentmid='$departmentmid' and branchmid=$branchmid";
		  $sql23="select umid  from res_user_master where  ustatus = 'Approved' and departmentmid in ($placeholders1) and branchmid in ($placeholders)";
			$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);
						
						
					}
				}
	}else if( !in_array('All',$branchmid) && in_array('All',$departmentmid)){
		
		// $sql23="select umid  from res_user_master where  departmentmid='$departmentmid' and branchmid=$branchmid";
		  $sql23="select umid  from res_user_master where  ustatus = 'Approved' and branchmid in ($placeholders)";
			$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_meetingallocation_detail (meetingmid,umid) values ('$meetingmid','$one_by_one_sid')";
			$re4 = mysqli_query($conn, $sql1);
			}
			
			if(!in_array('All',$numid))
{
	$numid = $_POST['numid'];
}
else if( in_array('All',$branchmid) && in_array('All',$departmentmid))
	{
		 // $sql23="select umid  from res_user_master where  departmentmid='$departmentmid' and branchmid=$branchmid";
		  $sql23="select umid  from res_user_master where  ustatus = 'Approved'";
			$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);
						
						
					}
				}
				
				
			
	}else if( in_array('All',$branchmid) && !in_array('All',$departmentmid)){
		
		// $sql23="select umid  from res_user_master where  departmentmid='$departmentmid' and branchmid=$branchmid";
		  $sql23="select umid  from res_user_master where  ustatus = 'Approved' and departmentmid in ('$placeholders1')";
			$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);
						
						
					}
				}
	}else if( !in_array('All',$branchmid) && !in_array('All',$departmentmid)){
		
		// $sql23="select umid  from res_user_master where  departmentmid='$departmentmid' and branchmid=$branchmid";
		  $sql23="select umid  from res_user_master where  ustatus = 'Approved' and departmentmid in ($placeholders1) and branchmid in ($placeholders)";
			$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);
						
						
					}
				}
	}else if( !in_array('All',$branchmid) && in_array('All',$departmentmid)){
		
		// $sql23="select umid  from res_user_master where  departmentmid='$departmentmid' and branchmid=$branchmid";
		  $sql23="select umid  from res_user_master where  ustatus = 'Approved' and branchmid in ($placeholders)";
			$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 ('$meetingmid','$one_by_one_sid','Meeting')";
			$re4 = mysqli_query($conn, $sql1);
			}
	}else{
		$_SESSION['msg1'] = "Error";
	}
		
		
	header('Location: meeting.php');
	
	
			
	
			
	
?>