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

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Fetch pending payments (e.g., checks to clear, pending MPESA)
$sql = "SELECT p.*, i.invoice_number, pt.first_name, pt.last_name 
        FROM payments p
        JOIN invoices i ON p.invoice_id = i.invoice_id
        JOIN patients pt ON i.patient_id = pt.patient_id
        WHERE p.payment_status = 'PENDING'
        ORDER BY p.payment_date DESC";
// Assuming payment_status column exists. If not, I'll default to all payments or check schema.
// Schema check: payments table usually has status.
$result = $conn->query($sql);
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Pending Payments')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</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">Pending Payments</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="pendingPayTable"
                                        class="table table-bordered dt-responsive nowrap table-striped">
                                        <thead>
                                            <tr>
                                                <th>Date</th>
                                                <th>Invoice #</th>
                                                <th>Patient</th>
                                                <th>Method</th>
                                                <th>Reference</th>
                                                <th>Amount</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            if ($result) {
                                                while ($row = $result->fetch_assoc()):
                                                    ?>
                                                    <tr>
                                                        <td><?php echo $row['payment_date']; ?></td>
                                                        <td><?php echo htmlspecialchars($row['invoice_number']); ?></td>
                                                        <td><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
                                                        </td>
                                                        <td><?php echo htmlspecialchars($row['payment_method']); ?></td>
                                                        <td><?php echo htmlspecialchars($row['transaction_reference']); ?></td>
                                                        <td><?php echo number_format($row['amount_paid'], 2); ?></td>
                                                        <td>
                                                            <button class="btn btn-sm btn-success"><i class="ri-check-line"></i>
                                                                Verify</button>
                                                        </td>
                                                    </tr>
                                                <?php endwhile;
                                            } else { ?>
                                                <tr>
                                                    <td colspan="7" class="text-center">No pending payments found (or table
                                                        error)</td>
                                                </tr>
                                            <?php } ?>
                                        </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>$('#pendingPayTable').DataTable();</script>
</body>

</html>