<?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']);
$parafcmtoken = urldecode($_POST['parafcmtoken']);

function pg_query_safe($con, string $sql)
{
    $res = pg_query($con, $sql);
    if (!$res) {
        throw new RuntimeException('DB Error: ' . pg_last_error($con) . ' | SQL: ' . $sql);
    }
    return $res;
}
//  $paraimeino ='398bcd7a638d12b2'; 
try{
if ($paraimeino !="" && $parafcmtoken !="") {
    
    $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
           $updateQuery = "UPDATE tblbranchdevicemapping SET fcmtoken = $1 WHERE deviceid = $2";
           pg_prepare($con, "updatefcmtoken", $updateQuery);
           pg_execute($con, "updatefcmtoken", [$parafcmtoken, $paraimeino]);
            // success
            $response["success"] = 1;
             $response["message"] = "Updated successfully";
 
            // echoing JSON response
            echo  json_encode($response,JSON_UNESCAPED_UNICODE);


        } else {
            // no user found
            $response["success"] = 0;
            $response["message"] = "No 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();
}
?>
