<?php
// array for JSON response
$response = array();

//check for required fields
if ( isset($_POST['ClientID']) ) {
		
		$VehicleID = $_POST['VehicleID'];
  		$VehicleGroupID = $_POST['VehicleGroupID'];
		$VehicleName = $_POST['VehicleName'];
  		$Brand = $_POST['Brand'];
		$Model = $_POST['Model'];
  		$Colour = $_POST['Colour'];
		$RegistrationNo = $_POST['RegistrationNo'];
  		$Account3ID = $_POST['Account3ID'];
		$Status = $_POST['Status'];
  		$Lng = $_POST['Lng'];
		$Lat = $_POST['Lat'];
  		$ContactNo = $_POST['ContactNo'];
		$SerialNo = $_POST['SerialNo'];
  		$UpdatedDate = $_POST['UpdatedDate'];
  		$ClientID = $_POST['ClientID'];
  		$ClientUserID = $_POST['ClientUserID'];
  		$NetCode = $_POST['NetCode'];
  		$SysCode = $_POST['SysCode'];

      // include db connect class
      require_once __DIR__ . '/../DBConnect.php';
               $parm=array();
	           $query="select max(VehicleID) as maxid from Vehicle2Name where ClientID =".$ClientID;
			   
	              try {
        // $result=$conn->query($sql);
        $stmt = $conn->prepare($query);
        
        // execute the query
        $stmt->execute();
        $users = $stmt->fetchAll();
		$VehicleID = 0;
        foreach ($users as $user) {
          
			$VehicleID = $user['maxid'] + 1;
        }
        // echo a message to say the UPDATE succeeded
       // $result = $stmt->rowCount(); // . " records UPDATED successfully";
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
	                  
			    $UpdatedDate = date("Y-m-d H:i:s");
          $sql = "INSERT INTO Vehicle2Name (VehicleID, VehicleGroupID, VehicleName, Brand, Model, Colour, RegistrationNo, Account3ID, Status, Lng, Lat, ContactNo, SerialNo, ClientID, ClientUserID, UpdatedDate, NetCode, SysCode) VALUES ('$VehicleID', '$VehicleGroupID', '$VehicleName', '$Brand', '$Model', '$Colour', '$RegistrationNo', '$Account3ID', '$Status', '$Lng', '$Lat', '$ContactNo', '$SerialNo', '$ClientID', '$ClientUserID', '$UpdatedDate', '$NetCode', '0');";
      		
      		try {
		   $result= $conn->exec($sql);		   
				if( $result === false ) {
				/////////////not inserted
					$response["success"] = 0;  
					$response["message"] = "User Not Register";
				echo json_encode($response);
				}else{
					/////////////inserted
				$response["success"] = 1;
				$response["VehicleID"] = $VehicleID;
				$response["UpdatedDate"] = $UpdatedDate;
				$response["message"] = "User Register";
				echo json_encode($response);
				}
			}
		catch (PDOException $e) {
			echo $sql . "<br>" . $e->getMessage();
		}
		 
		 
        } else{
      // success
      $response["success"] = 0;
      $response["message"] = "Send All Required Fields ";
      // echoing JSON response
      echo json_encode($response);
    }
?>
