<?php
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
if ($data['username'] == '' or $data['password'] == '') {
    $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went Wrong!");
} else {
    
    $mode = strip_tags(mysqli_real_escape_string($con, $data['mode']));
    if($mode == "login") {
        $user_name = strip_tags(mysqli_real_escape_string($con, $data['username']));
        $password = strip_tags(mysqli_real_escape_string($con, $data['password']));
        $imei_code = strip_tags(mysqli_real_escape_string($con, $data['imei_code']));
        $app_code = $data['app_code'];
    
        $chek = $con->query("select * from app_users where (user_name= '$user_name' or mobile='$user_name') and 
                                                            password='$password' and status = 1");
        $chek_imei = $con->query("select * from app_users where (user_name= '$user_name' or mobile='$user_name') and 
                                                            password='$password' and status = 1 and imei_code = '$imei_code'");
    
        $status = $con->query("select * from app_users where status = 1 and (user_name= '$user_name' or mobile='$user_name') ");
    
        if ($status->num_rows != 0) {
            if ($chek_imei->num_rows != 0) {
                $c = $con->query("select * from app_users where (user_name= '$user_name' or mobile='$user_name')
                                                                 and status = 1 and password = '$password' and imei_code = '$imei_code'");
                $c = $c->fetch_assoc();
                // $app_user_count = $con->query("SELECT app_users from app_access 
                //                                                 where app_code = '$app_code'")->fetch_assoc()['app_users'];
                $app_user_count = 0;
                $returnArr = array("data" => $c, "ResponseCode" => "200", 
                                    "Result" => "true", "ResponseMsg" => "Login successfully!", "users_count" => $app_user_count);
            } else {
                if ($imei_code != null) {
                    $sql = "update app_users set `imei_code`= '$imei_code' where (user_name= '$user_name' or mobile='$user_name')
                                                                                    and password = '$password'";
                    $result = $con->query($sql);
                }
    
                if ($chek->num_rows != 0) {
                    $c = $con->query("select * from app_users where (user_name= '$user_name' or mobile='$user_name')
                                                                     and status = 1 and password = '$password' and imei_code = '$imei_code'");
                    $c = $c->fetch_assoc();
                    // $app_user_count = $con->query("SELECT app_users from app_access
                    //                                      where app_code = '$app_code'")->fetch_assoc()['app_users'];
                    $app_user_count = 0;
                    $returnArr = array("data" => $c, "ResponseCode" => "200", 
                                        "Result" => "true", "ResponseMsg" => "Login successfully!", "users_count" => $app_user_count);
    
                } else {
                    $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Invalid Username or Password");
                }
            }
        } else {
            $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Your Account Still not Activated!!");
        }
    } else if($mode == ""){
        
    }
    
  
}

echo json_encode($returnArr);