<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['mobile'] == '')
{
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");
}
else
{
    $mobile = strip_tags(mysqli_real_escape_string($con,$data['mobile']));
    $imei = strip_tags(mysqli_real_escape_string($con,$data['imei']));
    $delicode = strip_tags(mysqli_real_escape_string($con,$data['deli_code']));
    $onekey = strip_tags(mysqli_real_escape_string($con,$data['one_key']));
    
    $che = $con->query("select * from assist_settings where mobile_no ='".$mobile."'");
    
    if($che->num_rows != 0)
    {
        $chek = $con->query("SELECT 
                        assist_details.name,
                        assist_details.deli_code,
                        assist_settings.imei_code,
                        assist_settings.mobile_no,
                        assist_details.last_name,
                        assist_settings.id
                        FROM assist_settings
                        INNER JOIN assist_details ON
                        assist_settings.deli_code = assist_details.deli_code
                        WHERE assist_settings.status = '1' and assist_settings.mobile_no = '".$mobile."'");
        while($rows = $chek->fetch_assoc())
        {
            $p['id'] = $rows['id'];
            $p['deli_code'] = $rows['deli_code'];
        	$p['imei_code'] = $rows['imei_code'];
        	$p['mobile_no'] = $rows['mobile_no'];
        	$p['name'] = $rows['name'];
        	$p['last_name'] = $rows['last_name'];
        	$c = $p;
        }
        
        $ch = $con->query("select stage_status from assist_details where mobile_no ='".$mobile."'");
        if($row = $ch->fetch_assoc())
        {
            $stage = $row['stage_status'];
        }
        
        if($c != null)
        $returnArr = array("user"=>$c,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Mobile Vaild","isNewUser"=>false,"stage"=>$stage);
        else
        $returnArr = array("user"=>$c,"ResponseCode"=>"200","Result"=>"false","ResponseMsg"=>"Mobile Vaild","isNewUser"=>false,"stage"=>'0');
    }
    else
    {
        $q1 = $con->query("INSERT INTO `assist_settings`(`deli_code`, `imei_code`, `one_id`, `mobile_no`, `assigned_orders`, `accepted_orders`, `rejected_orders`, `my_referrals`, `success_referrals`, 
        `deli_reviews`, `vaccinate_status`, `status`, `admin_status`) VALUES ('".$delicode."','".$imei."','".$onekey."','".$mobile."','0','0','0','0','0','0','0','1','0')");
        
        $q2 = $con->query("INSERT INTO `assist_details`(`deli_code`, `mobile_no`, `stage_status`) 
        VALUES ('".$delicode."','".$mobile."','1')");
                    
        $chek = $con->query("select * from assist_settings where mobile_no ='".$mobile."' and status = 1");
        while($rows = $chek->fetch_assoc())
        {
            $p['id'] = $rows['id'];
            $p['deli_code'] = $rows['deli_code'];
        	$p['imei_code'] = $rows['imei_code'];
        	$p['mobile_no'] = $rows['mobile_no'];
        	$c = $p;
        }

        if($q1 && $q2)
            $returnArr = array("user"=>$c,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Mobile Vaild","isNewUser"=>true,"stage"=>"1");
        else
            $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Some Error Occured!");
    }
}
echo json_encode($returnArr);
?>