<?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();




}




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









{









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




	{




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




		




		$query = "




		INSERT INTO employees (first_name,join_date,leave_date,nic_no,status)




        VALUES (:first_name, :join_date, :leave_date, :nic_no, :status)




    ";	




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




		$statement->execute(




			array(




          




          




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




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




          ':leave_date'       =>  $_POST["leave_date"],




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




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




          




          )




		);




		




	}




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




	{




		$query = "




		SELECT * FROM employees WHERE emp_id = :emp_id";




		




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




		$statement->execute(




			array(




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




			)




		);




		$result = $statement->fetchAll();




		foreach($result as $row)




		{




			




			




			




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




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




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




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




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




          




		}




		echo json_encode($output);




	}




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




	{




		




		




			$query = "




			UPDATE employees SET




				




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




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




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




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




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




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




			";




		




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




		$statement->execute();




		$result = $statement->fetchAll();




		if(isset($result))




		{




			echo 'Employee Details Edited';




		}




	}




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




	{




		$status = 'Inactive';




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




		{




			$status = 'Inactive';




		}




		$query = "




		UPDATE employees




		SET status = :status 




		WHERE emp_id = :emp_id




		";




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




		$statement->execute(




			array(




				':status'	=>	$status,




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




			)




		);	




		$result = $statement->fetchAll();	




		if(isset($result))




		{




			echo 'Employee Status change to ' . $status;




		}




	}




}









?>