<?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();
}
 $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'items_edit'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $items_edit = $statement->fetchColumn();
            $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'items_delete'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $items_delete = $statement->fetchColumn();
$query = '';
$output = array();
$query .= "
SELECT i.item_id,i.item_no,i.item_name,i.item_image,i.status,t.type_name, i.retail_price from 
items i,item_type t WHERE t.type_id=i.type_id AND ";
if(isset($_POST["search"]["value"]))
{
	$query .= '(item_name LIKE "%'.$_POST["search"]["value"].'%" ';
	
	$query .= 'OR type_name 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 DESC ';
}
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)
{
	$image = '';
	 if($row["item_image"] != '')
	 {
	  $image = '<img src="upload/'.$row["item_image"].'" class="img-thumbnail" width="100" height="100" />';
	 }
	 else
	 {
	  $image = '';
	 }
	$status = '';
	if($row["status"] == 'Active')
	{
		$status = '<span class="label label-success">Active</span>';
	}
	else
	{
		$status = '<span class="label label-danger">Inactive</span>';
	}
	$sub_array = array();
	$sub_array[] = $image;
	$sub_array[] = $row['item_no'];	
	$sub_array[] = $row['item_name'];
	
	$sub_array[] = $row['type_name'];
	$sub_array[] = $row['retail_price'];
	
	$sub_array[] = $status;
	if($items_edit=='ON')
	{
		$sub_array[] = '<button type="button" name="update" id="'.$row["item_id"].'" class="btn btn-warning update"><i class="fa fa-edit"></i></button>';	
	}
	
	if($items_delete='ON')
	{
		$sub_array[] = '<button type="button" name="delete" id="'.$row["item_id"].'" class="btn btn-danger  delete" data-status=""><i class="icon-copy fa fa-trash-o" aria-hidden="true"></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 where status = 'Active'");
	$statement->execute();
	return $statement->rowCount();
}
?>