<?php
include("../../classes/manager.class.php");
ini_set('display_errors', 1);
error_reporting(E_ALL);
$m = new Manager();
$output = '';
$cat_type = "";
$catType = "";
$reportData = '';

if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

extract($_REQUEST);

if (isset($cat_type) && $cat_type == 'All') {
    $filter = "";
} else {
    $filter = " AND cs.is_mart = '$cat_type'";
}

// Fetch Invoice Aging Report Data
$agingReportData = Invoice::GetInvoiceAgingReport($filter);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
</head>
<body>
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>

<div id="container">
    <?php include('../includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div>
                <h3> Customer Aging Report</h3>
            </div>

            <form action="" method="get" name="search" id="searchfrm">
                <table class="table">
                    <tr>
                        <td class="col s4">Type<br>
                            <select class="form-control" required id="cat_type" name="cat_type">
                                <option value="All" <?= ($cat_type == 'All') ? 'selected' : '' ?>>All</option>
                                <option value="0" <?= ($cat_type == '0') ? 'selected' : '' ?>>Wholesaler</option>
                                <option value="1" <?= ($cat_type == '1') ? 'selected' : '' ?>>Local Mart</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 (!empty($agingReportData)) { ?>
                                <button onclick="exportTableToCSV('aging-report-<?= date('d-m-Y-h-i-a') ?>.csv')"
                                        class="btn waves-effect waves-light"
                                        style="float:left; margin-left: 15px;">
                                    Export CSV
                                </button>
                                <button id="print"
                                        class="btn waves-effect waves-light"
                                        style="float:left; margin-left: 15px;">
                                    Print
                                </button>
                            <?php } ?>
                        </td>
                    </tr>
                </table>
            </form>

            <div id="floatThead" class="section">
                <table class="striped demo1 bordered">
                    <thead>
                        <tr>
                            <th>Customer Name</th>
                            <th>Invoice ID</th>
                            <th>Invoice Date</th>
                            <th>Invoice Amount</th>
                            <th>Payment Received</th>
                            <th>Balance</th>
                            <th>Aging Slab</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $groupedData = [];
                    foreach ($agingReportData as $invoice) {
                        $agingSlab = $invoice->Aging_Slab;
                        $groupedData[$agingSlab][] = $invoice;
                    }

                    foreach ($groupedData as $agingSlab => $invoices) {
                        echo "<tr class='aging-group' style='background-color: #f1f1f1; cursor: pointer;' onclick='toggleGroup(\"$agingSlab\")'>
                                <td colspan='7'><b>{$agingSlab}</b> (".count($invoices)." Invoices)</td>
                              </tr>";
                        
                        echo "<tbody id='group-{$agingSlab}' style='display: none;'>"; // Group details
                        foreach ($invoices as $invoice) {
                            echo "<tr>
                                    <td>{$invoice->Customer_Name}</td>
                                    <td>{$invoice->Invoice_ID}</td>
                                    <td>{$invoice->Invoice_Date}</td>
                                    <td>{$invoice->Invoice_Amount}</td>
                                    <td>{$invoice->Payment_Received}</td>
                                    <td>{$invoice->Balance}</td>
                                    <td>{$invoice->Aging_Slab}</td>
                                  </tr>";
                        }
                        echo "</tbody>";
                    }
                    ?>
                    </tbody>
                </table>
            </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'); ?>
<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 () {
        $('.invoice-details').hide();

        $('.aging-header').click(function () {
            $(this).next('.invoice-details').slideToggle();
        });

        $("#print").on("click", function (e) {
            var fewSeconds = 2;
            e.preventDefault();
            $('#searchfrm').prop('target', '_blank');
            $('#searchfrm').prop('action', "<?=PROJECT_ADMIN_URL?>report/print/aging_report_print.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>
