<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$UpdateAtt = '';
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));

if ($_POST) {
    $datetime = date('Y-m-d', strtotime($date)) . ' ' . date('H:i', strtotime($time));
    if (!empty($emp)) {
        if (isset($add)) {
            $employee = EmployeeSetup::GetEmployeeSetupById($emp);
            $employeeCat = EmployeeCategorySetup::GetEmployeeCategorySetupById($employee->category_id);

            $data['time_in'] = $datetime;
            $data['employee_id'] = $employee->id;
            $data['machine_id'] = $employee->machine_userid;
            $data['shift'] = $shift;
            $data['shift_start_time'] = $employeeCat->shift_start_time;
            $data['late_grace_time'] = $employeeCat->late_grace_time;
            $data['late_deduction'] = $employeeCat->late_deduction;
            Attendance::AddAttendance($data);
        } elseif (isset($del)) {
            $UpdateAtt = Attendance::DeleteAttendance($attid);
        } elseif (isset($edit)) {
            $employee = EmployeeSetup::GetEmployeeSetupById($emp);
            $data['time_in'] = $datetime;
            $UpdateAtt = Attendance::UpdateAttendance($attid, $data);
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <style>
        #time {
            color: #000;
            margin: 0px;
            font-family: "Courier New", Courier, monospace;
            font-size: 16px;
            text-align: left;
        }
    </style>
</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">
            <?php if ($UpdateAtt) { ?>
                <div class="alert alert-success  fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <strong>*</strong> Successfully Deleted.
                </div>
            <?php } ?>
            <div class="">
                <h3>Daily Attendance Report</h3>
                <a href="<?= PROJECT_ADMIN_URL ?>report/dailyattendance.php?dateto=<?= $dateto ?>&view=">
                    <button
                        class="right btn waves-effect waves-light"
                        style=" margin-left: 15px;">
                        Back To Attendance
                    </button>
                </a>
            </div>
            <script>
                $(function () {
                    var date = new Date('<?=date('m-d-Y', strtotime($dateto))?>');
                    var currentMonth = date.getMonth();
                    var currentDate = date.getDate();
                    var nextDate = date.getDate() + 1;
                    var currentYear = date.getFullYear();
                    $('#datepicker').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                        selectYears: true,
                        min: new Date(currentYear, currentMonth, currentDate),
                        max: new Date(currentYear, currentMonth, nextDate)
                    });
                });
            </script>
            <hr><?php
            if (isset($_GET['view'])) {
                ?>
                <div>
                    <?php
                    $filterEmployee = " WHERE status = 'active' AND id = '$emp'";
                    $qry = EmployeeSetup::GetEmployeeSetupList($filterEmployee);
                    if ($qry) {
                        foreach ($qry AS $qery) {
                            //echo '<div class="col-md-12"><section class="panel">';
                            //echo '<header class="panel-heading">' . $qery->name . '</header>';
                            echo '<table class="table table-bordered table-striped">';
                            $getDate = date('Y-m-d', strtotime($dateto));
                            $filterAtt = ' AND STR_TO_DATE(time_in, "%Y-%m-%d") = "' . $getDate . '" ORDER BY time_in ';
                            $present = '';
                            $attendanceEmployee = '';
                            $attendance = Attendance::GetAttendanceByEmployeeId($qery->id, $filterAtt);

                            if (empty($attendance) || $attendance[0]->shift == 'night') {
                                $getDateNight = date('Y-m-d 19:00:00', strtotime($dateto));
                                $getDateNightPlus = date('Y-m-d 18:59:59', strtotime('+1 day', strtotime($dateto)));
                                $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" ';
                                $attendance = Attendance::GetAttendanceByEmployeeId($qery->id, $filterAttNight);
                            }
                            if ($attendance) {
                                echo '<tr>';
                                echo '<th colspan="2" style="font-weight: bold;">' . $qery->name . ' (' . ucwords($attendance[0]->shift) . ')';
                                echo '<a style="cursor:pointer;" data-toggle="modal" class="modal-trigger" href="#AddModal" title="Add">
                                    <button
                                        class="right btn waves-effect waves-light"
                                        style=" margin-left: 15px;">
                                        Add
                                    </button>
                                </a>';
                                echo '</th>';
                                $count = 0;
                                foreach ($attendance AS $attendanceRow) {
                                    echo '<tr>';
                                    if ($count % 2 == 0) {
                                        echo '<td>Time In</td>';
                                        echo '<td>';
                                        echo $attendanceRow->time_in != '0000-00-00 00:00:00' ? date('d-m-Y h:i A', strtotime($attendanceRow->time_in)) : '';
                                        echo '<a href="#modal' . $count . '" data-toggle="modal" class="modal-trigger right todo-done"><i class="mdi-action-delete"></i></a>';
                                        echo '<div id="modal' . $count . '" class="modal fade"
                                                             role="dialog">
                                                                <form method="post" name="delattendanceform">
                                                                    <!-- Modal content-->
                                                                    <div class="modal-content">
                                                                        <h4 class="modal-title">Delete Data</h4>
                                                                        <div class="modal-body">
                                                                            <p>Are you sure you want to delete</p>
                                                                            <input class="form-control" value="' . $attendanceRow->id . '" type="hidden" name="attid" id="attid"/>
                                                                            <input class="form-control" value="' . $emp . '" type="hidden" name="emp" id="emp"/>
                                                                            <input class="form-control" value="1" type="hidden" name="del" id="del"/>
                                                                            <br><br><br>
                                                                        </div>
                                                                    </div>
																	<div class="modal-footer">
																		<input type="submit" class="btn waves-effect waves-light" name="submit"
																			   id="submit" value="Submit"/>
																	</div>
                                                                </form>
                                                        </div>';
                                        echo '<a href="#modaledit' . $count . '" style="margin-right: 10px;" data-toggle="modal" class="modal-trigger right todo-done"><i class="mdi-image-edit"></i></a>';
                                        ?>
                                        <script>
                                            $(function () {
                                                var date = new Date('<?=date('m-d-Y', strtotime($dateto))?>');
                                                var currentMonth = date.getMonth();
                                                var currentDate = date.getDate();
                                                var nextDate = date.getDate() + 1;
                                                var currentYear = date.getFullYear();
                                                $('#datepicker<?=$count?>').pickadate({
                                                    format: 'dd-mm-yyyy',
                                                    selectMonths: true,
                                                    selectYears: true,
                                                    min: new Date(currentYear, currentMonth, currentDate),
                                                    max: new Date(currentYear, currentMonth, nextDate)
                                                });
                                            });
                                        </script>
                                    <?php
                                    echo '<div id="modaledit' . $count . '" class="modal">
														<form method="post" name="editattendanceform">
															<!-- Modal content-->
															<div class="modal-content">
																	<h4 class="modal-title">Edit Data</h4>
																<div class="modal-body">
																	<p>Are you sure you want to delete</p>
																	<span class="col-sm-4">Date<br>
																		<input class="form-control" type="text" name="date" value="' . date('d-m-Y', strtotime($attendanceRow->time_in)) . '" id="datepicker' . $count . '"/>
																	</span>
																	<span class="col-sm-4">Time<br>
																		<input class="form-control" type="time" name="time" value="' . date('H:i', strtotime($attendanceRow->time_in)) . '" id="time"/>
																	</span>
																	<input class="form-control" value="' . $attendanceRow->id . '" type="hidden" name="attid" id="attid"/>
																	<input class="form-control" value="' . $emp . '" type="hidden" name="emp" id="emp"/>
																	<input class="form-control" value="1" type="hidden" name="edit" id="edit"/>
																	<br><br><br>
																</div>
															</div>
															<div class="modal-footer">
																<input type="submit" class="btn waves-effect waves-light" name="submit"
																	   id="submit" value="Submit"/>
															</div>
														</form>
												</div>';
                                    echo '</td>';
                                    } else {
                                    echo '<td>Time Out</td>';
                                    echo '<td>';
                                    echo $attendanceRow->time_in != '0000-00-00 00:00:00' ? date('d-m-Y h:i A', strtotime($attendanceRow->time_in)) : '';
                                    echo '<a href="#modal' . $count . '" data-toggle="modal" class="modal-trigger right todo-done"><i class="mdi-action-delete"></i></a>';
                                    echo '<div id="modal' . $count . '" class="modal">
											<form method="post" name="delattendanceform">
												<!-- Modal content-->
												<div class="modal-content">
													<h4 class="modal-title">Delete Data</h4>
													<div class="modal-body">
														<p>Are you sure you want to delete</p>
														<input class="form-control" value="' . $attendanceRow->id . '" type="hidden" name="attid" id="attid"/>
														<input class="form-control" value="' . $emp . '" type="hidden" name="emp" id="emp"/>
														<input class="form-control" value="1" type="hidden" name="del" id="del"/>
														<br><br><br>
													</div>
												</div>
												<div class="modal-footer">
													<input type="submit" class="bbtn waves-effect waves-light" name="submit"
														   id="submit" value="Submit"/>
												</div>
											</form>
										</div>';
                                    echo '<a href="#modaledit' . $count . '" style="margin-right: 10px;" data-toggle="modal"  class="modal-trigger right todo-done"><i class="mdi-image-edit"></i></a>';
                                    ?>
                                        <script>
                                            $(function () {
                                                var date = new Date('<?=date('m-d-Y', strtotime($dateto))?>');
                                                var currentMonth = date.getMonth();
                                                var currentDate = date.getDate();
                                                var nextDate = date.getDate() + 1;
                                                var currentYear = date.getFullYear();
                                                $('#datepicker<?=$count?>').pickadate({
                                                    format: 'dd-mm-yyyy',
                                                    selectMonths: true,
                                                    selectYears: true,
                                                    min: new Date(currentYear, currentMonth, currentDate),
                                                    max: new Date(currentYear, currentMonth, nextDate)
                                                });
                                            });
                                        </script>
                                        <?php
                                        echo '<div id="modaledit' . $count . '" class="modal">
												<form method="post" name="editattendanceform">
													<!-- Modal content-->
														<h4 class="modal-title">Edit Data</h4>
														<div class="modal-body">
															<p>Are you sure you want to delete</p>
															<div class="row margin">
																<div class="input-field col s12">
																	<input class="form-control" type="text" name="date" value="' . date('d-m-Y', strtotime($attendanceRow->time_in)) . '" id="datepicker' . $count . '"/>
																	<label for="datepicker" class="control-label">Date *</label>
																</div>
															</div>
															<div class="row margin">
																<div class="col s12">
																	<label class="control-label">Time *</label>
																</div>
																<div class="input-field col s12">
																	<input class="form-control" type="time" name="time" value="' . date('H:i', strtotime($attendanceRow->time_in)) . '" id="time"/>
																</div>
															</div>
															<input class="form-control" value="' . $attendanceRow->id . '" type="hidden" name="attid" id="attid"/>
															<input class="form-control" value="' . $emp . '" type="hidden" name="emp" id="emp"/>
															<input class="form-control" value="1" type="hidden" name="edit" id="edit"/>
															<br><br><br>
														</div>
														<div class="modal-footer">
															<input type="submit" class="btn waves-effect waves-light" name="submit"
																   id="submit" value="Submit"/>
														</div>
												</form>
                                                </div>';
                                        echo '</td>';
                                    }
                                    echo '</tr>';
                                    $count++;
                                }
                            } else {
                                echo '<tr>';
                                echo '<th>Employee Name';
                                echo '<a style="cursor:pointer;" data-toggle="modal" class="modal-trigger" href="#AddModal" title="Add">
                                    <button
                                        class="right btn waves-effect waves-light"
                                        style=" margin-left: 15px;">
                                        Add
                                    </button>
                                </a>';
                                echo '</th>';
                                echo '<th>Status</th>';
                                echo '</tr><tr>';
                                echo '<td>' . $qery->name . '</td>';
                                echo '<td>Absent</td></tr>';
                            }

                            echo '</table>';
                            //echo '</section></div>';
                        }
                    }
                    ?>
                    <!-- Modal -->
                    <div id="AddModal" class="modal">
                            <form method="post" name="addattendanceform"
                                  enctype="multipart/form-data">
                                <!-- Modal content-->
                                <div class="modal-content">
                                    <h4 class="modal-title">Add Data</h4>
                                    <div class="modal-body">
                                        <p>Please select below fields</p>
											<div class="row margin">
                                        <div class="input-field col s12">
												<input class="form-control" type="text" name="date" id="datepicker"/>
												<label for="datepicker" class="control-label">Date *</label>
											</div>
											</div>
											<div class="row margin">
												<div class="col s12">
													<label class="control-label">Time *</label>
												</div>
												<div class="input-field col s12">
													<input class="form-control" type="time" name="time" id="time"/>
												</div>
											</div>
											<div class="row margin">
                                        <div class="input-field col s12">
												<select class="form-control" name="shift" id="shift">
													<option value="day">Day</option>
													<option value="night">Night</option>
												</select>
												<label for="shift" class="control-label">Shift *</label>
											</div>
											</div>
                                        <input class="form-control" value="<?= $emp ?>" type="hidden" name="emp"
                                               id="emp"/>
                                        <input class="form-control" value="1" type="hidden" name="add" id="add"/>
                                        <br><br><br>
                                    </div>
                                </div>
								<div class="modal-footer">
									<input type="submit" class="btn waves-effect waves-light" name="submit"
										   id="submit" value="Submit"/>
								</div>
                            </form>
                        
                    </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>