<?php
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
$app_name = $data['app_name'];
$app_code = $data['app_code'];
$uid = $data['uid'];

if ($app_name == '' && $app_code='') {
	$returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went wrong try again !");
} else {
    if($uid != '0') {
        $status = $con->query("select * from app_users where status = 1 and id ='$uid'");
    } else {
        $status = $con->query("select * from app_users where status = 1 and id ='1'");
    }
    
    if ($status->num_rows != 0) {
        $app_access_query = $con->query("SELECT * from app_access where app_name = '$app_name' and app_code = '$app_code'");
        
        if($uid != '0') {
            $user_access_query = $con->query("SELECT app_users.type as t from app_users where id = '$uid' and status='1'");
            $user_count = $user_access_query->num_rows;
            if($user_count == 1){
                $user_type = $user_access_query->fetch_assoc()['t'];
            } else {
                $user_type = 0;
            }
        } else {
            $user_type = 0;
        }

        if($row = $app_access_query->fetch_assoc()) {
            $app_url = $row['latest_app_url'];
    	    $app_version = $row['app_version'];
    	    $app_users = $row['app_users'];
    	    $app_status = $row['status'];
            
    		$returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Success", 
                                "app_version"=>$app_version,"app_url"=>$app_url,"app_users"=>$app_users,
                                "app_status"=>$app_status,"user_type"=>$user_type);
        } else {
    		$returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Access has been denied");
        }
    } else {
        $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Your Account Deactivated!!");
    }

}

echo json_encode($returnArr);
mysqli_close($con);
?>