<?php
 
/*
 * Following code will list all the city
 */
 
// array for JSON response
$response = array();
 
 require_once __DIR__ . '/catchexception.php';
// include db connect class
require_once __DIR__ . '/db_config.php';
  
 
$paraimeino = urldecode($_POST['paraimeino']);
//  $paraimeino ='398bcd7a638d12b2'; 
try{
if ($paraimeino !="" ) {
    
    // get a product from products table
   /* $paracountqry="SELECT count(*) as countval from tblsyncdetails where imeino=$1 and synctype='branchdevicemapping';";
  $resultmax=  pg_prepare($con,"paracountqry",$paracountqry);
 $data = [$paraimeino];
 $res = pg_execute($con,"paracountqry",$data);
  if (!empty($res)) {
    if (pg_num_rows($res) > 0) {   
      while ($row = pg_fetch_assoc($res)) {
          $countval= $row["countval"] ;
        }
      }
    }

    if ($countval > 0){
      $paravanquery="SELECT *,(select branchname from tblbranchmaster where branchcode=dev.branchcode) as branchname from (select *,'Insert' as  process  from tblbranchdevicemapping where (select syncdate from tblsyncdetails where imeino=$1 and synctype='branchdevicemapping' order by syncdate desc limit 1 ) <= createddate and statuscode='1'
      union all  select *,'Update' as process from tblbranchdevicemapping where (select syncdate from tblsyncdetails where imeino=$2 and synctype='branchdevicemapping' order by syncdate desc limit 1 ) <=updateddate  
      ) as dev;";
      $resultmax=  pg_prepare($con,"paravanquery",$paravanquery);
      $data = [$paraimeino,$paraimeino];
      $result = pg_execute($con,"paravanquery",$data);
    }
    else{
      $paravanquery="SELECT *,'Insert' as  process,(select branchname from tblbranchmaster where branchcode=a.branchcode) as branchname  from tblbranchdevicemapping as a where  statuscode='1' and deviceid=$1";
      $resultmax=  pg_prepare($con,"paravanquery",$paravanquery);
      $data = [$paraimeino];
      $result = pg_execute($con,"paravanquery",$data);
    } */
    $paravanquery="SELECT *,'Insert' as  process,(select branchname from tblbranchmaster where branchcode=a.branchcode) as branchname  from tblbranchdevicemapping as a where  statuscode='1' and deviceid=$1";
      $resultmax=  pg_prepare($con,"paravanquery",$paravanquery);
      $data = [$paraimeino];
      $result = pg_execute($con,"paravanquery",$data);
    if (!empty($result)) {
        // check for empty result
        if (pg_num_rows($result) > 0) {
      // user node
            $response["Value"] = array();
 
            while ($row = pg_fetch_assoc($result)) {        
              $report = array();
               
                $report["autonum"] = $row["autonum"];
                $report["branchcode"] = $row["branchcode"];
                $report["process"] = $row["process"];
                $report["deviceid"] = $row["deviceid"];
                $report["statuscode"] = $row["statuscode"];
                $report["createddate"] = $row["createddate"];
                $report["updateddate"] = $row["updateddate"];
                $report["makerid"] = $row["makerid"]; 
                $report["branch_devicemapcode"] = $row["branch_devicemapcode"]; 
                $report["passcode"] = $row["passcode"]; 
                $report["branchname"] = $row["branchname"]; 
              // push single product into final response array
              array_push($response["Value"], $report);
            }
            // success
            $response["success"] = 1;
 
            // echoing JSON response
            echo  json_encode($response,JSON_UNESCAPED_UNICODE);


        } else {
            // no user found
            $response["success"] = 0;
            $response["message"] = "No Report found";
 
            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no user found
        $response["success"] = 0;
        $response["message"] = "No found";
 
        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
     echo  json_encode($response,JSON_UNESCAPED_UNICODE);
  }
  mch_db_release($con);
}
//catch exception
catch(Exception $e) {
mch_db_release($con);
echo 'Message: ' .$e->getMessage();
}
?>
