<?php
//user_fetch.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();
}
$output = array();
$sql = "SELECT app_status FROM app_settings WHERE app_name = 'stock_value'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $stock_value = $statement->fetchColumn();
          $sql = "SELECT IFNULL(doc_status,'OFF') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'cost'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $cost = $statement->fetchColumn();
$query = '';      

$query .= "SELECT i.item_image,i.item_id,i.item_no,CONCAT(t.type_name,' - ',i.item_name)item_name,i.item_cost,i.retail_price, i.stock from items i, item_type t WHERE
 t.type_id=i.type_id AND";		
   
//$query .= 'SELECT i.item_no,i.item_name,i.item_type,i.item_size,i.item_cost,i.item_cost_sm,
//s.stock as op_stock from items i,cur_stock s where i.item_no=s.item_no AND';
if(isset($_POST["search"]["value"]))
{
	$query .= '(item_name LIKE "%'.$_POST["search"]["value"].'%" ';
	
	
	$query .= 'OR stock LIKE "%'.$_POST["search"]["value"].'%" )';
	
	
	
}
if(isset($_POST["order"]))
{
	$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'ORDER BY item_name ASC ';
}
if($_POST["length"] != -1)
{
	$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
	
	
	$sub_array = array();
	//$sub_array[] = $row['item_id'];
	$sub_array[] = $row['item_no'];
	$sub_array[] = $row['item_name'];
	
	
	
	if($cost=='ON')
	{
		$sub_array[] = $row['item_cost'];
		
	}
	
	$sub_array[] = $row['retail_price'];
	$sub_array[] = $row['stock'];
	//$sub_array[] = '<a href="item_detail.php?detail=1&id='.$row["item_no"].'&stock='.$row["op_stock"].'" target="_blank">Search&nbsp;<span class="glyphicon glyphicon-search"></span></a>';
	$sub_array[] = '<button type="button" name="findme" id="'.$row["item_id"].'" stock="'.$row["stock"].'"class="form-control btn btn-primary findme"><i class="fa fa-search"></i></button>';
	
	
	
	$data[] = $sub_array;
}
$output = array(
	"draw"				=>	intval($_POST["draw"]),
	"recordsTotal"  	=>  $filtered_rows,
	"recordsFiltered" 	=> 	get_total_all_records($connect),
	"data"    			=> 	$data
);
echo json_encode($output);
function get_total_all_records($connect)
{
	$statement = $connect->prepare("SELECT * FROM items");
	$statement->execute();
	return $statement->rowCount();
}
?>