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

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

// Fetch recent payments
$sql = "SELECT pay.*, u.full_name AS patient_name, inv.invoice_id
        FROM payments pay
        LEFT JOIN patients p ON pay.patient_id = p.patient_id
        LEFT JOIN users u ON p.user_id = u.user_id
        LEFT JOIN invoices inv ON pay.invoice_id = inv.invoice_id
        ORDER BY pay.payment_date DESC, pay.payment_id DESC LIMIT 100";
$result = $conn->query($sql);
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Payment Receipts')); ?>
    <?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">Payment Receipts</h4>
                                <div class="page-title-right">
                                    <a href="collect_payment.php" class="btn btn-primary">New Payment</a>
                                </div>
                            </div>
                        </div>
                    </div>

                    <?php if (isset($_SESSION['success'])): ?>
                        <div class="alert alert-success"><?php echo $_SESSION['success'];
                        unset($_SESSION['success']); ?>
                        </div>
                    <?php endif; ?>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="receiptsTable"
                                        class="table table-bordered dt-responsive nowrap table-striped">
                                        <thead>
                                            <tr>
                                                <th>Ref No</th>
                                                <th>Date</th>
                                                <th>Patient</th>
                                                <th>Amount</th>
                                                <th>Method</th>
                                                <th>Invoice #</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($row = $result->fetch_assoc()): ?>
                                                <tr>
                                                    <td>#<?php echo $row['payment_id']; ?></td>
                                                    <td><?php echo date('M d, Y', strtotime($row['payment_date'])); ?></td>
                                                    <td><?php echo htmlspecialchars($row['patient_name']); ?></td>
                                                    <td><?php echo number_format($row['amount_paid'], 2); ?></td>
                                                    <td><?php echo $row['payment_method']; ?></td>
                                                    <td><?php echo $row['invoice_id'] ? '#' . $row['invoice_id'] : '-'; ?>
                                                    </td>
                                                    <td>
                                                        <a href="generate_receipt.php?id=<?php echo $row['payment_id']; ?>"
                                                            target="_blank" class="btn btn-sm btn-info"><i
                                                                class="ri-printer-line"></i> Print</a>
                                                    </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>$('#receiptsTable').DataTable({ order: [[0, 'desc']] });</script>
</body>

</html>