<?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')
	{
		
		$query="select IFNULL(MAX(type_id),10) from item_type";
		$statement = $connect->prepare($query);
		$statement->execute();
		$type_id = $statement->fetchColumn();
		if($type_id < 9){$type_id=10;}
		$type_id++;
		
		$query = "
		INSERT INTO item_type(type_id,type_name,user_id,status)
        VALUES ('".$type_id."','".$_POST["type_name"]."','".$user_id."','".$_POST["status"]."')
    ";	
		$statement = $connect->prepare($query);
		$statement->execute();
			
		$result = $statement->fetchAll();
		if(isset($result))
		{
			echo 'New Type Added';
		}
	}
	if($_POST['btn_action'] == 'fetch_single')
	{
		$query = "
		SELECT * FROM item_type WHERE type_id = :type_id";
		
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':type_id'	=>	$_POST["type_id"]
			)
		);
		$result = $statement->fetchAll();
		foreach($result as $row)
		{
			
			
			//$output['dated']          =  $row['dated'];
          $output['type_name']          =  $row['type_name'];
        
          
          $output['status']       =  $row["status"];
          
		}
		echo json_encode($output);
	}
	if($_POST['btn_action'] == 'Edit')
	{
		
		
			$query = "
			UPDATE item_type SET
				
				type_name = '".$_POST["type_name"]."',
				
				user_id = '".$user_id."',													
				status = '".$_POST["status"]."'
				WHERE type_id = '".$_POST["type_id"]."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();
		$result = $statement->fetchAll();
		if(isset($result))
		{
			echo 'Item Type Edited';
		}
	}
	if($_POST['btn_action'] == 'delete')
	{
		$status = 'Inactive';
		if($_POST['status'] == 'Active')
		{
			$status = 'Inactive';
		}
		$query = "
		UPDATE item_type
		SET status = :status 
		WHERE type_id = :type_id
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':status'	=>	$status,
				':type_id'		=>	$_POST["type_id"]
			)
		);	
		$result = $statement->fetchAll();	
		if(isset($result))
		{
			echo 'Type Status change to ' . $status;
		}
	}
}
?>