<?php
session_start();
include 'layouts/main.php';
include 'layouts/dbconn.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Fetch etims data
$sql = "SELECT e.*, i.invoice_number, i.total_amount 
        FROM etims_invoices e
        LEFT JOIN invoices i ON e.invoice_id = i.invoice_id
        ORDER BY e.created_at DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'eTIMS Invoices')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" type="text/css" />
    <link href="assets/libs/datatables.net-responsive-bs5/css/responsive.bootstrap5.min.css" rel="stylesheet"
        type="text/css" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">eTIMS Tax Invoices</h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                                        <li class="breadcrumb-item"><a href="#">eTIMS</a></li>
                                        <li class="breadcrumb-item active">Invoices</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">Fiscal Invoices</h5>
                                </div>
                                <div class="card-body">
                                    <table id="etimsTable"
                                        class="table table-bordered dt-responsive nowrap table-striped align-middle"
                                        style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>System Invoice #</th>
                                                <th>CU Invoice #</th>
                                                <th>Amount</th>
                                                <th>Date Signed</th>
                                                <th>Status</th>
                                                <th>Verification Code</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if ($result && $result->num_rows > 0): ?>
                                                <?php while ($row = $result->fetch_assoc()): ?>
                                                    <tr>
                                                        <td><?php echo htmlspecialchars($row['invoice_number']); ?></td>
                                                        <td><?php echo htmlspecialchars($row['cu_invoice_number']); ?></td>
                                                        <td><?php echo number_format($row['total_amount'], 2); ?></td>
                                                        <td><?php echo date('M d, Y H:i', strtotime($row['created_at'])); ?>
                                                        </td>
                                                        <td>
                                                            <span class="badge bg-success">Verified</span>
                                                        </td>
                                                        <td>
                                                            <small
                                                                class="text-muted"><?php echo htmlspecialchars($row['cu_serial_number']); ?></small>
                                                        </td>
                                                    </tr>
                                                <?php endwhile; ?>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script src="assets/libs/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
    <script src="assets/libs/datatables.net-responsive-bs5/js/responsive.bootstrap5.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#etimsTable').DataTable();
        });
    </script>
</body>

</html>