<?php
include("../../classes/manager.class.php");
$m = new Manager();

if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
</head>
<body>
<div id="container">
    <?php include('../includes/inc_top.php'); ?>

    <div class="row">
        <div class="col s12 m12">

            <div class="">
                <h3>Customer Pending Balance Report</h3>
            </div>

            <form action="" method="get" name="search" id="searchfrm">
                <table class="table">
                    <tr>
                        <td class="col-lg-4">
                            <input type="hidden" name="view" value="1" />
                            <button type="submit" class="btn waves-effect waves-light" style="float:left;">
                                View Report
                            </button>

                            <?php if (isset($_GET['view'])) { ?>
                                <button type="button"
                                        onclick="exportTableToCSV('pending-balance-<?= 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>

            <hr>

            <?php if (isset($_GET['view'])) {

                $rows = Invoice::GetCustomerPendingBalanceReport();

                $totalBalance = 0;
                $totalPendingInvoices = 0;
                ?>
                <div id="floatThead" class="adv-table section">
                    <table class="striped demo1 bordered">
                        <thead>
                        <tr style="border: 1px solid #666;">
                            <th>S.No</th>
                            <th>Customer</th>
                            <th>Pending Invoices</th>
                            <th>Last Payment Amount</th>
                            <th>Days Since Last Payment</th>
                            <th>Balance</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $counter = 0;
                        if ($rows) {
                            foreach ($rows as $r) {
                                $counter++;

                                $totalBalance += (float)$r->balance;
                                $totalPendingInvoices += (int)$r->pending_invoices;
                                ?>
                                <tr style="border: 1px solid #666;">
                                    <td><?= $counter ?></td>
                                    <td><?= $r->customer_name ?></td>
                                    <td><?= $r->pending_invoices ?></td>
                                    <td><?= number_format($r->last_payment_amount, 2, '.', '') ?></td>
                                    <td><?= $r->days_since_last_payment ?></td>
                                    <td><?= number_format($r->balance, 2, '.', '') ?></td>
                                </tr>
                                <?php
                            }
                        } else {
                            ?>
                            <tr>
                                <td colspan="6" style="text-align:center; padding: 12px;">
                                    No pending balances found.
                                </td>
                            </tr>
                            <?php
                        }
                        ?>

                        <?php if ($rows) { ?>
                        <tr>
                            <th colspan="2" style="text-align:right; border: 1px solid #000;">TOTAL</th>
                            <th style="border: 1px solid #000;"><?= $totalPendingInvoices ?></th>
                            <th style="border: 1px solid #000;"></th>
                            <th style="border: 1px solid #000;"></th>
                            <th style="border: 1px solid #000;"><?= number_format($totalBalance, 2, '.', '') ?></th>
                        </tr>
                        <?php } ?>

                        </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="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/customer_pending_balance.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>
