<?php
    include_once 'opendb.php';
     $response["success"] = -1;
     $response["message"] = "Error";
     $response["client_code"] ="";
     $response["req_no"]=-1;
     $response["client_type"] =-1;
     
     $CompID=0;
     if (isset($_POST["comp_id"]))
    {
         $CompID=$_POST["comp_id"]; 
    }
    if($CompID==0) {
         $response["message"] = "Error: Web Co ID invalid";
            // echo no users JSON
            die( json_encode($response));
    }
    if (isset($_POST["AppID"]))
    {
	
        $AppID=$_POST["AppID"] ;
        $ReqType=$_POST["ReqType"] ;
        $Notes=$_POST["Notes"] ;
        $ReqIndex=$_POST["ReqIndex"] ;
        $Request_time=$_POST["Request_time"];
        $Cust_Code=$_POST["Cust_Code"] ;
        $client_Code=$_POST["client_Code"] ;
        $sql = "SELECT counter_code,next_so_no,inactive,client_type FROM mobile_clients WHERE client_app_id like '".$AppID."' and comp_id=" .$CompID;
        $result = $conn->query($sql);
        if ($result->num_rows > 0 ) {
                if($row = $result->fetch_assoc()) 
                {
                    if ($row["inactive"]==0)
                    {
                       $response["success"] = 1;
                       $response["client_code"] = $row["counter_code"];
                       $client_Code=$row["counter_code"];
                       $response["message"] = "active";
                       $response["client_type"] = $row["client_type"];
                        $sql = "INSERT INTO client_requests (client_app_id,Notes,request_type,request_index,request_time,Cust_Code,Counter_code,comp_id) " .
                                                   " VALUES ('".$AppID."','".$Notes."','". $ReqType."',". $ReqIndex.",'".$Request_time."','".$Cust_Code."','".$client_Code."',".$CompID.")";
                        
                            if ($conn->query($sql) === TRUE)
                            {
                               $Eno = $conn->insert_id;
                               $response["req_no"] = $Eno;

                            } else
                            {
                               die ( "Error in insert Req :" . $sql . "<br>" . $conn->error);
                            } 

                     }
                     else
                     {
                        $response["success"] = 0;
                        $response["client_code"] = $row["counter_code"];
                        $response["message"] = "inactive";
                     }
                 }

        }
        else {
                  $response["success"] = -1;
                  $response["message"] = "client Not registered..!  : ".$AppID;
        }
    }
    else
    {
        $response["message"] = "No Data for app id";
    }
    
    $conn->close();
    echo json_encode($response);
?>