<?php









//user_action.php









include('db_con.php');




if (isset($_SESSION['user_id'])) {




    $user_type = $_SESSION['user_type'];




    $user_id = $_SESSION['user_id'];




    $user_name = $_SESSION['user_name'];




    $branch_id = $_SESSION['branch_id'];




    $branch_name = $_SESSION['branch_name'];




    $store_id = $_SESSION['store_id'];




    $store_name = $_SESSION['store_name'];




}else{




  header('Location: login');




  die();




}




$dated = date('Y-m-d');









if(isset($_POST['btn_action']))









{









	if($_POST['btn_action'] == 'Add')




	{




		




		




		$query = "




		INSERT INTO sales_man (sname,contact,status)




        VALUES (:sname, :contact, :status)




    ";	




		$statement = $connect->prepare($query);




		$statement->execute(




			array(




          




         




           ':sname'          =>  trim($_POST["sname"]),




          ':contact'          =>  trim($_POST["contact"]),         




          ':status'       =>  trim($_POST["status"]),




          




          )




		);









		




		//$result = $statement->fetchAll();




		if(isset($result))




		{




			echo 'New Sales Man Added';




		}




	}




	if($_POST['btn_action'] == 'fetch_single')




	{




		$query = "




		SELECT * FROM sales_man WHERE sm_id = :sm_id";




		




		$statement = $connect->prepare($query);




		$statement->execute(




			array(




				':sm_id'	=>	$_POST["sm_id"]




			)




		);




		$result = $statement->fetchAll();




		foreach($result as $row)




		{




			




			




			




          $output['sname']          =  $row['sname'];




          $output['contact']          =  $row['contact'];




          




          




          $output['status']       =  $row["status"];




          




		}




		echo json_encode($output);




	}




	if($_POST['btn_action'] == 'Edit')




	{




		




		




			$query = "




			UPDATE sales_man SET




				




				sname = '".$_POST["sname"]."',




				contact = '".$_POST["contact"]."',													




				status = '".$_POST["status"]."'




				WHERE sm_id = '".$_POST["sm_id"]."'




			";




		




		$statement = $connect->prepare($query);




		$statement->execute();




		//$result = $statement->fetchAll();




		if(isset($result))




		{




			echo 'Sales Man Details Edited';




		}




	}




	if($_POST['btn_action'] == 'delete')




	{




		$status = 'Inactive';




		if($_POST['status'] == 'Active')




		{




			$status = 'Inactive';




		}




		$query = "




		UPDATE sales_man




		SET status = :status 




		WHERE sm_id = :sm_id




		";




		$statement = $connect->prepare($query);




		$statement->execute(




			array(




				':status'	=>	$status,




				':sm_id'		=>	$_POST["sm_id"]




			)




		);	




		//$result = $statement->fetchAll();	




		if(isset($result))




		{




			echo 'Sales Man Status change to ' . $status;




		}




	}




}









?>