
<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['category_id'] == '' || $data['shop_id'] == '')
{
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");
}
else
{
	$cat_id = $data['category_id'];
	$shop_id = strip_tags(mysqli_real_escape_string($con,$data['shop_id']));
    $shop_code = strip_tags(mysqli_real_escape_string($con,$data['shop_code']));
    
	$current_time = date("H:i");
	
	$sql_query = "SELECT subcategory.*, shop_settings.start_time, shop_settings.end_time 
    FROM subcategory INNER JOIN shop_settings on subcategory.cattime != '' and subcategory.shop_id = shop_settings.shop_id
    where subcategory.catstatus='1' and subcategory.cat_id='$cat_id' and subcategory.shop_id = '$shop_id' and subcategory.shop_code = '$shop_code' and
    TIME_TO_SEC(IF (subcategory.cattime = 'Default', shop_settings.start_time, subcategory.catstarttime)) < TIME_TO_SEC('$current_time')
    and TIME_TO_SEC(IF (subcategory.cattime = 'Default', shop_settings.end_time, subcategory.catstoptime)) > TIME_TO_SEC('$current_time') 
    or subcategory.catstatus='1' and subcategory.cat_id='$cat_id' and subcategory.shop_id = '$shop_id' and subcategory.shop_code = '$shop_code' and
    TIME_TO_SEC(IF (subcategory.cattime = 'Customise',subcategory.catstarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
    and TIME_TO_SEC(IF (subcategory.cattime = 'Customise',subcategory.catstoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time')";
	
	$sel = $con->query($sql_query);
    
    // $sel = $con->query("select * from subcategory where cat_id=".$cat_id." and TIME_TO_SEC(catstarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(catstoptime) < TIME_TO_SEC('$current_time') and catstatus='1'");
    
    $count = $con->query("select * from subcategory where cat_id='$cat_id' and catstatus='1' and shop_id = '$shop_id' and shop_code = '$shop_code'")->num_rows;
    if($count != 0)
    {
    	$myarray = array();
        while($row = $sel->fetch_assoc())
        {
            $p['id'] = $row['id'];
    	    $p['cat_id'] = $row['cat_id'];
    		$p['name'] = $row['name'];
    		$p['img'] = $row['img'];
    		$sid = $row['id'];
    		$p['count'] = $con->query("select * from product where sid='$sid' and shop_id = '$shop_id' and shop_code = '$shop_code'")->num_rows;
    		$myarray[] = $p;
        }
        $returnArr = array("data"=>$myarray,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Subcategory List Founded!");
    }
    else 
    {
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>$sql_query);	
    }
}
echo json_encode($returnArr);
?>