<?php
	include "../db_connection/db_connection.php";
	include "../config/config.php";
	
	if($_GET['ajax_id']==1)
	{
		$flg = $_GET['flg'];
		$vls = $_GET['vls'];
		$pa_id = $_GET['pa_id'];
		
		$ses_id = session_id();	
		
		$SLCT_aj = "SELECT *,count(design_det_id) AS de_ct FROM po_temp_des WHERE party_det_id = '$pa_id'"; 
		$QURY_aj = mysqli_query($connect,$SLCT_aj);
		$FTCH_aj = mysqli_fetch_object($QURY_aj);
		
		$old_se_id = ($FTCH_aj->de_ct > 0)?$FTCH_aj->ses_id:$ses_id;
		
		if($ses_id == $old_se_id)
		{
			if($flg == 1)
			{	
				
				$INSRT		= "INSERT INTO po_temp_des(`party_det_id`, `design_det_id`,`ses_id`, `post_dt`)VALUES('$pa_id','$vls','$ses_id','$cur_time')";	
				$QURY		= mysqli_query($connect,$INSRT);			
						
			}
			else if($flg == 2)
			{
				$DELT = "DELETE FROM po_temp_des WHERE party_det_id = '$pa_id' AND design_det_id = '$vls'";
				$QRY12    = mysqli_query($connect,$DELT); 
				
			}	
		}
		else
		{
			$DELT = "DELETE FROM po_temp_des WHERE party_det_id = '$pa_id'";
			$QRY12    = mysqli_query($connect,$DELT); 			
		}	
     	
 	}
	else if($_GET['ajax_id']==2)
	{
		$flg = $_GET['flg'];
		$vls = $_GET['vls'];
		//echo $vls;
		$pa_id = $_GET['pa_id'];
		
		$ses_id = session_id();	
		
		$SLCT_aj = "SELECT *,count(design_det_id) AS de_ct FROM po_temp_des WHERE party_det_id = '$pa_id'"; 
		$QURY_aj = mysqli_query($connect,$SLCT_aj);
		$FTCH_aj = mysqli_fetch_object($QURY_aj);
		
		$old_se_id = ($FTCH_aj->de_ct > 0)?$FTCH_aj->ses_id:$ses_id;
		
		if($vls!='')
		{
			$vls = ltrim($vls,",");
			$des_ids = explode(",",$vls);
				
			if($ses_id == $old_se_id)
			{
							
				if($flg == 1)
				{	
					foreach($des_ids as $cat_id)
					{
						$INSRT		= "INSERT INTO po_temp_des(`party_det_id`, `design_det_id`,`ses_id`, `post_dt`)VALUES('$pa_id','$cat_id','$ses_id','$cur_time')";	
						$QURY		= mysqli_query($connect,$INSRT);			
					}
							
				}
				else if($flg == 2)
				{
					foreach($des_ids as $cat_id)
					{
						$DELT = "DELETE FROM po_temp_des WHERE party_det_id = '$pa_id' AND design_det_id = '$cat_id'";
						$QRY12    = mysqli_query($connect,$DELT); 
					}
					
				}
			}
			else
			{
				$DELT = "DELETE FROM po_temp_des WHERE party_det_id = '$pa_id' AND ses_id='$old_se_id'";
				$QRY12    = mysqli_query($connect,$DELT); 
				
				foreach($des_ids as $cat_id)
				{
					$INSRT		= "INSERT INTO po_temp_des(`party_det_id`, `design_det_id`,`ses_id`, `post_dt`)VALUES('$pa_id','$cat_id','$ses_id','$cur_time')";	
					$QURY		= mysqli_query($connect,$INSRT);			
				}
			}
		}
		
 	}
	else if($_GET['ajax_id']==3)
	{
		$pa_id = $_GET['pa_id'];
		
		$DELT = "DELETE FROM po_temp_des WHERE party_det_id = '$pa_id'";
		$QRY12    = mysqli_query($connect,$DELT);
		echo "1";
	}
	
	