<?php
//user_action.php
include('db_con.php');
include('function.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');
	
		$image = '';
		  if($_FILES["item_image"]["name"] != '')
		  {
		   $image = upload_image();
		  }
		  $sql = "select * from items where item_name = '".$_POST['item_name']."' and type_id = '".$_POST['item_type']."'";
		  $statement = $connect->prepare($sql);
		  $statement->execute();
		  $exist_item = $statement->fetchColumn();
		  if($exist_item =='')
		  {
		
		$query = "
		INSERT INTO items (dated,item_no, item_name,description,type_id,category_id,dimension,uom,min_qty,max_qty,location,ctn,hs_code,item_cost,retail_price,list_price,item_image,user_id,status)
        VALUES (:dated, :item_no, :item_name,:description, :type_id,:category_id, :dimension,
           :uom, :min_qty, :max_qty, :location,:ctn, :hs_code, :item_cost, :retail_price, :list_price, :item_image, :user_id, :status)
    ";	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
          
          ':dated'             =>  $dated,          
          ':item_no'          =>  trim($_POST["item_no"]),
          ':item_name'          =>  trim($_POST["item_name"]),          
          ':description'          =>  trim($_POST["description"]),          
          ':type_id'       =>  trim($_POST["item_type"]),
          ':category_id'       =>  trim($_POST["category_id"]),
          ':dimension'       =>  trim($_POST["dimension"]),        
          ':uom'       =>  trim($_POST["uom"]),        
          ':min_qty'       =>  trim($_POST["min_qty"]),
          ':max_qty'       =>  trim($_POST["max_qty"]),
          ':location'       =>  trim($_POST["location"]),
          ':ctn'       =>  0,
          ':hs_code'       =>  trim($_POST["hs_code"]),
          ':item_cost'       =>  trim($_POST["item_cost"]),          
          ':retail_price'       =>  trim($_POST["retail_price"]),
          ':list_price'       =>  trim($_POST["retail_price"]),        
               
          ':item_image'  => $image,
          ':user_id'       =>  $user_id,
          ':status'       =>  'Active'
          )
		);
		$statement = $connect->query("SELECT LAST_INSERT_ID()");
      	$item_id = $statement->fetchColumn();
		$result = $statement->fetchAll();
	
		}else{echo 'Item Already Exist';}
      
		}
		if(isset($result))
		{
			echo 'New Item Added';
		}
	
	}
	if($_POST['btn_action'] == 'fetch_single')
	{
		$query = "
		SELECT * FROM items WHERE item_id = :item_id";
		
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':item_id'	=>	$_POST['item_id']
			)
		);
		$result = $statement->fetchAll();
		foreach($result as $row)
		{		
			
           $output['item_name']	=  $row['item_name'];
           $output['item_no']	=  $row['item_no'];
           
           $output['type_id']	=  $row['type_id'];
           $output['category_id']	=  $row['category_id'];
           $output['dimension'] =  $row['dimension'];           
           $output['description'] =  $row['description'];           
           
           $output['uom']	=  $row['uom'];
           
           $output['min_qty']	=  $row['min_qty'];
           $output['max_qty']	=  $row['max_qty'];
           $output['location'] =  $row['location'];
           $output['ctn'] =  $row['ctn'];
           $output['hs_code'] =  $row['hs_code'];
           $output['item_cost']	=  $row['item_cost'];          
           $output['retail_price']	=  $row['retail_price'];
           $output['list_price']  =  $row['list_price'];
          
           $output['status']	=  $row['status'];
           if($row["item_image"] != '')
			  {
			   $output['item_image'] = '<img src="upload/'.$row["item_image"].'" class="img-thumbnail" width="50" height="35" /><input type="hidden" name="hidden_item_image" value="'.$row["item_image"].'" />';
			  }
			  else
			  {
			   $output['item_image'] = '<input type="hidden" name="hidden_item_image" value="" />';
			  }
			           
       
          
		}
		echo json_encode($output);
	}
	if($_POST['btn_action'] == 'Edit')
	{
		//	$item_no = $_POST['item_type'];
		$item_id = $_POST['item_id'];
		$image = '';
		  if($_FILES["item_image"]["name"] != '')
		  {
		   $image = upload_image();
		  }
		  else
		  {
		   $image = $_POST["hidden_item_image"];
		  }
		$dated = date('Y-m-d');
		
			$query = "
			UPDATE items SET
				dated = ".$dated.", 
				item_no = '".$_POST["item_no"]."',
				item_name = '".$_POST["item_name"]."',
				description = '".$_POST["description"]."', 				
				type_id = '".$_POST["item_type"]."',
				category_id = '".$_POST["category_id"]."',
        dimension = '".$_POST["dimension"]."',
				min_qty = '".$_POST["min_qty"]."',
				max_qty = '".$_POST["max_qty"]."',
        location = '".$_POST["location"]."',
        ctn = 0,
        hs_code = '".$_POST["hs_code"]."',
				item_cost = '".$_POST["item_cost"]."',				
				retail_price = '".$_POST["retail_price"]."',
        list_price = '".$_POST["retail_price"]."',
					
				item_image  = '".$image."',
				user_id = $user_id,
				status = '".$_POST["status"]."'
				WHERE item_id = '".$item_id."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();
		
		if(isset($result))
		{
			echo 'Item Details Edited';
		}
	}
	if($_POST['btn_action'] == 'delete')
	{
		$status = 'Inactive';
		if($_POST['status'] == 'Active')
		{
			$status = 'Inactive';
		}
		$query = "
		UPDATE items 
		SET status = :status 
		WHERE item_id = :item_id
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':status'	=>	$status,
				':item_id'		=>	$_POST["item_id"]
			)
		);	
		$result = $statement->fetchAll();	
		if(isset($result))
		{
			echo 'Item Status change to ' . $status;
		}
	}
?>