<?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]";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <style>
        li.optgroup:nth-child(2n) ~ li span:not(.optgroup) {
            color: #999 !important;
        }
    </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">
            <div class="">
                <h3>Loan 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 } ?>
            <form action="" method="get" name="search"
                  id="searchfrm">
                <table class="table">
                    <tr style="border-top: 1px solid #ddd;">
                        <td class="col s6">Employee<br>
                            <?php
                            $icon = "";
                            $filterGetCust = " ORDER BY status";
                            $qryEmployee = EmployeeSetup::GetEmployeeSetupList($filterGetCust);
                            if ($qryEmployee) {
                                echo '<select id="employee_id" name="employee_id" class="form-control">';
                                echo '<option value="all">All</option>';
                                $activeGroup = 0;
                                $inactiveGroup = 0;
                                foreach ($qryEmployee as $qery) {
                                    if ($activeGroup == 0 && $qery->status == 'active') {
                                        $activeGroup = 1;
                                        $icon = "../images/active.png";
                                        echo '<optgroup label="' . ucfirst($qery->status) . '">';
                                    } else if ($inactiveGroup == 0 && $qery->status == 'inactive') {
                                        $inactiveGroup = 1;
                                        $icon = "../images/disable.png";
                                        echo '</optgroup><optgroup label="' . ucfirst($qery->status) . '">';
                                    }
                                    echo '<option data-icon="' . $icon . '" value=' . $qery->id;
                                    if ($qery->id == $employee_id) {
                                        echo ' selected ';
                                    }
                                    echo '>' . $qery->name . '</option>';
                                }
                                echo '</optgroup></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('loan-<?= 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 (!empty($employee_id)) {
                ?>
                <div id="floatThead" class="section">
                        <?php
                        if($employee_id == 'all'){
                            $filterGetCust = " ORDER BY status";
                        }else{
                            $filterGetCust = " WHERE id = '".$employee_id."' ORDER BY status";
                        }
                        $qryEmployee = EmployeeSetup::GetEmployeeSetupList($filterGetCust);
                        foreach ($qryEmployee AS $qryEmp) {
                            $loanfilter = " WHERE employee_id = '".$qryEmp->id."' ";
                            $qryloan = LoanSetup::GetLoanList($loanfilter);
                            if($qryloan){
                            ?>
                            <table class="striped demo1 bordered">
                                <thead>
                                <tr style="border: 1px solid #666;">
                                    <th colspan="6"><?=$qryEmp->name?> <span style="float: right"><?=$qryEmp->status?></span></th>
                                </tr>
                                <tr style="border: 1px solid #666;">
                                    <th>Loan Description</th>
                                    <th>Amount</th>
                                    <th>Installment</th>
                                    <th>Start Date</th>
                                    <th>End Date</th>
                                    <th>Paid</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                <?php
                                $loanstatus = 0;
                                foreach($qryloan AS $qreyloan){
                                    if($qreyloan->employee_id == $qryEmp->id){
                                        echo "<td>".$qreyloan->description."</td>";
                                        echo "<td>".$qreyloan->amount."</td>";
                                        echo "<td>".$qreyloan->installment."</td>";
                                        echo "<td>".date('d-m-Y', strtotime($qreyloan->start_date))."</td>";
                                        $endDate = date("d-m-Y", strtotime("+".intval($qreyloan->installment)." month", strtotime($qreyloan->start_date)));
                                        echo "<td>".$endDate."</td>";
                                        echo "<td>-</td>";
                                        $loanstatus = 1;
                                        $installmentprice = $qreyloan->amount/$qreyloan->installment;
                                    }
                                }
                                if($loanstatus == 0){
                                    echo "<td colspan='6'>-</td>";
                                }
                                ?>
                                </tr>
                                    <?php
                                    $startDateSal = date("d-m-Y", strtotime($qreyloan->start_date));
                                    $endDateSal = date("d-m-Y", strtotime($endDate));
                                    $salfilter = " WHERE employee_id = '".$qryEmp->id."' AND DATE_FORMAT(date_time,'%Y-%m-%d')>='".date('Y-m-d', strtotime($startDateSal))."' AND DATE_FORMAT(date_time,'%Y-%m-%d')<='".date('Y-m-d', strtotime($endDateSal))."' AND advance = 0  ORDER BY date_time";
                                    $salaryemp = SalarySetup::GetSalarySetupList($salfilter);
                                    $totinstallmentprice = 0;
                                    foreach($salaryemp AS $salaryemps){
                                        if($salaryemps->employee_id == $qryEmp->id){
                                            echo "<tr>";
                                            echo "<td>".date('m-Y', strtotime($salaryemps->date_time))."</td>";
                                            echo "<td></td>";
                                            echo "<td></td>";
                                            echo "<td></td>";
                                            echo "<td></td>";
                                            echo "<td>".$installmentprice."</td>";
                                            echo "</tr>";
                                            $totinstallmentprice += $installmentprice;
                                        }
                                    }
                                    ?>
                                <tr>
                                    <td colspan="5"><strong>Total</strong></td>
                                    <td><strong><?=$totinstallmentprice?></strong></td>
                                </tr>
                                <tr>
                                    <td colspan="5"><strong>Total Remaining Balance</strong></td>
                                    <td><strong><?=($qreyloan->amount - $totinstallmentprice)?></strong></td>
                                </tr>
                                </tbody>
                            </table>
                            <br>
                            <br>
                            <?php
                            }
                        }
                        ?>
                </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/loan.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>