<?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 overdue invoices
$sql = "SELECT i.*, p.first_name, p.last_name, p.patient_number 
        FROM invoices i
        JOIN patients p ON i.patient_id = p.patient_id
        WHERE i.status = 'OVERDUE' 
        ORDER BY i.due_date ASC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Overdue Invoices')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <!-- DataTables -->
    <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">
                    <!-- Page Title -->
                    <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">Overdue 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="list_invoices.php">Invoices</a></li>
                                        <li class="breadcrumb-item active">Overdue</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">List of Overdue Invoices</h5>
                                </div>
                                <div class="card-body">
                                    <table id="overdueTable"
                                        class="table table-bordered dt-responsive nowrap table-striped align-middle"
                                        style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>Invoice #</th>
                                                <th>Patient</th>
                                                <th>Amount</th>
                                                <th>Due Date</th>
                                                <th>Days Overdue</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($row = $result->fetch_assoc()):
                                                $dueDate = new DateTime($row['due_date']);
                                                $today = new DateTime();
                                                $interval = $today->diff($dueDate);
                                                $daysOverdue = $interval->format('%a');
                                                ?>
                                                <tr>
                                                    <td><?php echo $row['invoice_number']; ?></td>
                                                    <td>
                                                        <?php echo $row['first_name'] . ' ' . $row['last_name']; ?><br>
                                                        <small
                                                            class="text-muted"><?php echo $row['patient_number']; ?></small>
                                                    </td>
                                                    <td><?php echo number_format($row['total_amount'], 2); ?></td>
                                                    <td><?php echo date('M d, Y', strtotime($row['due_date'])); ?></td>
                                                    <td><span class="badge bg-danger"><?php echo $daysOverdue; ?>
                                                            Days</span></td>
                                                    <td>
                                                        <div class="dropdown d-inline-block">
                                                            <button class="btn btn-soft-secondary btn-sm dropdown"
                                                                type="button" data-bs-toggle="dropdown"
                                                                aria-expanded="false">
                                                                <i class="ri-more-fill align-middle"></i>
                                                            </button>
                                                            <ul class="dropdown-menu dropdown-menu-end">
                                                                <li><a href="view_invoice.php?id=<?php echo $row['invoice_id']; ?>"
                                                                        class="dropdown-item"><i
                                                                            class="ri-eye-fill align-bottom me-2 text-muted"></i>
                                                                        View</a></li>
                                                                <li><a href="send_reminder.php?id=<?php echo $row['invoice_id']; ?>"
                                                                        class="dropdown-item"><i
                                                                            class="ri-mail-send-fill align-bottom me-2 text-muted"></i>
                                                                        Send Reminder</a></li>
                                                            </ul>
                                                        </div>
                                                    </td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <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 () {
            $('#overdueTable').DataTable();
        });
    </script>
</body>

</html>