<?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 = 'jv_edit'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_edit = $statement->fetchColumn();
            $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'jv_delete'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_delete = $statement->fetchColumn();
            $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'jv_print'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_print = $statement->fetchColumn();
$query = '';
$output = array();
$query .= "
SELECT m.jv_id,m.dated,m.jv_no,m.source,m.description,u.user_name
from jv m,user_detail u WHERE m.user_id=u.user_id and m.status = 'Active' AND m.source = 'GL Jv' AND";
if(isset($_POST["search"]["value"]))
{
	$query .= '(source LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR jv_id LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR jv_no LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR m.dated LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR user_name LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR description LIKE "%'.$_POST["search"]["value"].'%" )';
	
	
}
if(isset($_POST["order"]))
{
	$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'ORDER BY jv_id 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)
{
	
	$sub_array = array();
	$sub_array[] = $row['jv_id'];	
	$sub_array[] = $row['dated'];
	$sub_array[] = $row['jv_no'];
	$sub_array[] = $row['source'];
	$sub_array[] = $row['description'];
	$sub_array[] = $row['user_name'];
	
	
	
	
	if($jv_edit=='ON')
	{
		$sub_array[] = '<a href="jv_edit.php?update=1&id='.$row["jv_id"].'"><span class="btn btn-warning"><i class="fa fa-edit"></i></span></a>';
	}	
		
	if($jv_print=='ON')
	{
		$sub_array[] = '<a href="jv_print?print=1&id='.$row["jv_id"].'"><span class="btn btn-success "><i class="fa fa-print"></i></span></a>';
	}
	if($jv_delete=='ON')
	{
		$sub_array[] = '<button type="button" name="delete" id="'.$row["jv_id"].'" class="btn btn-danger  delete" data-status=""><i class="fa fa-trash-o"></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 jv");
	$statement->execute();
	return $statement->rowCount();
}
?>