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

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

$user_id = $_SESSION['user_id'];
$pat_res = $conn->query("SELECT patient_id FROM patients WHERE user_id = $user_id")->fetch_assoc();
$patient_id = $pat_res['patient_id'];

// Limit to last 50 payments
$sql = "SELECT p.*, i.invoice_number 
        FROM payments p
        JOIN invoices i ON p.invoice_id = i.invoice_id
        WHERE i.patient_id = ?
        ORDER BY p.payment_date DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $patient_id);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

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

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="paymentsTable" class="table table-bordered dt-responsive nowrap w-100">
                                        <thead>
                                            <tr>
                                                <th>Reference</th>
                                                <th>Date</th>
                                                <th>Invoice</th>
                                                <th>Method</th>
                                                <th>Amount</th>
                                                <th>Status</th>
                                                <th>Receipt</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($row = $result->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?php echo $row['payment_reference']; ?></td>
                                                    <td><?php echo date('M d, Y H:i', strtotime($row['payment_date'])); ?>
                                                    </td>
                                                    <td><a
                                                            href="patient_invoices.php"><?php echo $row['invoice_number']; ?></a>
                                                    </td>
                                                    <td><?php echo str_replace('_', ' ', $row['payment_method']); ?></td>
                                                    <td>KES <?php echo number_format($row['amount'], 2); ?></td>
                                                    <td><span class="badge bg-success"><?php echo $row['status']; ?></span>
                                                    </td>
                                                    <td>
                                                        <a href="patient_download_records.php?type=receipt&id=<?php echo $row['payment_id']; ?>"
                                                            class="btn btn-sm btn-soft-info">
                                                            <i class="ri-receipt-line"></i> Receipt
                                                        </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>
        $(document).ready(function () {
            $('#paymentsTable').DataTable({
                order: [[1, 'desc']]
            });
        });
    </script>
</body>

</html>