<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "fromto")
    $fromTo = explode (",", $paramValue);
}

$wd_id = array_pop ($fromTo);
$to    = array_pop ($fromTo);
$from  = array_pop ($fromTo);

$dal = new DAL();
  
for ($i=0; $i<count($fromTo); $i++) {
  $rowid = $fromTo[$i];

  if ($from == "leftmulMultiSelect3" && $to == "middlemulMultiSelect3") {
    $sql = "INSERT INTO shiftcrew " .
           "(personnel_id, workday_id) " .
           "VALUES ($rowid, $wd_id) " ;
    $result = $dal->query($sql);
    $refresh = "110";
  }       

  else if ($from == "middlemulMultiSelect3" && $to == "leftmulMultiSelect3") {
//print_r ("$wd_id, $to, $from, $rowid");
    $sql = "DELETE FROM shiftcrew " .
           "WHERE id = $rowid ";
    $result = $dal->query($sql);
    $refresh = "110";
  }
    
  else if ($from == "middlemulMultiSelect3" && $to == "rightmulMultiSelect3") {
    $sql = "UPDATE shiftcrew " .
           "SET supervisor = 1 " .
           "WHERE id = $rowid " ;
    $result = $dal->query($sql);
    $refresh = "011";
  }
    
  else if ($from == "rightmulMultiSelect3" && $to == "middlemulMultiSelect3") {
    $sql = "UPDATE shiftcrew " .
           "SET supervisor = 0 " .
           "WHERE id = $rowid " ;
    $result = $dal->query($sql);
    $refresh = "011";
  }
}

echo json_encode($refresh);

?>

