<?php
ob_start();
session_start();
include("include.php");
$res='';


if(!empty($_POST['st']) && $_POST['st']=='dis')
{
$sel=executework("select * from haindavasakthi_district where sid='".$_POST['state']."'");
//echo "select * from haindavasakthi_district where sid='".$_POST['st_id']."'order by mandal";
$cnt=@mysqli_num_rows($sel);
while($row=@mysqli_fetch_array($sel))
{
	$res.="~".$row['id']."=".$row['dt_name'];
}
}

if(!empty($_POST['st']) && $_POST['st']=='mandal')
{
	$sel=executework("select * from haindavasakthi_mandal where dtid='".$_POST['dt']."'");
	$cnt=@mysqli_num_rows($sel);
	while($row=@mysqli_fetch_array($sel))
	{
		$res.="~".$row['id']."=".$row['mandal_name'];
	}
}

if(!empty($_POST['st']) && $_POST['st']=='village')
{
	$sel=executework("select * from haindavasakthi_village where mandal_id='".$_POST['mi']."'");
	echo "select * from haindavasakthi_village where mandal_id='".$_POST['mi']."'";
	$cnt=@mysqli_num_rows($sel);
	while($row=@mysqli_fetch_array($sel))
	{
		$res.="~".$row['id']."=".$row['village_name'];
	}
}

if(!empty($_POST['st']) && $_POST['st']=='dharma_sub_cat')
{
	$sel=executework("select * from haindavasakthi_dharma_cat where top_id='".$_POST['sub_id']."'");
	$cnt=@mysqli_num_rows($sel);
	while($row=@mysqli_fetch_array($sel))
	{
		$res.="~".$row['id']."=".$row['category'];
	}
}

if(!empty($_POST['st']) && $_POST['st']=='activity_sub_cat')
{
	$sel=executework("select * from haindavasakthi_catg where top_id='".$_POST['cat_id']."'");
	$cnt=@mysqli_num_rows($sel);
	while($row=@mysqli_fetch_array($sel))
	{
		$res.="~".$row['id']."=".$row['catg_name'];
	}
} 

if(!empty($_POST['st']) && $_POST['st']=='del_img')
{
	echo "update haindavasakthi_activity set ".$_POST['dfield']."='' where id='".$_POST['del_id']."'";
	$upd=executework("update haindavasakthi_activity set ".$_POST['dfield']."='' where id='".$_POST['del_id']."'");
}

//----------- Major Details 

if(!empty($_POST['st']) && $_POST['st']=='major_details')
{
	$sel=executework("select * from lms_major_details where degree_id='".$_POST['degree_id']."' and status=1");
	$cnt=@mysqli_num_rows($sel);
	while($row=@mysqli_fetch_array($sel))
	{
		$res.="~".$row['id']."=".$row['major_name'];
	}
} 

if(!empty($_POST['st']) && $_POST['st']=='cart_status')
{
	
	$sel=executework("select * from lms_cart_course where user_id='".$_SESSION['login_id']."' and user_type='Student' and status=0");
	$cnt=@mysqli_num_rows($sel);
	
	if($cnt==0)
		{
			$_SESSION['cart_id']=time();
			$int=executeworkins("insert into lms_cart_course (cart_id,user_id,user_type,created_by,created_on) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','Student','".$_SESSION['login_id']."','".date('Y-m-d')."')");
		}
		else
		{
			$row=@mysqli_fetch_array($sel);
			$_SESSION['cart_id']=$row['cart_id'];
		}
	
	$selcart=executework("select * from lms_cart_list where cart_id='".$_SESSION['cart_id']."' and child_id='".$_POST['child_id']."' and purchage_course_id='".$_POST['course_id']."'");
	$cntcart=@mysqli_num_rows($selcart);
	$rowcart=@mysqli_fetch_array($selcart);
	
	if($cntcart==0 && $_POST['action']=='add')
	{
			$cart_courses=executework("select * from lms_instructor_courses where id='".$_POST['course_id']."'");
			$cntcart_courses=@mysqli_num_rows($cart_courses);
			$rowcart_courses=@mysqli_fetch_array($cart_courses);
	
			$cart=executework("insert into lms_cart_list (cart_id,parent_id,child_id,purchage_course_id,amount,p_date,created_by,created_on,status) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','".$_POST['child_id']."','".$_POST['course_id']."','".$rowcart_courses['course_price']."','".date('Y-m-d')."','".$_SESSION['login_id']."','".date('Y-m-d h:i:s')."','1')");
					
	}
	
	if($cntcart>0 && $_POST['action']=='remove')
	{
		$sel=executework("DELETE FROM lms_cart_list WHERE id='".$rowcart['id']."'");
		$res.="add";
	}
	
		$cart_items=executework("select COUNT(*) AS cnt,SUM(amount) AS total_amount from lms_cart_list where cart_id='".$_SESSION['cart_id']."'");
		$cntcart_items=@mysqli_num_rows($cart_items);
		$rowcart_items=@mysqli_fetch_array($cart_items);
		
		$update=executework("update lms_cart_course set cart_item='".$rowcart_items['cnt']."',amount='".$rowcart_items['total_amount']."' where cart_id='".$_SESSION['cart_id']."' ");
}


