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

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

include('../includes/inc_head.php');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Customer Last Invoice Report</title>
</head>
<body>
<!-- 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>

<div id="container">
    <?php include('../includes/inc_top.php'); ?>

    <div class="row">
        <div class="col s12 m12">
            <div class="card-panel">
                <h3>Customer Last Invoice Report</h3>
                <hr>

                <?php
                $filter = ""; // you can add filters if needed e.g. "WHERE cust.status='active'"
                $lastInvoices = Invoice::GetCustomerLastInvoiceList($filter);
                ?>

                <?php if ($lastInvoices) { ?>
                    <table class="bordered responsive-table highlight">
                        <thead>
                            <tr>
                                <th>S.No</th>
                                <th>Date</th>
                                <th>Invoice #</th>
                                <th>Customer</th>
                                <th>Status</th>
                                <th>Price</th>
                                <th>Sub Total</th>
                                <th>Days Passed</th>
                                
                            </tr>
                        </thead>
                        <tbody>
                        <?php
                        $count = 1;
                        foreach ($lastInvoices as $inv) {
                            echo "<tr>";
                            echo "<td>{$count}</td>";
                            echo "<td>" . date('d-m-Y', strtotime($inv->date)) . "</td>";
                            echo "<td>{$inv->invoice_no}</td>";
                            echo "<td>{$inv->customer_name}</td>";
                            echo "<td>{$inv->status}</td>";
                            echo "<td>" . number_format($inv->price, 2) . "</td>";
                            echo "<td>" . number_format($inv->sub_total, 2) . "</td>";
                            echo "<td>{$inv->days_passed}</td>";
                           
                            $count++;
                        }
                        ?>
                        </tbody>
                    </table>
                <?php } else { ?>
                    <div id="card-alert" class="card orange lighten-5">
                        <div class="card-content orange-text">
                            <p>WARNING : <strong>No records found.</strong></p>
                        </div>
                        <button type="button" class="close orange-text" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">x</span>
                        </button>
                    </div>
                <?php } ?>

                <div class="clearfix"><br></div>
                <div class="row">
                    <div class="col s12">
                        <button type="button" class="btn waves-effect waves-light"
                                onclick="window.location.href='invoice.php?view'">
                            Back to Invoices
                        </button>
                        <button type="button" class="btn waves-effect waves-light green"
                                onclick="window.print()">
                            Print Report
                        </button>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<?php include('../includes/inc_footer.php'); ?>
<?php include('../includes/inc_js.php'); ?>

<script>
$(document).ready(function() {
    $('table').DataTable({
        "pageLength": 25,
        "order": [[ 1, "asc" ]]
    });
});
</script>

</body>
</html>
