<?php
require_once("config.class.php");
require_once("mysqldb.class.php");
require_once("time2.php");

#$timeGettersSetters;

$time = new TimeGettersSetters();
$bodyContent = "";
$cust_id=200;


$del_list .=" <select size=\"1\" name=\"Qstat5\"><option  value=1> Project 1</option>";
$del_list .="<option  value=2> Project 2</option>";
$del_list .="<option  value=3> Project 3</option>";
$del_list .="<option  value=4> Project 4</option></select>";

/**
 * Actions
 */

if ($_GET['action'] == "clockIn") {
    $time->clockIn($cust_id);
    header("Location:{$_SERVER['PHP_SELF']}"); exit();
} else if ($_GET['action'] == "clockOut") {
    $time->clockOut($cust_id);
    header("Location:{$_SERVER['PHP_SELF']}"); exit();
}

/**
 * Render page
 */
if ($time->isClockedIn($cust_id)) {
    $title = "IN";
    $action = "clockOut";
    $buttons = "
    <input disabled='disabled' type='submit' value='Clock in' /> <input type='submit' value='Clock out' />";
} else {
    $title = "OUT";
    $action = "clockIn";
    $buttons = " Project Charge Code:    $del_list
    <input type='submit' value='Clock in' /> <input disabled='disabled' type='submit' value='Clock out' />";
}

$timeForm = <<<eof
<form action="{$_SERVER['PHP_SELF']}?action={$action}" method="post">
{$buttons}
</form>
eof;
$timeWorkedToday = $time->convertUnixTimeToHours($time->getTotalTimeForDay(date("Y-m-d"),$cust_id));
$todaysTime = "<p><strong>Today:</strong> " . $time->convertUnixTimeToHours($time->getTotalTimeForDay(date("Y-m-d"),$cust_id)) . " hours (" . $time->convertUnixTimeToMinutes($time->getTotalTimeForDay(date("Y-m-d"),$cust_id)) . " minutes)</p>";
$thisWeeksTime = "<p><strong>This week:</strong> " . $time->convertUnixTimeToHours($time->getTotalTimeForCurrentWeek($cust_id)) . " hours (" . $time->convertUnixTimeToMinutes($time->getTotalTimeForCurrentWeek($cust_id)) . " minutes)</p>";

$allTime = "";
$a_allHoursWorked = $time->getHoursWorkedForEachDay($cust_id);
if ($a_allHoursWorked) {
    $cnt = 0;
    $weekNumber = null;
    $a_weeksTotal = array();
    $arrayCount = count($a_allHoursWorked) - 1;
    foreach ($a_allHoursWorked as $date=>$secondsWorked) {
        $weekNumber = date("W", strtotime($date));
        if ($cnt === 0) {
            # starts a new week total
            $a_weeksTotal[$weekNumber] = $time->getTotalTimeForDay($date,$cust_id);
        } else {
            if ($weekNumber === $previousWeekNumber) {
                # keep adding time to this weeks total
                $a_weeksTotal[$weekNumber] += $time->getTotalTimeForDay($date,$cust_id);
            } else if ($weekNumber !== $previousWeekNumber || $cnt === $arrayCount) {
                $allTime .= "<p class='weekHeading'><strong>Week({$previousWeekNumber}) Total:</strong> " . $time->convertUnixTimetoHours($a_weeksTotal[$previousWeekNumber]) . " hours (" . $time->convertUnixTimeToMinutes($a_weeksTotal[$previousWeekNumber]) . " minutes)</p>";
                # starts a new week total
                $a_weeksTotal[$weekNumber] = $time->getTotalTimeForDay($date,$cust_id);
            }
        }
        $allTime .= "<p><strong>{$date}:</strong> " . $time->convertUnixTimeToHours($secondsWorked) . " hours (" . $time->convertUnixTimeToMinutes($secondsWorked) . " minutes)</p>";
        $previousWeekNumber = $weekNumber;
        # This if block catches the very first week entered.
        if ($weekNumber !== $previousWeekNumber || $cnt === $arrayCount) {
            $allTime .= "<p class='weekHeading'><strong>Week({$previousWeekNumber}) Total:</strong> " . $time->convertUnixTimetoHours($a_weeksTotal[$previousWeekNumber]) . " hours (" . $time->convertUnixTimeToMinutes($a_weeksTotal[$previousWeekNumber]) . " minutes)</p>";
            # starts a new week total
            $a_weeksTotal[$weekNumber] = $time->getTotalTimeForDay($date, $cust_id);
        }

        $cnt++;
    }
}

$bodyContent .= "<h1>Time Tracker</h1>" . $timeForm . "<h3>Recent times worked</h3>" . $todaysTime . $thisWeeksTime . "<h3>All times worked</h3>" . $allTime;

$html = <<<eof
<html>
<head>
<meta http-equiv="refresh" content="60;url={$_SERVER['PHP_SELF']}" />
<title>{$title} $timeWorkedToday hrs - Time</title>
<style type="text/css">
/* CSS goes here */
/* The css variable below is populated via ssi/css.php if you need dynamic css. Throughly test in needed browsers. */

body {
    font-family:sans-serif;
    font-size:100%;
    background-color:#93021C;
    text-align:center;
    }
li {
    margin-bottom:8px;
    }
a, a:link {
    color:maroon;
    }
a:hover {
    color:navy;
    text-decoration:none;
    }
div#container {
    width:500px;
    text-align:left;
    margin-left:auto;
    margin-right:auto;
    margin-top:32px;
    margin-bottom:16px;
    padding:16px;
    background-color:white;
    -moz-border-radius:16px;
    -moz-box-shadow: 0px 0px 16px #000;
    -webkit-box-shadow: 0px 0px 16px #000;
    -o-box-shadow: 0px 0px 16px #000;
    box-shadow: 0px 0px 16px #000;
    }
.mono {
    font-family:monospace;
    }

h1, h2, h3, h4 {
    margin:8px 4px;
    padding:4px 8px;
    background-color:#dfdfdf;
    -moz-border-radius:8px;
    -moz-box-shadow: 0px 0px 4px #000;
    -webkit-box-shadow: 0px 0px 4px #000;
    -o-box-shadow: 0px 0px 4px #000;
    box-shadow: 0px 0px 4px #000;
}

.DISTweekHeading {
    background-color:#eeeeee;
    -moz-border-radius:8px;
    -moz-box-shadow: 0px 0px 4px #000;
    -webkit-box-shadow: 0px 0px 4px #000;
    -o-box-shadow: 0px 0px 4px #000;
    box-shadow: 0px 0px 4px #000;
}
.weekHeading {
    background-color:white;
    -moz-border-radius-bottomleft:8px;
    -moz-border-radius-bottomright:8px;
    -moz-box-shadow: 0px 4px 2px gray;
    -webkit-box-shadow: 0px 4px 2px gray;
    -o-box-shadow: 0px 4px 2px gray;
    box-shadow: 0px 4px 2px gray;
}

form, p {
    margin:4px 4px;
    padding:4px 4px;
}

li {
    list-style-type:none;
}
</style>
</head>
<body>
<div id="container">
{$bodyContent}
</div>
</body>
</html>
eof;

print($html);
