<?php
session_start();
$lo_id = $_SESSION['login'];

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id")
    $ow_id = $paramValue;
  elseif ($paramName == "workday_id")
    $wo_id = $paramValue;
  elseif ($paramName == "absent")
    $absent = $paramValue;
  elseif ($paramName == "projectparcel_id")
    $projectparcel_id = $paramValue;
}

$textDate = date('D M j, Y', strtotime($date));

$dal = new DAL;

// Get the personnel id for the login id
//$sql = "SELECT personnel_id FROM login WHERE id = $lo_id LIMIT 1";
//$pe_id = $dal->query($sql)->personnel_id;

if ($ow_id == 0) {

  $ow_id = hexdec(uniqid());

  if ($absent == 0) {
    $sql = "INSERT INTO officeworkrole " .
           "(id, workday_id, login_id, projectparcel_id) " . 
           "VALUES ($ow_id, $wo_id, $lo_id, $projectparcel_id) " ;
  }
  else {
    $sql = "INSERT INTO officeworkrole " .
           "(id, workday_id, login_id, projectparcel_id, teamrole_id, startime, " .
           "stoptime, breakdur, pausetime, duration, billable, billrationale) " . 
           "VALUES ($ow_id, $wo_id, $lo_id, $projectparcel_id, 7,  " .
           "'08:30', '16:30', '00.00', '00:00', '08.00', 0, 'Absent') ";
  }
  $result = $dal->query($sql);
}

else {
    
  $sql = "UPDATE officeworkrole SET projectparcel_id=$projectparcel_id " .
         "WHERE id = $ow_id "; 
  $result = $dal->query($sql);
}

echo json_encode ($ow_id);

?>
