<?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)
    {
        $orders = $con->query("select * from orders where status ='completed' and shop_id='$shop_id' and shop_code='$shop_code'")->num_rows;
        $riders = $con->query("select * from product where status ='1' and shop_id='$shop_id' and shop_code='$shop_code'")->num_rows;
        $users = $con->query("select * from user where status ='1' and shop_id='$shop_id' and shop_code='$shop_code'")->num_rows;
        $total = 0;
        $sels = $con->query("select * from orders where status ='completed' and shop_id='$shop_id' and shop_code='$shop_code'");
        while($row = $sels->fetch_assoc())
        {
            $total += $row['total'];
        }
        $sales = $total;
        $sales = number_format($sales,2);
        
    	$se = $con->query("select shop_logo from shop_details where status ='1' and shop_id='$shop_id' and shop_code='$shop_code'");
        while($row = $se->fetch_assoc())
        {
            $img= $row['shop_logo'];
        }
        
        $sql = "SELECT * FROM orders where status = 'completed' and shop_id='$shop_id' and shop_code='$shop_code' order by order_date DESC, id DESC limit 5";
        $result = mysqli_query($con,$sql);
        while($rows = mysqli_fetch_assoc($result))
        {
            $cat =  $con->query("select name,mobile from user where id=".$rows['uid']."");
        	if($row = $cat->fetch_assoc())
        	$name = $row['name'];
        	$date = $rows['datesloat'];
        	$orderid = $rows['order_id'];
        	$paymode = $rows['p_method'];
        	$time = $rows['d_time'];
        	$time = date("g:i a", strtotime($time));
        	$rid = $rows['rid'];
        	$rt =  $con->query("select name from assist_details where deli_code='$rid'");
        	if($row = $rt->fetch_assoc())
        	$assname = $row['name'];

        	$p['img'] = $img;
        	$p['subject'] = $name . " (". $orderid . ")";
        	$p['content'] = $paymode . " Order delivered on ".$date.", ".$time." by ".$assname;
            $re_orders[] = $p;
        }
        
        $uuu = $con->query("select * from user where shop_id='$shop_id' and shop_code='$shop_code' order by use_date DESC limit 6");
        $i=0;
        while($row1 = $uuu->fetch_assoc())
        {
            $i = $i + 1;
            $q['sno'] = $i;
        	$q['name'] = $row1['name'];
        	$q['mobile'] = $row1['mobile'];
        	$dd = new DateTime($row1['use_date']);
            $q['u_date'] = $dd->format('d-m-Y'); 
        	$re_cus[] = $q;
        }
        
        $uut = $con->query("select * from orders where shop_id='$shop_id' and shop_code='$shop_code' and status = 'completed'");
        while($row2 = $uut->fetch_assoc())
        {
            $str =  $row2['pid'];
            $uids =  $row2['uid'];
        	$pid[] = explode("$;",$str);
        }
        $result = call_user_func_array("array_merge", $pid);
        $new_res = array_count_values($result);
        
        $count_res = count($new_res);
        
        $keys = array_keys($new_res);
        
        for($i=0; $i < $count_res; $i++) {
            $pro_id = $keys[$i];
            $sel_id = $con->query("select * from product where shop_id='$shop_id' and shop_code='$shop_code' and id = '$pro_id'");
            while($rows_id = $sel_id->fetch_assoc())
            {
            		$pr['name'] = $rows_id['pname'];
            		$pr['value'] = $new_res[$keys[$i]];
            		$pro[] = $pr;
            }
        }

        $rows = array('orders'=>$orders,'products'=>$riders,'users'=>$users,'sales'=>$sales,'re_orders'=>$re_orders,'re_customers'=>$re_cus,'pid'=>$result,'pro_id'=>$pro);
        echo json_encode($rows);
    }
    else
    {
        $rows = array('status'=>"",'message' => 'Data Error');
        echo json_encode($rows);
    }
}

?>