<?php 

require 'db.php';

if(isset($_GET['sid']) && isset($_GET['token']))
{
    $shop_id = $_GET['sid'];
    $shop_code = $_GET['token'];

    include 'token_check.php';
    
    if($token_check)
    {
        $cat = $con->query("select * from category where shop_id='$shop_id' and shop_code='$shop_code'");
        while($rows = $cat->fetch_assoc())
        {
            $p['id'] = $rows['id'];
        	$p['catname'] = $rows['catname'];
        	$p['catimg'] = $rows['catimg'];
        	$p['count'] = $con->query("select * from subcategory where cat_id=".$rows['id']." and shop_id='$shop_id' and shop_code='$shop_code'")->num_rows;
        	$p['catstatus'] = $rows['catstatus'];
        	$category[] = $p;
        }
        
        $subcat = $con->query("select * from subcategory where shop_id='$shop_id' and shop_code='$shop_code'");
        while($rows = $subcat->fetch_assoc())
        {
            $s['id'] = $rows['id'];
        	$s['subcatname'] = $rows['name'];
        	$s['subcatimg'] = $rows['img'];
        	$cat =  $con->query("select catname from category where id=".$rows['cat_id']." and shop_id='$shop_id' and shop_code='$shop_code'");
        	if($row = $cat->fetch_assoc())
        	$s['catname'] = $row['catname'];
        	$s['catstatus'] = $rows['catstatus'];
        	$subcategory[] = $s;
        }

        $prod = $con->query("select * from product where shop_id='$shop_id' and shop_code='$shop_code'");
        while($rows = $prod->fetch_assoc())
        {
        	$result['id'] = $rows['id'];
            $result['product_name'] = $rows['pname'];
            $result['product_image'] = $rows['pimg'];
            $result['product_type'] = $rows['ptype'];
            $result['product_desc'] = $rows['psdesc'];
            $result['popular'] = $rows['popular'];
            $result['status'] = $rows['status'];
            
            $result['cattime'] = $rows['protime'];
            if($result['cattime'] == "Default")
        	{
        	    $sel = $con->query("select start_time,end_time from shop_settings where shop_id='$shop_id' and shop_code='$shop_code'");
                if($row = $sel->fetch_assoc())
                {
                    $result['catstarttime'] = $row['start_time'];
                    $result['catstoptime'] = $row['end_time'];
                }
        	}
        	else
        	{
        	    $result['catstarttime'] = $rows['prostarttime'];
        	    $result['catstoptime'] = $rows['prostoptime'];
        	}
        	
        	$cat_id = $rows['cid'];
            $scat_id = $rows['sid'];
            $cat =  $con->query("select catname from category where id='$cat_id' and shop_id='$shop_id' and shop_code='$shop_code'");
            if($row1 = $cat->fetch_assoc())
            $result['catname'] = $row1['catname'];
            
            $scat =  $con->query("select name from subcategory where id='$scat_id' and shop_id='$shop_id' and shop_code='$shop_code'");
            if($row2 = $scat->fetch_assoc())
            $result['subcatname'] = $row2['name'];
            
            $a = explode('$;',$rows['pgms']);
            $ab = explode('$;',$rows['pprice']);
            $k=array();
            for($i=0;$i<count($a);$i++)
            {
                $k[$i] = array("product_type"=>$a[$i],"product_price"=>$ab[$i]);
            }
            
            $result['price'] = $k;
            $result['discount'] = $rows['discount'];
            $result['stock'] = $rows['stock'];
            $product[] = $result;
        }
        
        $json_array = array(categorylist=>$category,subcategorylist=>$subcategory,productlist=>$product);
        echo json_encode($json_array); 
    }
    else
    {
        $rows = array('status'=>"",'message' => 'Data Error');
        echo json_encode($rows);
    }
}


?>