<?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");


	
	$offermid=$_POST['offermid'];
	$offername=checkvalidstring ($_POST['offername']);
	
	
	$subservicemid=$_POST['subservicemid'];
	$branchmid=$_POST['branchmid'];
	$offervalidtill=date("Y-m-d",strtotime($_POST['offervalidtill']));
	$offerstartdate=date("Y-m-d",strtotime($_POST['offerstartdate']));
	
	
	$offerdesc=checkvalidstring ($_POST['offerdesc']);
	
	
	$offertrems=checkvalidstring ($_POST['offertrems']);
	
	
	$offercouponcode=checkvalidstring ($_POST['offercouponcode']);
	
	
	$offerdiscount=checkvalidstring ($_POST['offerdiscount']);
	
	
	
 
	

	 $eve = "update res_offer_master set offername='$offername',offervalidtill='$offervalidtill',offerstartdate='$offerstartdate',offerdesc='$offerdesc',offertrems='$offertrems',offercouponcode='$offercouponcode',offerdiscount='$offerdiscount'  where offermid='$offermid'";

	

	if ($conn->query($eve) === TRUE)

	{		

		$_SESSION['msg'] = "offer Record Updated succesfully";
		$query="delete from res_offersubservice_detail where offermid=$offermid";
	$result= mysqli_query($conn, $query) or die("Error");
	
	
	
		$delete_checkbox_array = $_POST['subservicemid'];
		$n=0;
			foreach ($delete_checkbox_array as $one_by_one_sid) 
			{
				$subservicemid=$one_by_one_sid;
				if($subservicemid==0)
				{
				}
				else
				{	
					$eve2="insert into res_offersubservice_detail (offermid,subservicemid) values($offermid,'$subservicemid')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}
			
			
			$query1="delete from res_offercountry_detail where offermid=$offermid";
	$result1= mysqli_query($conn, $query1) or die("Error");
			$delete_checkbox_array1 = $_POST['countrymid'];
		$n=0;
			foreach ($delete_checkbox_array1 as $one_by_one_sid) 
			{
				$countrymid=$one_by_one_sid;
				if($countrymid==0)
				{
				}
				else
				{	
					$eve2="insert into res_offercountry_detail (offermid,countrymid) values($offermid,'$countrymid')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}
			$query2="delete from res_offerbranch_detail where offermid=$offermid";
	$result2= mysqli_query($conn, $query2) or die("Error");
		
			$delete_checkbox_array2 = $_POST['countrymid'];
		$n=0;
			foreach ($delete_checkbox_array2 as $one_by_one_sid) 
			{
				$branchmid=$one_by_one_sid;
				if($branchmid==0)
				{
				}
				else
				{	
					$eve2="insert into res_offerbranch_detail (offermid,branchmid) values($offermid,'$branchmid')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}

		header('Location: offer.php');		

	} 

	else 

	{

			

		$_SESSION['msg'] = "Error";

		header('Location: offer.php');

	}

?>