<?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"=>"401","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)
            {
                $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();
                $returnArr = array("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
        {
            $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`)values('$shop_id','$shop_code','$name','$imei','$email','$mobile','$timestamp','$password','$hash_password')");
            
            $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)
                {
                    $c = $con->query("select * from user where mobile='$mobile' and status = 1 and password='$password' and shop_id = '$shop_id' and shop_code = '$shop_code'");
                    $user_details = $c->fetch_assoc();
                    
                    $returnArr = array("user"=>$user_details,"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)
                    {
                        $c = $con->query("select * from user where mobile='$mobile' and status = 1 and shop_id = '$shop_id' and shop_code = '$shop_code'");
                        $user_details = $c->fetch_assoc();
                        
                        $returnArr = array("user"=>$user_details,"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);
?>