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




		$branch_name = $_POST['branch_name'];




		$city = $_POST['city'];




		$query = "




		INSERT INTO branch (dated,branch_name,city,country,status)




        VALUES ('".$dated."','".$branch_name."','".$city."','Pakistan','Active')




    ";	




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




		$statement->execute();




			




		$result = $statement->fetchAll();




		if(isset($result))




		{




			echo 'New Branch Added';




		}




	}




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




	{




		$query = "




		SELECT * FROM branch WHERE branch_id = :branch_id";




		




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




		$statement->execute(




			array(




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




			)




		);




		$result = $statement->fetchAll();




		foreach($result as $row)




		{




			




			




			//$output['dated']          =  $row['dated'];




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




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




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




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




          




		}




		echo json_encode($output);




	}




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




	{




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




		




			$query = "




			UPDATE branch SET




				dated = '".$dated."',




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




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




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




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




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




			";




		




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




		$statement->execute();




		$result = $statement->fetchAll();




		if(isset($result))




		{




			echo 'Branch Details Edited';




		}




	}




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




	{




		$status = 'Inactive';




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




		{




			$status = 'Inactive';




		}




		$query = "




		UPDATE branch




		SET status = :status 




		WHERE branch_id = :branch_id




		";




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




		$statement->execute(




			array(




				':status'	=>	$status,




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




			)




		);	




		$result = $statement->fetchAll();	




		if(isset($result))




		{




			echo 'Branch Status change to ' . $status;




		}




	}




}









?>