<?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');
		$item_no = $_POST['item_type'];
		$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_name,description,type_id,dimension,uom,min_qty,max_qty,location,ctn,item_cost,retail_price,list_price,item_image,user_id,status)
        VALUES (:dated, :item_name,:description, :type_id, :dimension,
           :uom, :min_qty, :max_qty, :location,:ctn,:item_cost, :retail_price, :list_price, :item_image, :user_id, :status)
    ";	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
          
          ':dated'             =>  $dated,          
          ':item_name'          =>  trim($_POST["item_name"]),
          ':description'          =>  trim($_POST["description"]),          
          ':type_id'       =>  trim($_POST["item_type"]),
          ':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'       =>  trim($_POST["ctn"]),
          ':item_cost'       =>  trim($_POST["item_cost"]),          
          ':retail_price'       =>  trim($_POST["retail_price"]),
          ':list_price'       =>  trim($_POST["list_price"]),        
                  
          ':item_image'  => $image,
          ':user_id'       =>  $user_id,
          ':status'       =>  'Active'
          )
		);
		$statement = $connect->query("SELECT LAST_INSERT_ID()");
      	$item_id = $statement->fetchColumn();
		$result = $statement->fetchAll();
		$new_item_no = $item_no .$item_id;
		$sql = "update items set item_no = '".$new_item_no."' where item_id = '".$item_id."'";
		$statement = $connect->prepare($sql);
		$statement->execute();
		
		if(isset($statement))
		{
			echo 'New Item Added';
		}
	}
		else { echo 'Item name Already Exist';}
	
	}
	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['description']	=  $row['description'];
           $output['type_id']	=  $row['type_id'];
           $output['dimension'] =  $row['dimension'];           
           
           $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['item_cost']	=  $row['item_cost'];          
           $output['retail_price']	=  $row['retail_price'];
           $output['list_price']  =  $row['list_price'];
          //$output['op_stock']	=  $row['op_stock'];
           $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_name = '".$_POST["item_name"]."',
				description = '".$_POST["description"]."', 				
				type_id = '".$_POST["item_type"]."',			
        dimension = '".$_POST["dimension"]."',
        uom = '".$_POST["uom"]."',
				min_qty = '".$_POST["min_qty"]."',
				max_qty = '".$_POST["max_qty"]."',
        location = '".$_POST["location"]."',
        ctn = '".$_POST["ctn"]."',
				item_cost = '".$_POST["item_cost"]."',				
				retail_price = '".$_POST["retail_price"]."',
        list_price = '".$_POST["list_price"]."',
						
				item_image  = '".$image."',
				user_id = $user_id,
				status = '".$_POST["status"]."'
				WHERE item_id = '".$item_id."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();
		$new_item_no = $item_no .$item_id;
		$sql = "update items set item_no = '".$new_item_no."' where item_id = '".$item_id."'";
		$statement = $connect->prepare($sql);
		$statement->execute();
	
		
		if(isset($statement))
		{
			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;
		}
	}
}	
?>