<?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");
	
	//$offername=$_POST['offername'];
	$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']);
	
	
	
 
	
		
	
		 $eve1 = "INSERT INTO res_offer_master (offername,offerdesc,offervalidtill,offerstartdate,offerdiscount,offertrems,offercouponcode) VALUES('$offername','$offerdesc','$offervalidtill','$offerstartdate','$offerdiscount','$offertrems','$offercouponcode')";
		
		
		if($conn->query($eve1) === TRUE)
	{
			$last_id = $conn->insert_id;
			$delete_checkbox_array = $_POST['subservicemid'];
		$n=0;
			foreach ($delete_checkbox_array as $one_by_one_sid1) 
			{
				$subservicemid=$one_by_one_sid1;
				if($subservicemid==0)
				{
				}
				else
				{	
					$eve2="insert into res_offersubservice_detail (offermid,subservicemid) values($last_id,'$subservicemid')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}
			
			$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($last_id,'$countrymid')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}
			
			$delete_checkbox_array2 = $_POST['branchmid'];
		$n=0;
			foreach ($delete_checkbox_array2 as $one_by_one_sid2) 
			{
				$branchmid=$one_by_one_sid2;
				if($branchmid==0)
				{
				}
				else
				{	
					$eve2="insert into res_offerbranch_detail (offermid,branchmid) values($last_id,'$branchmid')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}
			$_SESSION['msg'] = "Record saved successfully";
	}else{
		$_SESSION['msg1'] = "Error";
	}
			
	
	header('Location: offer.php');
			
	
?>