<?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]";

// default status
if (!isset($status)) {
    $status = 'active';
}

// NEW: default employee type (all / admin / labour)
if (!isset($emp_type)) {
    $emp_type = 'all';
}
?>
<!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>Employee Status 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 } ?>
            <form action="" method="get" name="search"
                  id="searchfrm">
                <table class="table">
                    <tr style="border-top: 1px solid #ddd;">
                        <td class="col s6">
                            Status<br>
                            <select class="form-control" name="status" id="status">
                                <option value="active" <?= (($status=='active')?'selected':'') ?>>Active</option>
                                <option value="inactive" <?= (($status=='inactive')?'selected':'') ?>>In active</option>
                            </select>

                            <br><br>
                            <!-- NEW: Employee Type filter -->
                            Employee Type<br>
                            <select class="form-control" name="emp_type" id="emp_type">
                                <option value="all" <?= (($emp_type=='all')?'selected':'') ?>>All</option>
                                <option value="admin" <?= (($emp_type=='admin')?'selected':'') ?>>Admin</option>
                                <option value="labour" <?= (($emp_type=='labour')?'selected':'') ?>>Labour</option>
                            </select>

                            <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>
                            <?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>Joining Date</th>
                            <th>Employee Name</th>
                            <th>Status</th>
                            <th>Salary</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        // base filter: active / inactive
                        $filterEmployee = " WHERE status = '$status' ";

                        // NEW: apply employee type filter on designation
                        // Labour: designation contains 'LABOUR' or is one of specific labour roles
                        if ($emp_type == 'labour') {
                            $filterEmployee .= "
                                AND (
                                    UPPER(designation) LIKE '%LABOUR%'
                                    OR UPPER(designation) IN ('PACKING STAFF','MACHINE OPERATOR','MIXER OPERATOR')
                                )
                            ";
                        } elseif ($emp_type == 'admin') {
                            $filterEmployee .= "
                                AND NOT (
                                    UPPER(designation) LIKE '%LABOUR%'
                                    OR UPPER(designation) IN ('PACKING STAFF','MACHINE OPERATOR','MIXER OPERATOR')
                                )
                            ";
                        }

                        $filterEmployee .= " ORDER BY name";

                        $qry = EmployeeSetup::GetEmployeeSetupList($filterEmployee);
                        if ($qry) {
                            $totalSalary = 0;
                            $countingSr = 1;
                            foreach ($qry AS $qery) {
                                $totalSalary += $qery->salary;
                                echo '<tr style="border: 1px solid #666;">';
                                echo '<td style="width: 50px;">' . $countingSr . '</td>';
                                echo '<td>' . date('d-m-Y', strtotime($qery->joining_date)) . '</td>';
                                echo '<td>' . $qery->name . '</td>';
                                echo '<td>' . $qery->status . '</td>';
                                echo '<td>' . $qery->salary . '</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/employee.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>
