<?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");
	
	$commisionname=checkvalidstring($_POST['commisionname']);
	
	

	//$countryname=$_POST['countryname'];
	
	$collegemid= ($_POST['collegemid']);
	
	 $campusmid= ($_POST['campusmid']);
$countrymid= ($_POST['countrymid']);
	$slabstrucher = '';
	$commissionstrucher=checkvalidstring ($_POST['commissionstrucher']);
	$termsclaimable=checkvalidstring ($_POST['termsclaimable']);
	$cmcurrency=checkvalidstring ($_POST['cmcurrency']);
	$cmgstincexc=checkvalidstring ($_POST['cmgstincexc']);
	$ctaxpercent=checkvalidstring ($_POST['ctaxpercent']);
	$commissionstruchervalue=checkvalidstring ($_POST['commissionstruchervalue']);
	$ctaxname=checkvalidstring ($_POST['ctaxname']);
	$cmremarks=checkvalidstring ($_POST['cmremarks']);
	$slabstrucher=checkvalidstring ($_POST['slabstrucher']);
	
	
	
		
		 $eve1 = "INSERT INTO res_commission_master (commisionname,collegemid, commissionstrucher,termsclaimable,cmgstincexc,ctaxpercent,commissionstruchervalue,countrymid,ctaxname,cmcurrency,cmremarks,campusmid,slabstrucher) VALUES('$commisionname','$collegemid','$commissionstrucher','$termsclaimable','$cmgstincexc','$ctaxpercent','$commissionstruchervalue','$countrymid','$ctaxname','$cmcurrency','$cmremarks','$campusmid','$slabstrucher')";
		
			
			if($conn->query($eve1) === TRUE)
	{
			$last_id = $conn->insert_id;
		/*   $eve2 = "INSERT INTO res_contact_master (contactname, contactemail,contactnumber,cmobilenumber,contactdepartmnet,cposition,cremarks,caltenateemail,caddress1,caddress2,ccountry,cstate,ccity,carea,cpostalcode,cmiddlename,csurname,cmsgappname,cmsgappid,cvideoappname,cvideoappid,ccompanyname,csubcategory,clinktype,clinkid) VALUES('$contactname','$contactemail','$contactnumber','$cmobilenumber','$contactdepartmnet','$cposition','$cremarks','$caltenateemail','$caddress1','$caddress2','$countrymid','$statemid','$citymid','$areamid','$bankpostalcode','$cmiddlename','$csurname','$cmsgappname','$cmsgappid','$cvideoappname','$cvideoappid','$companyname','$csubcategory','College Commission','$last_id')";
		  $re2 = mysqli_query($conn, $eve2); */
		  if($commissionstrucher == 'Slab')
		  {
		  $delete_checkbox_array = $_POST['slabmid'];
		$n=0;
			foreach ($delete_checkbox_array as $one_by_one_sid) 
			{
				$slabmid=$one_by_one_sid;
					
				$svalue=$_POST['svalue'][$n];
				$n = $n + 1;
			
				
				if($slabmid==0)
				{
				}
				else
				{	
					$eve2="insert into res_commissionslab_detail (commissionmid,slabmid,svalue) values($last_id,'$slabmid','$svalue')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}
		  }
		  
		  $delete_checkbox_array1 = $_POST['ctistartmonth'];
		$m=0;
			foreach ($delete_checkbox_array1 as $one_by_one_sid) 
			{
				$ctistartmonth=$one_by_one_sid;
					
				$ctistartyear=$_POST['ctistartyear'][$m];
				$ctiendmonth=$_POST['ctiendmonth'][$m];
				$ctieendyear=$_POST['ctieendyear'][$m];
				$m = $m + 1;
			
				
				
					 $eve2="insert into res_commissiontermintake_detail (commissionmid,ctistartmonth,ctistartyear,ctiendmonth,ctieendyear) values($last_id,'$ctistartmonth','$ctistartyear','$ctiendmonth','$ctieendyear')";
					$re2 = mysqli_query($conn, $eve2);
					
				
			}
			
			
	$campusmid=$_POST['campusmid'];
	if (in_array('All', $campusmid))
	{
		
		$eve3 = "select ccampusmid from res_collegecampus_master where collegemid=$collegemid";
		$re3 = mysqli_query($conn, $eve3);

    //Count total number of rows
    if(mysqli_num_rows($re3) > 0)
	{
		$ccampusmid1 = array();
        while($rt3 = mysqli_fetch_assoc($re3))
		{
			$ccampusmid=$rt3['ccampusmid'];
			array_push($ccampusmid1,$ccampusmid);
		}
	}
	
	foreach ($ccampusmid1 as $one_by_one_sid) 
			{
				$ccampusmid=$one_by_one_sid;
					$eve2="insert into res_commissioncampus_detail (commissionmid,ccampusmid) values($last_id,'$ccampusmid')";
					$re2 = mysqli_query($conn, $eve2);
					
				
			}
		
	}else{
		 $delete_checkbox_array2 = $_POST['campusmid'];
		$p=0;
			foreach ($delete_checkbox_array2 as $one_by_one_sid) 
			{
				$ccampusmid=$one_by_one_sid;
					$eve2="insert into res_commissioncampus_detail (commissionmid,ccampusmid) values($last_id,'$ccampusmid')";
					$re2 = mysqli_query($conn, $eve2);
					
				
			}
	}
		
		$re2 = mysqli_query($conn, $eve2);
	
	$_SESSION['msg'] = "Record saved successfully";
	}else{
		$_SESSION['msg1'] = "Error";
	}
			
	$_SESSION['msg'] = "Record saved successfully";
	header('Location: commission.php');
			
	
?>