<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$dateto = '';
$timeTotal = array();
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
extract($_REQUEST);
if ($dateto == "") {
    $dateto = date('Y-m-d');
} else {
    $dateto = date("Y-m-d", strtotime($dateto));
}
$dateto = date("d-m-Y", strtotime($dateto));

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
</head>
<body>
<!-- Start Page Loading -->
<!--<div id="loader-wrapper">-->
<!--    <div id="loader"></div>-->
<!--    <div class="loader-section section-left"></div>-->
<!--    <div class="loader-section section-right"></div>-->
<!--</div>-->
<!-- End Page Loading -->
<div id="container">
    <?php include('../includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Daily Attendance Report</h3>
            </div>
            <script>
                $(function () {
                    var date = new Date();
                    var currentMonth = date.getMonth();
                    var currentDate = date.getDate();
                    var currentYear = date.getFullYear();
                    $('#datepicker').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                        selectYears: true,
                        max: new Date(currentYear, currentMonth, currentDate)
                    });
                });
            </script>
            <table class="table">
                <form action="" method="get" name="search"
                      id="searchfrm">
                    <tr>
                        <td class="col s12">Date<br>
                            <input type="text" name="dateto" id="datepicker"
                                   value="<?= $dateto ?>" class="form-control"/>
                            <input type="hidden" name="view" id="view" class="form-control"/>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="col s12">
                            <button type="submit" class="btn waves-effect waves-light"
                                    style="float:left;">
                                Search
                            </button>
                        </td>
                    </tr>
                </form>
            </table>
            <hr><?php
            if (isset($_GET['view'])) {
                ?>
                <div>
                    <?php
                    $filterEmployee = " WHERE status = 'active'";
                    $qry = EmployeeSetup::GetEmployeeSetupList($filterEmployee);
                    if ($qry) {
                        foreach ($qry AS $qery) {
                            echo '<table class="table bordered striped">';
                            $getDate = date('Y-m-d', strtotime($dateto));
                            $getDateNight = date('Y-m-d 19:00:00', strtotime($dateto));
                            $getDateNightPlus = date('Y-m-d 18:59:59', strtotime('+1 day', strtotime($dateto)));
                            $filterAttDay = ' AND STR_TO_DATE(time_in, "%Y-%m-%d") = "' . $getDate . '" AND shift = "day" ';
                            $filterAttNight = ' AND STR_TO_DATE(time_in, "%Y-%m-%d %H:%i:%s") >= "' . $getDateNight . '" AND STR_TO_DATE(time_in, "%Y-%m-%d %H:%i:%s") <= "' . $getDateNightPlus . '" AND shift = "night" ';
                            $present = '';
                            $attendanceEmployee = '';
                            $attendanceDay = '';
                            $attendanceNight = '';
                            $attendanceDay = Attendance::GetAttendanceByEmployeeId($qery->id, $filterAttDay);
                            $attendanceNight = Attendance::GetAttendanceByEmployeeId($qery->id, $filterAttNight);
                            if ($attendanceDay || $attendanceNight) {
                                if (!empty($attendanceDay)) {
                                    echo '<tr style="border: 1px solid #666;">';
                                    echo '<th width="300px">Employee Name <div class="label label-primary" style="float: right; position: relative;"><a href="' . PROJECT_ADMIN_URL . 'report/dailyattendance_ind.php?dateto=' . $dateto . '&emp=' . $qery->id . '&view="><span class="chip light-blue white-text">View</span></a></div></th>';
                                    $totalslots = count($attendanceDay);
                                    for ($c = 1; $c <= $totalslots; $c++) {
                                        if ($c % 2 == 0) {
                                            echo '<th>Time Out</th>';
                                        } else {
                                            echo '<th>Time In</th>';
                                        }
                                    }
                                    echo '<th>Shift</th>';
                                    echo '<th>Work Hours</th>';
                                    echo '</tr><tr style="border: 1px solid #666;">';
                                    echo '<td>' . $qery->name . '</td>';

                                    $count = 0;
                                    $counter = 0;
                                    $enddatetimes = '';
                                    foreach ($attendanceDay AS $attendanceRow) {
                                        if ($attendanceRow->shift == 'day') {
                                            echo '<td>';
                                            echo $attendanceRow->time_in != '0000-00-00 00:00:00' ? date('Y-m-d h:i A', strtotime($attendanceRow->time_in)) : '';
                                            echo '</td>';
                                            $counter++;
                                            if ($counter % 2 == 0) {
                                                $timeEnd = '';
                                                $timeEnd = $attendanceRow->time_in;

                                                $diff = abs(strtotime($timeEnd) - strtotime($timeStart));

                                                $years = floor($diff / (365 * 60 * 60 * 24));
                                                $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
                                                $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

                                                $hours = intval(($diff % 86400) / 3600);
                                                $minutes = intval(($diff / 60) % 60);
                                                $seconds = intval($diff % 60);

                                                //$interval = date_diff($timeEnd, $timeStart);
                                                //abs( strtotime( $timeEnd ) - strtotime( $timeStart ) );

                                                $timeTotal[] = date('H:i:s', strtotime($hours . ':' . $minutes . ':' . $seconds));
                                                //$asdasdsad = date('H:i:s', strtotime($hours.':'.$minutes.':'.$seconds));
                                                //echo strtotime($asdasdsad).'<br>';
                                                //echo '<td>'.$hours.' : '.$minutes.' : '.$seconds.'</td>';
                                            } else {
                                                $timeStart = $attendanceRow->time_in;
                                            }
                                        }
                                        $count++;
                                    }
                                    if ($timeTotal) {
                                        $enddatetimes = '';
                                        $enddatetime = '';
                                        foreach ($timeTotal AS $timeTotalRow) {
                                            $enddatetime = strtotime($timeTotalRow) + strtotime($enddatetime) - strtotime('00:00:00');
                                            $enddatetimes = intval($enddatetime);
                                        }
                                    }
                                    $enddatetimes = $enddatetimes + strtotime('00:00:00');
                                    //print_r($timeTotal);
                                    $timeTotal = array();
                                    echo '<td>' . ucwords($attendanceRow->shift) . '</td>';
                                    echo '<td>' . date('H:i:s', $enddatetimes) . '</td>';
                                } else if (!empty($attendanceNight)) {
                                    echo '<tr style="border: 1px solid #666;">';
                                    echo '<th width="300px">Employee Name <div class="label label-primary" style="float: right; position: relative;"><a href="' . PROJECT_ADMIN_URL . 'report/dailyattendance_ind.php?dateto=' . $dateto . '&emp=' . $qery->id . '&view="><span class="chip light-blue white-text">View</span></a></div></th>';
                                    $totalslots = count($attendanceNight);
                                    for ($c = 1; $c <= $totalslots; $c++) {
                                        if ($c % 2 == 0) {
                                            echo '<th>Time Out</th>';
                                        } else {
                                            echo '<th>Time In</th>';
                                        }
                                    }
                                    echo '<th>Shift</th>';
                                    echo '<th>Work Hours</th>';
                                    echo '</tr><tr style="border: 1px solid #666;">';
                                    echo '<td>' . $qery->name . '</td>';

                                    $count = 0;
                                    $counter = 0;
                                    $enddatetimes = '';
                                    foreach ($attendanceNight AS $attendanceRow) {
                                        if ($attendanceRow->shift == 'night') {
                                            $nightstarttime = date('Y-m-d 19:00:00', strtotime($dateto));
                                            $nightendtime = date('Y-m-d 18:59:00', strtotime('+1 day', strtotime($dateto)));
                                            //echo $nightstarttime."<br>";
                                            if ($attendanceRow->time_in >= $nightstarttime && $attendanceRow->time_in <= $nightendtime) {
                                                echo '<td>';
                                                echo $attendanceRow->time_in != '0000-00-00 00:00:00' ? date('Y-m-d h:i A', strtotime($attendanceRow->time_in)) : '';
                                                echo '</td>';
                                                $counter++;
                                                if ($counter % 2 == 0) {
                                                    $timeEnd = '';
                                                    $timeEnd = $attendanceRow->time_in;

                                                    $diff = abs(strtotime($timeEnd) - strtotime($timeStart));

                                                    $years = floor($diff / (365 * 60 * 60 * 24));
                                                    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
                                                    $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

                                                    $hours = intval(($diff % 86400) / 3600);
                                                    $minutes = intval(($diff / 60) % 60);
                                                    $seconds = intval($diff % 60);

                                                    //$interval = date_diff($timeEnd, $timeStart);
                                                    //abs( strtotime( $timeEnd ) - strtotime( $timeStart ) );

                                                    $timeTotal[] = date('H:i:s', strtotime($hours . ':' . $minutes . ':' . $seconds));
                                                    //$asdasdsad = date('H:i:s', strtotime($hours.':'.$minutes.':'.$seconds));
                                                    //echo strtotime($asdasdsad).'<br>';
                                                    //echo '<td>'.$hours.' : '.$minutes.' : '.$seconds.'</td>';
                                                } else {
                                                    $timeStart = $attendanceRow->time_in;
                                                }
                                            }
                                            //echo '<td>';
                                            //echo $attendanceRow->time_in != '0000-00-00 00:00:00' ? date('Y-m-d h:i A', strtotime($attendanceRow->time_in)) : '';
                                            //echo '</td>';
                                        }
                                        $count++;
                                    }
                                    if ($timeTotal) {
                                        $enddatetimes = '';
                                        $enddatetime = '';
                                        foreach ($timeTotal AS $timeTotalRow) {
                                            $enddatetime = strtotime($timeTotalRow) + strtotime($enddatetime) - strtotime('00:00:00');
                                            $enddatetimes = intval($enddatetime);
                                        }
                                    }
                                    $enddatetimes = $enddatetimes + strtotime('00:00:00');
                                    //print_r($timeTotal);
                                    $timeTotal = array();
                                    echo '<td>' . ucwords($attendanceRow->shift) . '</td>';
                                    echo '<td>' . date('H:i:s', $enddatetimes) . '</td>';
                                }
                                echo '</tr>';
                            } else {
                                echo '<tr style="border: 1px solid #666;">';
                                echo '<th width="300px">Employee Name <div class="label label-primary" style="float: right; position: relative;"><a href="' . PROJECT_ADMIN_URL . 'report/dailyattendance_ind.php?dateto=' . $dateto . '&emp=' . $qery->id . '&view="><span class="chip light-blue white-text">View</span></a></div></th>';
                                echo '<th>Status</th>';
                                echo '</tr><tr style="border: 1px solid #666;">';
                                echo '<td>' . $qery->name . '</td>';
                                echo '<td>Absent</td></tr>';
                            }

                            echo '</table>';
                            echo '<br>';
                            //echo '</section></div>';
                        }
                    }
                    ?>
                </div>
                <?php

            } // close view
            ?>
        </div>
    </div>
</div>
</div>
<?php include('../includes/inc_footer.php'); ?>
<script type="text/javascript" src="<?= PROJECT_URL ?>js/download.js"></script>
<?php include('../includes/inc_js.php'); ?>
<style>
    @page {
        size: A4;
        margin: 5mm 0mm;
    }

    @media print {
        table {
            page-break-inside: auto
        }

        tr {
            page-break-inside: avoid;
            page-break-after: auto
        }

        #container {
            page-break-after: always;
        }

        header, aside, form, footer {
            display: none;
        }

        .adv-table th, .adv-table td {
            padding: 5px;
        }

        h2 {
            font-size: 18px;
            text-align: center;
        }

        hr {
            margin-top: 5px;
            margin-bottom: 5px;
        }
    }
</style>
</body>
</html>