<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['mobile'] == '' || $data['shop_id'] == '')
{
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");
}
else
{
    $mobile = strip_tags(mysqli_real_escape_string($con,$data['mobile']));
    $type = strip_tags(mysqli_real_escape_string($con,$data['type']));
    $shop_id = strip_tags(mysqli_real_escape_string($con,$data['shop_id']));
    $shop_code = strip_tags(mysqli_real_escape_string($con,$data['shop_code']));
    
    if($type == "check")
    {
        $chek = $con->query("select * from user where mobile='$mobile' and shop_id = '$shop_id' and shop_code = '$shop_code'");
    
        if($chek->num_rows != 0)
        {
            $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Login successfully!");
        }
        else  
        {
            $returnArr = array("ResponseCode"=>"201","Result"=>"false","ResponseMsg"=>"Invalid Email/Mobile No or Password!!!");
        }
        
    }
    else if($type == "login")
    {
        $password = strip_tags(mysqli_real_escape_string($con,$data['password']));

        $chek = $con->query("select * from user where (mobile='$mobile' or email='$mobile') and password='$password' and shop_id = '$shop_id' and shop_code = '$shop_code'");
        $status = $con->query("select * from user where status = '1' and (mobile='$mobile' or email='$mobile') and shop_id = '$shop_id' and shop_code = '$shop_code'");
    
        if($chek->num_rows != 0)
        {
            if($status->num_rows !=0)
            {
                $add = array();
                $c = $con->query("select * from user where (mobile='$mobile' or email='$mobile')  and status = 1 and password='$password' and shop_id = '$shop_id' and shop_code = '$shop_code'");
                $c = $c->fetch_assoc();
                $u_id = $c['id'];
                $shop_de = $con->query("select * from shop_details where shop_id = '$shop_id' and shop_code = '$shop_code'");
            	if($rowww = $shop_de->fetch_assoc())
            	{
            	    $s_latitude = $rowww['latitude'];
                    $s_longitude = $rowww['longitude'];
            	}
                
                $add = array();
            	$d = $con->query("select * from address where uid = '$u_id'");
                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;
                }
                $returnArr = array("address"=>$add,"user"=>$c,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Login successfully!");
            }
            else
            {
                $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Your Account Deactivated!");
            }
        }
        else  
        {
            $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Invalid Password !");
        }
        
    }
    else if($type == "signup")
    {
        $name = strip_tags(mysqli_real_escape_string($con,$data['name']));
        $email = strip_tags(mysqli_real_escape_string($con,$data['email']));
        $imei = strip_tags(mysqli_real_escape_string($con,$data['imei']));
        $password = strip_tags(mysqli_real_escape_string($con,$data['password']));

        $checkmob = $con->query("select * from user where mobile='$mobile' and shop_id = '$shop_id' and shop_code = '$shop_code'");
        
        if($checkmob->num_rows != 0)
        {
            $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Mobile Number Already Registered!");
        }
        else
        {
            $coins = 10;
            $timestamp = date("Y-m-d H:i:s");
            $hash_password = md5($password);
            $con->query("insert into user(`shop_id`,`shop_code`,`name`,`imei`,`email`,`mobile`,`use_date`,`password`,`hash_password`,`coins`)values
            ('$shop_id','$shop_code','$name','$imei','$email','$mobile','$timestamp','$password','$hash_password','$coins')");
            
            $chek = $con->query("select * from user where mobile='$mobile' and password='$password' and shop_id = '$shop_id' and shop_code = '$shop_code'");
            $status = $con->query("select * from user where status = '1' and mobile='$mobile' and shop_id = '$shop_id' and shop_code = '$shop_code'");
            
            if($status->num_rows !=0)
            {
                if($chek->num_rows != 0)
                {
                    $add = array();
                    $c = $con->query("select * from user where (mobile='$mobile' or email='$mobile')  and status = 1 and password='$password' and shop_id = '$shop_id' and shop_code = '$shop_code'");
                    $c = $c->fetch_assoc();
                    $u_id = $c['id'];

                    $transaction_name = "Signup Bonus";
                    $datesloat = date("d-m-Y");
                    $timesloat = date("H:i A");   
                    $sql_coin = "insert into coin_transactions(`uid`,`name`,`coins`,`coin_type`,`c_date`,`c_time`)values('$u_id','$transaction_name','$coins','1','$datesloat','$timesloat')";
                    $con->query($sql_coin);
                    
                    $returnArr = array("user"=>$c,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Registration successfully!");
                }
                else  
                {
                    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Error! Retry Again!");
                }
            }
        }
        
    }
    else if($type == "forgot")
    {
        $password = strip_tags(mysqli_real_escape_string($con,$data['password']));
        
        $counter = $con->query("select * from user where mobile='$mobile' and shop_id = '$shop_id' and shop_code = '$shop_code'");
        
        if($counter->num_rows != 0)
        {
            $hash = md5($password);
            $sql ="update user set `password` ='$password',`hash_password` = '$hash' where mobile='$mobile' and shop_id = '$shop_id' and shop_code = '$shop_code'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $chek = $con->query("select * from user where mobile='$mobile' and shop_id = '$shop_id' and shop_code = '$shop_code'");
                $status = $con->query("select * from user where status = '1' and mobile='$mobile' and shop_id = '$shop_id' and shop_code = '$shop_code'");
                
                if($status->num_rows !=0)
                {
                    if($chek->num_rows != 0)
                    {
                        $add = array();
                        $c = $con->query("select * from user where (mobile='$mobile' or email='$mobile')  and status = 1 and password='$password' and shop_id = '$shop_id' and shop_code = '$shop_code'");
                        $c = $c->fetch_assoc();
                        $u_id = $c['id'];
                        $shop_de = $con->query("select * from shop_details where shop_id = '$shop_id' and shop_code = '$shop_code'");
                    	if($rowww = $shop_de->fetch_assoc())
                    	{
                    	    $s_latitude = $rowww['latitude'];
                            $s_longitude = $rowww['longitude'];
                    	}
                        
                        $add = array();
                    	$d = $con->query("select * from address where uid = '$u_id'");
                        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;
                        }
                        $returnArr = array("address"=>$add,"user"=>$c,"ResponseCode"=>$sql,"Result"=>"true","ResponseMsg"=>"Password Changed Successfully");
                    }
                    else  
                    {
                        $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Error! Retry Again!");
                    }
                }
            }
        }
        else
        {
            $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Mobile Not Registered!");  
        }
        
    }
}
echo json_encode($returnArr);
?>