<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$datefrom = "";
$dateto = "";
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
extract($_REQUEST);
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
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'); ?>
</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>Salary Report</h3>
            </div>
            <?php if (isset($_GET['sucess'])) { ?>
                <div class="alert alert-success  fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <strong>*</strong> Successfully Updated.
                </div>
            <?php } ?>
            <?php if (isset($_GET['error'])) { ?>
                <div class="alert alert-error  fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    Error Ocurred. Try Again Later.
                </div>
            <?php } ?>
            <?php if (isset($_GET['availed'])) { ?>
                <div class="alert alert-error  fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    Your leaves limit is over. Contact to admin
                </div>
            <?php } ?>
            <script>
                $(function () {
                    $('#datepicker').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                      selectYears: true,
                    });
                    $('#datepicker1').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                      selectYears: true,
                    });
                });
            </script>
            <form action="" method="get" name="search"
                  id="searchfrm">
                <table class="table">
                    <tr style="border-top: 1px solid #ddd;">
                        <td class="col s6">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 s6">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 s12">
                            <button type="submit" class="btn waves-effect waves-light"
                                    style="float:left;">
                                Search
                            </button>
                            <?php if (isset($_GET['view'])) { ?>
                                <button
                                    onclick="exportTableToCSV('payment-<?= date('d-m-Y-h-i-a') ?>.csv')"
                                    class="btn waves-effect waves-light"
                                    style="float:left; margin-left: 15px;">
                                    Export CSV
                                </button>
                            <?php } ?>
                            <button
                                id="print"
                                class="btn waves-effect waves-light"
                                style="float:left; margin-left: 15px;">
                                Print
                            </button>
                        </td>
                    </tr>
                </table>
            </form>
            <hr><?php
            if (isset($_GET['view']) && empty($employee_id)) {
                ?>
                <div id="floatThead" class="section">
                    <table class="striped demo1 bordered">
                        <thead>
                        <tr style="border: 1px solid #666;">
                            <th>S.No</th>
                            <th>Employee Name</th>
                            <th>Date</th>
                            <th>Advance</th>
                            <th>Amount</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $getYearMonth = date('Y-m-00', strtotime($year . '-' . $monthCheck . '-01'));
                        $filterSalaryReport = " WHERE DATE_FORMAT(date_time,'%Y-%m-00')='$getYearMonth' ";
                        $filterSalaryReport .= " ORDER BY employee_id";
                        $qry = SalarySetup::GetSalarySetupList($filterSalaryReport);
                        if ($qry) {
                            $totalSalary = 0;
                            $countingSr = 1;
                            foreach ($qry AS $qery) {
                                $emp = EmployeeSetup::GetEmployeeSetupById($qery->employee_id);
                                $totalSalary += $qery->amount;
                                echo '<tr style="border: 1px solid #666;">';
                                echo '<td style="width: 50px;">' . $countingSr . '</td>';
                                echo '<td>' . $emp->name . '</td>';
                                echo '<td>' . date('Y-m-d', strtotime($qery->date_time)) . '</td>';
                                echo '<td>' . ($qery->advance == 1 ? "Yes" : "No") . '</td>';
                                echo '<td>' . $qery->amount . '</td>';
                                echo '</tr>';
                                $countingSr++;
                            }
                            echo '<tr style="border: 1px solid #666;">';
                            echo '<td colspan="4">Total Salary</td>';
                            echo '<td>' . $totalSalary . '</td>';
                            echo '</tr>';
                        }
                        ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } ?>
        </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'); ?>
<script type="text/javascript" src="<?= PROJECT_URL ?>js/plugins/floatThead/jquery.floatThead.min.js"></script>
<script type="text/javascript"
        src="<?= PROJECT_URL ?>js/plugins/floatThead/jquery.floatThead-slim.min.js"></script>
<script type="application/javascript">
    $(document).ready(function () {
        $("#print").on("click", function (e) {
            var fewSeconds = 2;
            e.preventDefault();
            $('#searchfrm').prop('target', '_blank');
            $('#searchfrm').prop('action', "<?=PROJECT_ADMIN_URL?>report/print/salary.php?").submit();
            setTimeout(function () {
                $('#searchfrm').prop('target', "");
                $('#searchfrm').prop('action', "");
            }, fewSeconds * 1000);
        });
        $('table.demo1').floatThead({
            position: 'fixed',
            scrollingTop: 65
        });
        $('table.demo1 thead').css('background', '#ECEFF1');
    });
</script>
</body>
</html>