<?php

//category_fetch.php
 $key=strtolower(strip_tags($_REQUEST['key']));
include('db.php');
$i=0;
$query = '';

$output = array();

// $query .= "SELECT * FROM item_category where type='furnuture'";
$query .= "SELECT * FROM item_category where type='$key'";
$querry = "SELECT * FROM item_category where type='$key'";
$search_query = "SELECT * FROM item_category ";

if(isset($_POST["search"]["value"]))
{
	// $search_query .= 'WHERE type LIKE "%'.$key.'%" ';
	$search_query .= 'WHERE category LIKE "%'.$_POST["search"]["value"].'%" ';
}

if(isset($_POST['order']))
{
	$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'ORDER BY id DESC ';
}

if($_POST['length'] != -1)
{
	$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}

$statement = $connect->prepare($querry);

$statement->execute();

$result = $statement->fetchAll();

$data = array();

$filtered_rows = count($result);

foreach($result as $row)
{
	$status = '';
	$i++;
	if($row['category'] == 'active')
	{
		$status = '<span class="label label-success">Active</span>';
	}
	else
	{
		$status = '<span class="label label-danger">Inactive</span>';
	}
	$sub_array = array();
	// $sub_array[] = $row['id'];
	$sub_array[] = $i;
	$sub_array[] = ucfirst($row['category']);
	$sub_array[] = '<button type="button" name="update" id="'.$row["id"].'" class="btn btn-warning btn-xs update">Update</button>';
	$sub_array[] = '<button type="button" name="delete" id="'.$row["id"].'" class="btn btn-danger btn-xs delete" data-status="'.$row["category"].'">Delete</button>';
	$data[] = $sub_array;
}

$output = array(
	"draw"			=>	intval($_POST["draw"]),
	"recordsTotal"  	=>  $filtered_rows,
	"recordsFiltered" 	=> 	get_total_all_records($connect),
	"data"				=>	$data
);

function get_total_all_records($connect)
{
	// $key=10;
	$key=strtolower($_REQUEST['key']);
	$query = "SELECT * FROM item_category where type='$key'";
	$statement = $connect->prepare($query);
	$statement->execute();
	return Count($statement->fetchAll());
	// return $key;
}

echo json_encode($output);

?>