<?php
session_start();

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid")
    $ow_id = $paramValue;
  else if ($paramName == "set")
    $set = $paramValue;
  else if ($paramName == "time")
    $time = $paramValue;
}

date_default_timezone_set("Canada/Eastern");

$valid = 1;
$message = "OK";

$buttons = ["start", "pause", "resume", "stop"];
if (in_array($set, $buttons)) {
  $hours = date("H");
  $minutes = date("i");
  $m = ((($minutes + 7.5)/15 | 0) * 15) % 60;
  $h = (((($minutes/105) + .5) | 0) + $hours) % 24;
  $now = sprintf("%02d", $h) . ':' . sprintf("%02d", $m);
}

$dal = new DAL();

if ($set == 'start') {
  $sql = "UPDATE officeworkrole SET startime='$now' " .
         "WHERE id = $ow_id ";
  $result = $dal->query($sql);
  $time = $now;
}

else if ($set == 'stop') {
  $sql = "SELECT startime, breakdur FROM officeworkrole " .
         "WHERE id = $ow_id LIMIT 1 ";
  $of = $dal->query($sql);
  
  $dur = (substr ($now, 0,2) + substr ($now, 3,2)/60)
         - (substr ($of->startime, 0,2) + substr ($of->startime, 3,2)/60)
         - $of->breakdur;

  $duration = sprintf ("%01.2f", $dur);
  
  if ($duration < 0) {
    $duration = 0;
    $valid = 0;
    $message = "Stop value causes negative duration";
  }    
    
  $sql = "UPDATE officeworkrole SET stoptime='$now', duration=$duration " .
           "WHERE id = $ow_id ";
  $result = $dal->query($sql);
  $time = $now;
}

else if ($set == 'pause') {

  $sql = "UPDATE officeworkrole " .
         "SET pausetime='$now' WHERE id = $ow_id ";
  $result = $dal->query($sql);
  $time = $now;
}

else if ($set == 'resume') {

  $sql = "SELECT pausetime, breakdur FROM officeworkrole " .
         "WHERE id = $ow_id LIMIT 1 ";
  $of = $dal->query($sql);
  
  $break = (substr ($now, 0,2) + substr ($now, 3,2)/60)
    - (substr ($of->pausetime, 0,2) + substr ($of->pausetime, 3,2)/60)
    + $of->breakdur;
    
  $breakdur = sprintf ("%01.2f", $break);

  if ($breakdur < 0) {
    $breakdur = 0;
    $valid = 0;
    $message = "Resume value causes negative duration";
  }

  $sql = "UPDATE officeworkrole SET breakdur='$breakdur' " .
         "WHERE id = $ow_id ";
  $result = $dal->query($sql);
  $time = $breakdur;
}

else if ($time == "00:00") {

  $sql = "SELECT $set FROM officeworkrole " .
         "WHERE id = $ow_id LIMIT 1 ";
  $time = $dal->query($sql)->$set;
}

else if ($set == 'duration') {

  $sql = "UPDATE officeworkrole SET startime='00:00', breakdur=0.00, " .
         "pausetime='00:00', stoptime='00:00', duration=$time " .
         "WHERE id = $ow_id ";
  $result = $dal->query($sql);
}

else if ($set == 'startime') {
  $sql = "SELECT breakdur, stoptime FROM officeworkrole " .
         "WHERE id = $ow_id LIMIT 1 ";
  $of = $dal->query($sql);
  
  $dur = (substr ($of->stoptime, 0,2) + substr ($of->stoptime, 3,2)/60)
         - (substr ($time, 0,2) + substr ($time, 3,2)/60)
         - $of->breakdur;

  $duration = sprintf ("%01.2f", $dur);

  if ($duration < 0) {
    $duration = 0;
    $valid = 0;
    $message = "Start value causes negative duration";
  }

  $sql = "UPDATE officeworkrole SET startime='$time', duration=$duration " .
         "WHERE id = $ow_id ";
  $result = $dal->query($sql);
}

else if ($set == 'breakdur') {
  $sql = "SELECT startime, stoptime FROM officeworkrole " .
         "WHERE id = $ow_id LIMIT 1 ";
  $of = $dal->query($sql);
  
  $dur = (substr ($of->stoptime, 0,2) + substr ($of->stoptime, 3,2)/60)
         - (substr ($of->startime, 0,2) + substr ($of->startime, 3,2)/60)
         - $time;

  $duration = sprintf ("%01.2f", $dur);

  if ($duration < 0) {
    $duration = 0;
    $valid = 0;
    $message = "Break value causes negative duration";
  }
  
  $sql = "UPDATE officeworkrole SET breakdur='$time', duration=$duration " .
         "WHERE id = $ow_id ";
  $result = $dal->query($sql);
//print_r ("result:$result");  
//  $time = $duration;
}

else if ($set == 'stoptime') {
  $sql = "SELECT startime, breakdur FROM officeworkrole " .
         "WHERE id = $ow_id LIMIT 1 ";
  $of = $dal->query($sql);
  
  $dur = (substr ($time, 0,2) + substr ($time, 3,2)/60)
         - (substr ($of->startime, 0,2) + substr ($of->startime, 3,2)/60)
         - $of->breakdur;

  $duration = sprintf ("%01.2f", $dur);

  if ($duration < 0) {
    $duration = 0;
    $valid = 0;
    $message = "Stop value causes negative duration";
  }
  
  $sql = "UPDATE officeworkrole SET stoptime='$time', duration=$duration " .
         "WHERE id = $ow_id ";
  $result = $dal->query($sql);
}

$return = [$time, $valid, $message];

echo json_encode($return);

?>

