<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid") 
    $ve_id = $paramValue;  
  else if ($paramName == "rowids") 
    $rowIds = explode (",", $paramValue);  
  else if ($paramName == "chkids") 
    $chkIds = explode (",", $paramValue);  
}

$dal = new DAL();

for ($i=0; $i<count($rowIds); $i++) {
  $wo_id = $rowIds[$i];    

  if ($chkIds[$i] == 0) {
    $sql = "DELETE FROM vehiclewash " .
           "WHERE vehicle_id = $ve_id AND washoption_id = $wo_id ";
    $result = $dal->query($sql)->id;
  }

  else {
    $sql = "SELECT id FROM vehiclewash " .
           "WHERE vehicle_id = $ve_id AND washoption_id = $wo_id ";
    $result = $dal->query($sql);

    if (count ($result) == 0) {
      $sql = "INSERT INTO vehiclewash (vehicle_id, washoption_id) " .
             "VALUES ($ve_id, $wo_id) " ;
      $result = $dal->query($sql);
    }  
  }
}
    
echo json_encode($ve_id);

?>

