<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);

// $iconx_code = "9876543211";
$iconx_code = $data['iconx_code'];
$uid = $data['user_id'];

if($iconx_code == '')
{
	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong try again !");
}
else
{
	$shops = $con->query("select * from shop_list where iconx_code='$iconx_code'");
	if($roww = $shops->fetch_assoc())
	{
	    $shop_id = $roww['shop_id'];
        $shop_code = $roww['shop_code'];
	}

    if($shop_id == '' || $shop_code == '')
    {
    	$returnArr = array("ResponseCode"=>$shop_id,"Result"=>$shop_code,"ResponseMsg"=>"Something Went wrong try again !");
    }
    else 
    { 
        $current_time = date("H:i");
        
        $sql_que = "SELECT
            shop_details.shop_id,
            shop_details.shop_code,
            shop_details.shop_logo,
            shop_details.shop_type,
            shop_details.shop_desc,
            shop_details.doc_no,
            shop_details.gst_no,
            shop_details.max_km,
            shop_details.app_version,
            shop_details.app_download,
            shop_details.latest_app_url,
            shop_details.expiry_date,
            shop_details.manager_mobile,
            shop_details.latitude,
            shop_details.longitude,
            shop_settings.tips_value,
            shop_settings.shop_fb,
            shop_settings.shop_in,
            shop_settings.shop_tw,
            shop_settings.shop_message,
            shop_settings.start_time,
            shop_settings.end_time,
            shop_settings.shop_status
            FROM shop_settings
            
            INNER JOIN shop_details ON
            shop_settings.shop_id = shop_details.shop_id
            
            WHERE
            shop_details.expiry_date >=CURRENT_TIMESTAMP and shop_settings.shop_id = '$shop_id' and shop_settings.shop_code = '$shop_code'";
        
        $sql_query = "SELECT
            shop_details.expiry_date,
            shop_details.manager_mobile,
            shop_details.latitude,
            shop_details.longitude,
            shop_settings.shop_message,
            shop_settings.start_time,
            shop_settings.end_time,
            shop_settings.shop_status
            FROM shop_settings
            
            INNER JOIN shop_details ON
            shop_settings.shop_id = shop_details.shop_id
            
            WHERE
            shop_details.expiry_date >=CURRENT_TIMESTAMP and TIME_TO_SEC(shop_settings.start_time) < TIME_TO_SEC('$current_time') 
            and TIME_TO_SEC(shop_settings.end_time) > TIME_TO_SEC('$current_time') 
            and shop_settings.shop_id = '$shop_id' and shop_settings.shop_code = '$shop_code'";
        
    	$cur = $con->query($sql_que)->fetch_assoc();
    	$curr = $con->query($sql_query)->fetch_assoc();
    	
    	$s_latitude = $curr['latitude'];
        $s_longitude = $curr['longitude'];
    	
    	$on_title = ["Merchantt","Best-Loved","Hassle Free","Bestt Deals"];
    	
    	$on_content  = ["Choose your Favourite Merchant","Identity your Best-Loved Thing","Hassle Free Payments with Convenient","Enjoy Your Best Deals with RECIDE"];
    	
    	$on_image = ["https://app.iconxtechnologies.in/image/test.png","https://app.iconxtechnologies.in/image/test.png","https://app.iconxtechnologies.in/image/test.png","https://app.iconxtechnologies.in/image/test.png"];
    	
    	$on_bot_title = ["You Dream, We Deliver","Instant, Hot and Yummy","We Care about you & your Food","We Deliver Fantasy at your doorstep"];
    	
    	$onboard = array("title"=>$on_title,"image"=>$on_image,"content"=>$on_content,"bot_title"=>$on_bot_title);
    	
    	$add = array();
    	$d = $con->query("select * from address where uid = '$uid'");
        while ($row = $d->fetch_assoc()) {

            $addd['latitude'] = $row['latitude'];
            $addd['longitude'] = $row['longitude'];

            $addd['delivery_charge'] = 0;
            $addd['distance'] = $row['distance'];
            $addd['shop_distance'] = $row['shop_distance'];

            $addd['id'] = $row['id'];
            $addd['hno'] = $row['hno'];
            $addd['area'] = $row['area'];
            $addd['landmark'] = $row['landmark'];
            $addd['name'] = $row['name'];
            $addd['pincode'] = $row['pincode'];
            $addd['street'] = $row['street'];
            $addd['type'] = $row['type'];
            $addd['uid'] = $row['uid'];
            $add[] = $addd;
        }
        
        $dis = array();
    	$e = $con->query("select * from distance_db where status = 1 and shop_id = '$shop_id' and shop_code = '$shop_code' ORDER BY distance ASC");
        while ($rowe = $e->fetch_assoc()) {
            $diss['id'] = $rowe['id'];
            $diss['title'] = $rowe['name'];
            $diss['d_charge'] =  $rowe['dcharge'];
            $diss['distance'] =  $rowe['distance'];
            $dis[] = $diss;
        }
        
        $user = $con->query("select * from user where id = '$uid'");
        if($row_user = $user->fetch_assoc()) {
            $coins = $row_user['coins'];
        }
        
        $chek_shop = $con->query("select * from shop_details where shop_code = '$shop_code' limit 1");
        while($shop = $chek_shop->fetch_assoc())
        {
            $sho = array();
            $s_latitude = $shop['latitude'];
            $s_longitude = $shop['longitude'];
            
            $sho['delivery_charge'] = 0;
            $sho['latitude'] = $shop['latitude'];
            $sho['longitude'] =  $shop['longitude'];
            $sho['distance'] = "0";
            $sho['address1'] = $shop['address1'];
            $sho['address2'] = $shop['address2'];
            $sho['area'] = $shop['area'];
            $sho['city'] = $shop['city'];
            $sho['pincode'] = $shop['pincode'];
            $sho['shop_name'] = $shop['shop_name'];
            $sho['owner'] = $shop['owner'];
            $sho['owner_mobile'] = $shop['owner_mobile'];
            $sho['manager'] = $shop['manager'];
            $sho['manager_mobile'] = $shop['manager_mobile'];
            $sho['doc_no'] = $shop['doc_no'];
            $sho['shop_id'] = $shop['shop_id'];
            $sho['shop_code'] = $shop['shop_code'];
            $shopp[] = $sho;
        }
        
        $is_raining = 0;
    	$is_govt = 0;

    	$kp = array("coins"=>$coins,"rain"=>$is_raining,"govt"=>$is_govt,"Main_Data"=>$cur,"onboard_data"=>$onboard,"deli_charges"=>$dis,"address"=>$add,"shops"=>$shopp);
    	if($curr != null)
    	{
    	    $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Data Get Successfully!","ResultData"=>$kp);
    	}
    	else
    	{
    	    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"No data","ResultData"=>$kp);
    	}
    	echo json_encode($returnArr);
    }
    
}


?>