<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
 
$uid = $data['uid'];
if($uid == '' || $data['shop_id'] == '')
{
	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong  try again !");
}
else 
{
    // $shop_id = "8316293";
    // $shop_code = "Ey3a8s7U79";
    $shop_id = strip_tags(mysqli_real_escape_string($con,$data['shop_id']));
    $shop_code = strip_tags(mysqli_real_escape_string($con,$data['shop_code']));
    
    $chek = $con->query("select * from user where id = '$uid' and status = '1' and shop_id = '$shop_id' and shop_code = '$shop_code'");

    if($chek->num_rows != 0)
    {
        $v = array();
    	$cp = array(); 
    	$d = array();
    	$p = array();
    	$sec = array();
    	
        $sel = $con->query("select * from banner where shop_id = '$shop_id' and shop_code = '$shop_code'");
        while($row = $sel->fetch_assoc())
        {
            $v[] = $row;
        }
        
        $current_time = date("H:i");
        
        $sels = $con->query("SELECT category.*, shop_settings.start_time, shop_settings.end_time 
        FROM category 
        INNER JOIN shop_settings on 
        category.cattime != '' and category.shop_id = shop_settings.shop_id
        where category.catstatus='1' and category.shop_id = '$shop_id' and category.shop_code = '$shop_code' and
        TIME_TO_SEC(IF (category.cattime = 'Default', shop_settings.start_time, category.catstarttime)) < TIME_TO_SEC('$current_time')
        and TIME_TO_SEC(IF (category.cattime = 'Default', shop_settings.end_time, category.catstoptime)) > TIME_TO_SEC('$current_time') 
        or category.catstatus='1' and category.shop_id = '$shop_id' and category.shop_code = '$shop_code' and
        TIME_TO_SEC(IF (category.cattime = 'Customise',category.catstarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
        and TIME_TO_SEC(IF (category.cattime = 'Customise',category.catstoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time') limit 6");
        
        while($rows = $sels->fetch_assoc())
        {
            $p['id'] = $rows['id'];
    		$p['catname'] = $rows['catname'];
    		$p['catimg'] = $rows['catimg'];
    		$sid = $rows['id'];
    		$p['count'] = $con->query("select * from subcategory where cat_id='$sid' and shop_id = '$shop_id' and shop_code = '$shop_code'")->num_rows;
    		$cp[] = $p;
        }
    
        include 'product_with_dis_home.php';
    
    	$curr = $con->query("SELECT
            shop_details.manager_mobile,
            shop_details.latitude,
            shop_details.longitude,
            shop_details.doc_no,
            shop_settings.*
            FROM shop_settings
            
            INNER JOIN shop_details ON
            shop_settings.shop_id = shop_details.shop_id
            
            WHERE shop_settings.shop_id = '$shop_id' and shop_settings.shop_code = '$shop_code'")->fetch_assoc();
            
            
        $cy = $con->query("select * from coupon_code where status='1' and admin_status='1' and shop_id = '$shop_id' and shop_code = '$shop_code' order by priority ASC");
    	$coupon_count = $con->query("select * from coupon_code where status='1' and admin_status='1' and shop_id = '$shop_id' and shop_code = '$shop_code' order by priority ASC")->num_rows;
    	if($coupon_count != 0)
    	{
        	$cc = array();
        	$q = array();
    	    while($row = $cy->fetch_assoc())
        	{
        	    $code = $row['code'];
        	    $count = $con->query("select * from orders where uid='$uid' and coupon_code='$code'")->num_rows;
        	    $use_limit = intval($row['max_use_limit']);
        	    if($use_limit != 0)
        	    {
            	    if($count < $use_limit)
            	    {
                		$cc['id'] = $row['id'];
                		$cc['name'] = $row['name'];
                		$cc['image'] = $row['image'];
                		$cc['code'] = $row['code'];
                		$cc['discount'] = $row['discount'];
                		$cc['description'] = $row['description'];
                		$cc['max_order'] = $row['max_order_amount'];
                		$cc['max_discount'] = $row['max_discount_amount'];
                		$cc['status'] = $row['status'];
                		$q[] = $cc;
            	    }
        	        
        	    }
        	    else
        	    {
        	        $cc['id'] = $row['id'];
            		$cc['name'] = $row['name'];
            		$cc['image'] = $row['image'];
            		$cc['code'] = $row['code'];
            		$cc['discount'] = $row['discount'];
            		$cc['description'] = $row['description'];
            		$cc['max_order'] = $row['max_order_amount'];
            		$cc['max_discount'] = $row['max_discount_amount'];
            		$cc['status'] = $row['status'];
            		$q[] = $cc;
        	    }
        	}
    	}
    	else{
    	    $q = [];
    	}
    	
    	$kp = array("coupon_data"=>$q,"Main_Data"=>$curr,'Banner'=>$v,'Catlist'=>$cp,'Productlist'=>$d,"dynamic_section"=>$sec);
    	
    	$returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Data Get Successfully!","ResultData"=>$kp);

    }
    else  
    {
        $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Your Account Deactivated!");
    }
    
}
echo json_encode($returnArr);