if(!empty($_POST['st']) && $_POST['st'] == 'add_to_cart')
{	
	$output=1;
	$sel=executework("select * from lms_cart_course where user_id='".$_SESSION['login_id']."' and user_type='Student' and status=0");
	$cnt=@mysqli_num_rows($sel);
	if($cnt==0)
		{
			$_SESSION['cart_id']=time();
			$int=executeworkins("insert into lms_cart_course (cart_id,user_id,user_type,created_by,created_on,status) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','Student','".$_SESSION['login_id']."','".date('Y-m-d')."',0)");
		}
		else
		{
			$row=@mysqli_fetch_array($sel);
			$_SESSION['cart_id']=$row['cart_id'];
		}

		$selcart=executework("select * from lms_cart_list where cart_id='".$_SESSION['cart_id']."' and child_id='".$_POST['child_id']."' and purchage_course_id='".$_POST['course_id']."'");
		$cntcart=@mysqli_num_rows($selcart);
		$rowcart=@mysqli_fetch_array($selcart);
			
		if($cntcart==0 && $_POST['action']=='add')
		{
				$cart_courses=executework("select * from lms_instructor_courses where id='".$_POST['course_id']."'");
				$cntcart_courses=@mysqli_num_rows($cart_courses);
				$rowcart_courses=@mysqli_fetch_array($cart_courses);
						
				$cart=executework("insert into lms_cart_list (cart_id,parent_id,child_id,purchage_course_id,amount,p_date,created_by,created_on,status) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','".$_POST['child_id']."','".$_POST['course_id']."','".$rowcart_courses['course_price']."','".date('Y-m-d')."','".$_SESSION['login_id']."','".date('Y-m-d h:i:s')."','1')");
				$res .= "~1=success";
						
		}	
		
		if($cntcart>0 && $_POST['action']=='remove')
		{
			$sel=executework("DELETE FROM lms_cart_list WHERE id='".$rowcart['id']."'");
			$res .= "~0=success";
		}
		
		$cart_items=executework("select COUNT(*) AS cnt,SUM(amount) AS total_amount from lms_cart_list where cart_id='".$_SESSION['cart_id']."'");
		$cntcart_items=@mysqli_num_rows($cart_items);
		$rowcart_items=@mysqli_fetch_array($cart_items);
		if(empty($rowcart_items['cnt']))
		{
		    $cnt=0;
		    $amt=0;
		}
		else
		{
		    $cnt=$rowcart_items['cnt'];
		    $amt=$rowcart_items['total_amount'];
		}
		
		$update=executeworkins("update lms_cart_course set cart_item='".$cnt."',amount='".$amt."' where cart_id='".$_SESSION['cart_id']."' ");
}
	
	
	
	
	
	
	
	
	
	/*$id=1;
	$res .= "~".$id."=success";

	echo $res;
	exit;*/
	
	/*$id = $_POST['product_id'];

	if(!isset($_SESSION['cart'])){
		$_SESSION['cart'] = array();
	}

	$_SESSION['cart'][] = $id;

	$count = count($_SESSION['cart']);

	$res = "";
	$res .= "~".$id."=success";
	$res .= "~count=".$count;

	echo $res;
	exit;
*/

	/*$sel=executework("select * from lms_cart_course where user_id='".$_SESSION['login_id']."' and user_type='Student' and order_status NOT IN('completed','cancelled')");
	$cnt=@mysqli_num_rows($sel);
	
	if($cnt==0)
		{
			$_SESSION['cart_id']=time();
			$int=executeworkins("insert into lms_cart_course (cart_id,user_id,user_type,created_by,created_on) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','Student','".$_SESSION['login_id']."','".date('Y-m-d')."')");
		}
		else
		{
			$row=@mysqli_fetch_array($sel);
			$_SESSION['cart_id']=$row['cart_id'];
		}
	
	$selcart=executework("select * from lms_cart_list where cart_id='".$_SESSION['cart_id']."' and child_id='".$_POST['child_id']."' and purchage_course_id='".$_POST['course_id']."'");
	$cntcart=@mysqli_num_rows($selcart);
	$rowcart=@mysqli_fetch_array($selcart);
	
	if($cntcart==0 && $_POST['action']=='add')
	{
			$cart_courses=executework("select * from lms_instructor_courses where id='".$_POST['course_id']."'");
			$cntcart_courses=@mysqli_num_rows($cart_courses);
			$rowcart_courses=@mysqli_fetch_array($cart_courses);
	
			$cart=executework("insert into lms_cart_list (cart_id,parent_id,child_id,purchage_course_id,amount,p_date,created_by,created_on,status) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','".$_POST['child_id']."','".$_POST['course_id']."','".$rowcart_courses['course_price']."','".date('Y-m-d')."','".$_SESSION['login_id']."','".date('Y-m-d h:i:s')."','1')");
					
	}*/
	
	
	
	/*if($cntcart>0 && $_POST['action']=='remove')
	{
		$sel=executework("DELETE FROM lms_cart_list WHERE id='".$rowcart['id']."'");
		$res.="add";
	}
	
		$cart_items=executework("select COUNT(*) AS cnt,SUM(amount) AS total_amount from lms_cart_list where cart_id='".$_SESSION['cart_id']."'");
		$cntcart_items=@mysqli_num_rows($cart_items);
		$rowcart_items=@mysqli_fetch_array($cart_items);
		
		$update=executework("update lms_cart_course set cart_item='".$rowcart_items['cnt']."',amount='".$rowcart_items['total_amount']."' where cart_id='".$_SESSION['cart_id']."' ");*/



	//$res .= "~".$output."=success";


//}

if(!empty($_POST['st']) && $_POST['st']=='get_card_count')
{
	$sel=executework("select *,(lms_cart_list.id) AS main_id from lms_cart_list,lms_instructor_courses,lms_cart_course where lms_instructor_courses.id=lms_cart_list.purchage_course_id and lms_cart_course.cart_id=lms_cart_list.cart_id and lms_cart_course.user_type='Student' and lms_cart_list.parent_id='".$_POST['log_id']."' and lms_cart_course.status=0");
	
		$cnt=@mysqli_num_rows($sel);
		$res.="~".$cnt;
}


echo $res;
?>