<?php
include('../head.php');
include('../header.php'); 
requirePermission($pdo, $_SESSION['clientId'], 'reimbursement');

$pspId = $_SESSION['pspId'];

// Fetch patients with reimbursement data
$query = "SELECT 
    tblpatient.*,
    tblpatientdetails.patientId,
    tblpatientdetails.fileId,
    tblpatientdetails.createdDate,
    tblwcpart1.wcStatus,
    tblreimbursement.reimbursementId,
    tblreimbursement.reimbursementStatus,
    tblreimbursement.patientStatus,
    tblreimbursement.enrollmentCompleteDate,
    tblreimbursement.trtStrtDate
FROM tblpatient 
INNER JOIN tblpatientdetails ON tblpatient.id = tblpatientdetails.pId 
LEFT JOIN tblwcpart1 ON tblpatient.id = tblwcpart1.pId AND tblpatientdetails.fileId = tblwcpart1.fileId
LEFT JOIN tblreimbursement ON tblpatient.id = tblreimbursement.pId AND tblpatientdetails.fileId = tblreimbursement.fileId
WHERE tblpatient.isDeleted='N' 
AND tblpatientdetails.isDeleted='N'
AND tblpatientdetails.pspId = ?
AND tblwcpart1.wcStatus IN ('5', '13', '14', '15', '9')
ORDER BY tblpatient.id DESC";

$stmt = $pdo->prepare($query);
$stmt->execute([$pspId]);
$patients = $stmt->fetchAll();
?>

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">Reimbursement Management</h1>
                </div>
            </div>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Patient Reimbursement Status</h3>
                        </div>
                        <div class="card-body">
                            <table id="reimbursementTable" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>Patient ID</th>
                                        <th>Patient Name</th>
                                        <th>DOB</th>
                                        <th>Province</th>
                                        <th>Treatment Start</th>
                                        <th>Enrollment Complete</th>
                                        <th>Reimbursement Status</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach($patients as $patient): ?>
                                    <tr>
                                        <td><?= $patient['patientId']; ?></td>
                                        <td><?= $patient['firstName'].' '.$patient['lastName']; ?></td>
                                        <td><?= $patient['dob']; ?></td>
                                        <td><?= $patient['province']; ?></td>
                                        <td><?= $patient['trtStrtDate'] ? date('Y-m-d', strtotime($patient['trtStrtDate'])) : 'N/A'; ?></td>
                                        <td><?= $patient['enrollmentCompleteDate'] ? date('Y-m-d', strtotime($patient['enrollmentCompleteDate'])) : 'N/A'; ?></td>
                                        <td>
                                            <?php 
                                            if($patient['reimbursementStatus'] == '8' || $patient['reimbursementStatus'] == '27'): ?>
                                                <span class="badge badge-success">Completed</span>
                                            <?php elseif($patient['reimbursementStatus']): ?>
                                                <span class="badge badge-warning">In Progress</span>
                                            <?php else: ?>
                                                <span class="badge badge-secondary">Pending</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <a href="edit_reimbursement.php?id=<?= $patient['id']; ?>&fileId=<?= $patient['fileId']; ?>" 
                                               class="btn btn-sm btn-info">
                                                <i class="fas fa-edit"></i> Update
                                            </a>
                                            <a href="view_reimbursement.php?id=<?= $patient['id']; ?>&fileId=<?= $patient['fileId']; ?>" 
                                               class="btn btn-sm btn-primary">
                                                <i class="fas fa-eye"></i> View
                                            </a>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<?php include('../footer.php'); ?>

<script>
$(document).ready(function() {
    $('#reimbursementTable').DataTable({
        "paging": true,
        "lengthChange": true,
        "searching": true,
        "ordering": true,
        "info": true,
        "autoWidth": false,
        "responsive": true,
        "pageLength": 25
    });
});
</script>
