<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "baseid")
    $lo_id = $paramValue;
  if ($paramName == "date")
    $date = $paramValue;
}

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

$dal = new DAL;

$sql = "SELECT id FROM workday WHERE date = '$date' LIMIT 1";
$wo_id = $dal->query($sql)->id;

if (null === $wo_id) {
  $wo_id = hexdec(uniqid());

  $sql = "INSERT INTO workday (id, date, textdate) VALUES ($wo_id, '$date', '$textDate')";
  $result_id = $dal->query($sql);
}

$sql = "SELECT id FROM loginworkday WHERE workday_id = $wo_id AND login_id = $lo_id LIMIT 1";
$lw_id = $dal->query($sql)->id;

if (null === $lw_id) {
  $lw_id = hexdec(uniqid());

  $sql = "INSERT INTO loginworkday " .
         "(id, workday_id, login_id) " . 
         "VALUES ($lw_id, '$wo_id', $lo_id) " ;
 $result = $dal->query($sql);
}

echo json_encode ($lo_id);

?>
