<?php
 	session_start(); 

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



$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	
	include("connect.php");
	include("connectp.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'];
	
	$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 = "INSERT INTO res_meeting_master (meetingplacetype,meetinglink,meetingagenda,meetingminutes,meetingdate,maddedon,maddedby,mupdatedon,mupdatedby) VALUES('$meetingplacetype','$meetinglink','$meetingagenda','$meetingminutes','$meetingdate','$mdate1','$usermid','$mdate1','$usermid')";
		
		if ($conn->query($eve1) === TRUE)
	{
		$_SESSION['msg'] = "Record saved successfully";
		$last_id = $conn->insert_id;
		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 ('$last_id','$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 ('$last_id','$one_by_one_sid','Meeting')";
			$re4 = mysqli_query($conn, $sql1);
			}
	}else{
		$_SESSION['msg1'] = "Error";
	}
		
		
	header('Location: meeting.php');
	
	
			
	
			
	
?>