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

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

extract($_REQUEST);

$days = isset($days) && $days != "" ? (int)$days : 7;
if ($days <= 0) { $days = 7; }

?>
<!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>Follow Up Report</h3>
            </div>

            <form action="" method="get" name="search" id="searchfrm">
                <table class="table">
                    <tr>
                        <td class="col-lg-4">
                            Days Since Last Invoice (>=)<br>
                            <input type="number" name="days" id="days"
                                   value="<?= $days ?>" class="form-control" min="1"/>
                            <input type="hidden" name="view" id="view" class="form-control"/>
                        </td>
                    </tr>

                    <tr>
                        <td class="col-lg-4">
                            <button type="submit" class="btn waves-effect waves-light" style="float:left;">
                                Search
                            </button>

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

                $filter = ""; // keep empty for now (future filters can be added here)
                $rows = Invoice::GetCustomerFollowUpReport($days, $filter);

                ?>
                <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>Contact</th>
                            <th>Balance</th>
                            <th>Total Invoices</th>
                            <th>Days Since Last Invoice</th>
                            <th>Remarks</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $counter = 0;

                        if ($rows) {
                            foreach ($rows as $r) {
                                $counter++;
                                ?>
                                <tr style="border: 1px solid #666;">
                                    <td><?= $counter ?></td>
                                    <td><?= $r->customer_name ?></td>
                                    <td><?= $r->customer_contact ?></td>
                                    <td><?= number_format($r->balance, 2, '.', '') ?></td>
                                    <td><?= $r->total_invoices ?></td>
                                    <td><?= $r->days_since_last_invoice ?></td>
                                    <td style="min-width: 220px;"></td>
                                </tr>

                                <?php
                            }
                        } else {
                            ?>
                            <tr>
                                <td colspan="6" style="text-align:center; padding: 12px;">
                                    No customers found for follow-up.
                                </td>
                            </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="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/follow_up.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>
