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

$qryDate = date("Y-m-d", strtotime($datefrom));
$qryTo = date("Y-m-d", strtotime($dateto));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <style>
        .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
            border: none;
        }
    </style>
</head>
<body>
<section id="container">
    <?php include('../includes/inc_top.php'); ?>
    <section id="main-content">
        <section class="wrapper">
            <div class="panel">
                <div class="panel-body">
                    <div class="">
                        <div class="span12">
                            <div class="">
                                <h2>Attendance Report</h2>
                            </div>
                            <script>
                                $(function () {
                                    $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
                                    $("#datepicker1").datepicker({dateFormat: 'yy-mm-dd'});
                                });
                            </script>
                            <table class="table">
                                <form action="" method="get" name="search"
                                      id="searchfrm">
                                    <tr>
                                        <td class="col-lg-4">Month<br>
                                            <select class="form-control" name="month" id="month">
                                                <?php
                                                if ($month) {
                                                    $monthcount = $month;
                                                } else {
                                                    $monthcount = date('F');
                                                }
                                                for ($m = 1; $m <= 12; ++$m) {
                                                    echo '<option value="' . date('F', mktime(0, 0, 0, $m, 1)) . '"';
                                                    if (date('F', mktime(0, 0, 0, $m, 1)) == trim($monthcount)) {
                                                        echo ' selected';
                                                        $monthCheck = $m;
                                                    }
                                                    echo '>' . date('F', mktime(0, 0, 0, $m, 1)) . '</option>';
                                                }
                                                ?>
                                            </select>
                                            <input type="hidden" name="view" id="view" class="form-control"/>
                                        </td>
                                        <td class="col-lg-4">Year<br>
                                            <select class="form-control" name="year" id="year">
                                                <?php
                                                if ($year) {
                                                    $currentYear = $year;
                                                } else {
                                                    $currentYear = date('Y');
                                                }
                                                for ($i = 1; $i <= 10; $i++) {
                                                    $yearcount = 2016 + $i;
                                                    echo '<option value="' . $yearcount . '"';
                                                    if ($currentYear == $yearcount) {
                                                        echo ' selected';
                                                    }
                                                    echo '>' . $yearcount . '</option>';
                                                }
                                                ?>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" class="col-lg-4">
                                            <button type="submit" class="btn btn-primary btn-large"
                                                    style="float:left;">
                                                Search
                                            </button>
                                            <?php if (isset($_GET['view'])) { ?>
                                            <button
                                                onclick="exportTableToCSV('payment-<?= date('d-m-Y-h-i-a') ?>.csv')"
                                                class="btn btn-primary btn-large"
                                                style="float:left; margin-left: 15px;">
                                                Export CSV
                                            </button>

                        </div>

                        <?php } ?>
                        </td>
                        </tr>
                        </form>
                        <tr>
                            <td colspan="2" class="col-lg-4">
                                <a style="margin-left:30px; display: block; cursor:pointer;font-size: 10px;padding: 1px;"
                                   data-toggle="modal"
                                   href="#myModalPaid"
                                   title="Download">
                                    <button
                                        class="pull-right btn btn-primary btn-large"
                                        style=" margin-left: 15px;">
                                        Download
                                    </button>
                                </a>
                                <a style="margin-left:30px; display: block; cursor:pointer;font-size: 10px;padding: 1px;"
                                   data-toggle="modal"
                                   href="#myModal"
                                   title="Import">
                                    <button
                                        class="pull-right btn btn-primary btn-large"
                                        style=" margin-left: 15px;">
                                        Import
                                    </button>
                                </a>

                                <!-- Modal -->
                                <div id="myModalPaid" class="modal fade"
                                     role="dialog">
                                    <div class="modal-dialog">
                                        <form action="http://192.168.0.20/form/Download" method="post" name="mainform">
                                            <!-- Modal content-->
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close"
                                                            data-dismiss="modal">&times;</button>
                                                    <h4 class="modal-title">Download Data From Device</h4>
                                                </div>
                                                <div class="modal-body">
                                                    <p>Please select the period to import data</p>
                                                                <span class="col-lg-4">Date From<br>
                                                                    <input type="text" name="sdate" id="datepicker"
                                                                           placeholder="Date From"
                                                                           value="<?= $datefrom ?>"
                                                                           class="form-control"/>
                                                                </span>
                                                                <span class="col-lg-4"> Date To<br>
                                                                    <input type="text" name="edate" id="datepicker1"
                                                                           placeholder="Date To"
                                                                           value="<?= $dateto ?>" class="form-control"/>
                                                                </span>
                                                    <?php
                                                    $maxEmp = EmployeeSetup::GetMaxMachineIdFromEmployee();
                                                    for ($a = 1; $a <= $maxEmp->machineid; $a++) {
                                                        echo '<input type="hidden" checked="" name="uid" value="' . $a . '">';
                                                    }
                                                    ?>
                                                    <br><br><br>
                                                </div>

                                                <div class="modal-footer">
                                                    <input type="submit" class="btn btn-danger" name="submit"
                                                           id="submit" value="Download"/>
                                                    <button type="button"
                                                            class="btn btn-danger"
                                                            data-dismiss="modal">Close
                                                    </button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>

                                <!-- Modal -->
                                <div id="myModal" class="modal fade"
                                     role="dialog">
                                    <div class="modal-dialog">
                                        <form action="test.php" method="post" name="testform"
                                              enctype="multipart/form-data">
                                            <!-- Modal content-->
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close"
                                                            data-dismiss="modal">&times;</button>
                                                    <h4 class="modal-title">Import Data</h4>
                                                </div>
                                                <div class="modal-body">
                                                    <p>Please select file to import</p>
                                                                <span class="col-lg-4">Import File<br>
                                                                    <input class="form-control" type="file"
                                                                           name="importfile"
                                                                           id="importfile"/>
                                                                </span>
                                                    <br><br><br>
                                                </div>

                                                <div class="modal-footer">
                                                    <input type="submit" class="btn btn-danger" name="submit"
                                                           id="submit" value="Download"/>
                                                    <button type="button"
                                                            class="btn btn-danger"
                                                            data-dismiss="modal">Close
                                                    </button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        </table>
                        <hr><?php
                        if (isset($_GET['view'])) {
                            ?>
                            <div>
                                <table style="text-align:center;" class="display table table-bordered table-striped">
                                    <tbody>
                                    <tr style="border: 1px solid #666;">
                                        <th>Employee</th>
                                        <?php
                                        if ($year) {
                                            $year = $year;
                                        } else {
                                            $year = date('Y');
                                        }
                                        for ($d = 1; $d <= 31; $d++) {
                                            $time = mktime(12, 0, 0, $monthCheck, $d, $year);
                                            if (date('m', $time) == $monthCheck)
                                                echo '<th>' . date('d', $time) . '</th>';
                                        }
                                        ?>
                                        <th>Late</th>
                                        <th>Absent</th>
                                        <th>AL/CL</th>
                                        <th>Holiday</th>
                                    </tr>
                                    <?php
                                    $getYearMonth = date('Y-m-00', strtotime($year . '-' . $monthCheck . '-01'));
                                    $holidayFilter = ' WHERE STR_TO_DATE(ac_date, "%Y-%m") = "' . $getYearMonth . '"';
                                    $holidays = HolidaySetup::GetHolidaySetupList($holidayFilter);

                                    $filterEmployee = " WHERE status = 'active'";
                                    $qry = EmployeeSetup::GetEmployeeSetupList($filterEmployee);
                                    if ($qry) {
                                        foreach ($qry AS $qery) {
                                            $lateCount = 0;
                                            $absentCount = 0;
                                            $holidayCount = 0;
                                            echo '<tr>';
                                            echo '<td>' . $qery->name . '</td>';
                                            for ($d = 1; $d <= 31; $d++) {
                                                $getDate = date('Y-m-d', strtotime($year . '-' . $monthCheck . '-' . $d));
                                                $background = '';
                                                if ($holidays) {
                                                    foreach ($holidays AS $holidaysRow) {
                                                        if ($getDate == $holidaysRow->ac_date) {
                                                            $background = '#FFA500';
                                                            $holidayCount++;
                                                        }
                                                    }
                                                }
                                                $filterAtt = ' AND (STR_TO_DATE(time_in, "%Y-%m-%d") = "' . $getDate . '" OR STR_TO_DATE(time_out, "%Y-%m-%d") = "' . $getDate . '")';
                                                $present = '';
                                                $color = '#000';
                                                $attendanceEmployee = '';
                                                $attendanceEmployee = Attendance::GetAttendanceByEmployeeId($qery->id, $filterAtt);
                                                if (!empty($attendanceEmployee[0]->time_in) && $attendanceEmployee[0]->time_in != '0000-00-00 00:00:00') {
                                                    //echo date('Y-m-d', strtotime($attendanceEmployee[0]->time_in));
                                                    //echo date('Y-m-d').'<br>';
                                                    if ($attendanceEmployee[0]->shift == 'day') {
                                                        if (!empty($attendanceEmployee[0]->late_grace_time)) {
                                                            $startTime = date("Y-m-d", strtotime('+' . $attendanceEmployee[0]->late_grace_time . ' min', strtotime($attendanceEmployee[0]->time_in)));
                                                        } else {
                                                            $startTime = date('Y-m-d', strtotime($attendanceEmployee[0]->time_in));
                                                        }
                                                        if ($startTime <= date('Y-m-d')) {
                                                            if ($attendanceEmployee) {
                                                                //echo date('H:i:s', strtotime($attendanceEmployee[0]->time_in)) . '<br>';

                                                                if (date('H:i:s', strtotime($attendanceEmployee[0]->time_in)) <= $attendanceEmployee[0]->shift_start_time) {
                                                                    $present = 'P';
                                                                    $color = '#00FF00';
                                                                } else {
                                                                    $present = 'L';
                                                                    $color = '#996600';
                                                                    $lateCount++;
                                                                }
                                                            } else {
                                                                if(!empty($background)){
                                                                    $present = 'H';
                                                                }else{
                                                                    $color = '#FF0000';
                                                                    $present = 'A';
                                                                    $absentCount++;
                                                                }
                                                            }
                                                        }
                                                    } else if ($attendanceEmployee[0]->shift == 'night') {
                                                        if (!empty($attendanceEmployee[0]->late_grace_time)) {
                                                            $startTime = date("Y-m-d H:i:s", strtotime('+' . $attendanceEmployee[0]->late_grace_time . ' min', strtotime($attendanceEmployee[0]->time_in)));
                                                        } else {
                                                            $startTime = date('Y-m-d H:i:s', strtotime($attendanceEmployee[0]->time_in));
                                                        }
                                                        if ($startTime <= date('Y-m-d 19:00:00')) {
                                                            if ($attendanceEmployee) {
                                                                //echo date('H:i:s', strtotime($attendanceEmployee[0]->time_in)) . '<br>';
                                                                if (date('H:i:s', strtotime($attendanceEmployee[0]->time_in)) <= $attendanceEmployee[0]->shift_start_time) {
                                                                    $present = 'P';
                                                                    $color = '#00FF00';
                                                                } else {
                                                                    $present = 'L';
                                                                    $color = '#996600';
                                                                    $lateCount++;
                                                                }
                                                            } else {
                                                                if(!empty($background)){
                                                                    $present = 'H';
                                                                }else{
                                                                    $color = '#FF0000';
                                                                    $present = 'A';
                                                                    $absentCount++;
                                                                }
                                                            }
                                                        }
                                                    }
                                                } else if ($getDate <= date('Y-m-d')) {
                                                    if(!empty($background)){
                                                        $present = 'H';
                                                    }else{
                                                        $present = 'A';
                                                        $color = '#FF0000';
                                                        $absentCount++;
                                                    }
                                                } else {
                                                    if(!empty($background)){
                                                        $present = 'H';
                                                    }else{
                                                        $present = '-';
                                                    }
                                                }

                                                $time = mktime(12, 0, 0, $monthCheck, $d, $year);
                                                if (date('m', $time) == $monthCheck) {
                                                    echo '<td style="background-color:'.$background.'; font-weight:bold; color:' . $color . '; " >' . $present . '</td>';
                                                    //echo '<td>' . date('d', $time) . 'd' . $present . '</td>';
                                                }
                                            }
                                            echo '<td>'.$lateCount.'</td>';
                                            echo '<td>'.$absentCount.'</td>';
                                            echo '<td></td>';
                                            echo '<td>'.$holidayCount.'</td>';
                                            echo '</tr>';
                                        }
                                    }
                                    ?>
                                    <tr style="border: 1px solid #666;">
                                        <th>Employee</th>
                                        <?php
                                        if ($year) {
                                            $year = $year;
                                        } else {
                                            $year = date('Y');
                                        }
                                        for ($d = 1; $d <= 31; $d++) {
                                            $time = mktime(12, 0, 0, $monthCheck, $d, $year);
                                            if (date('m', $time) == $monthCheck)
                                                echo '<th>' . date('D', $time) . '</th>';
                                        }
                                        ?>
                                        <th>Late</th>
                                        <th>Absent</th>
                                        <th>AL/CL</th>
                                        <th>Holiday</th>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                            <?php

                        } // close view
                        ?>
                    </div>
                </div>
            </div>
            <footer>
                <?php include('../includes/inc_footer.php'); ?>
            </footer>
            </div>
        </section>
        <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>