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

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

// Fetch claims
$sql = "SELECT c.*, p.first_name, p.last_name, p.patient_number, ip.provider_name 
        FROM insurance_claims c
        JOIN patients p ON c.patient_id = p.patient_id
        JOIN insurance_providers ip ON c.provider_id = ip.provider_id
        ORDER BY c.created_at DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Track Insurance Claims')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.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">

                    <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">Track Insurance Claims</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 active">Track Claims</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">Claims History</h5>
                                </div>
                                <div class="card-body">
                                    <table id="claimsTable"
                                        class="table table-bordered dt-responsive nowrap table-striped align-middle"
                                        style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>Claim #</th>
                                                <th>Patient</th>
                                                <th>Provider</th>
                                                <th>Service Date</th>
                                                <th>Amount</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if ($result && $result->num_rows > 0): ?>
                                                <?php while ($row = $result->fetch_assoc()): ?>
                                                    <tr>
                                                        <td><?php echo htmlspecialchars($row['claim_number']); ?></td>
                                                        <td>
                                                            <?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?><br>
                                                            <small
                                                                class="text-muted"><?php echo htmlspecialchars($row['patient_number']); ?></small>
                                                        </td>
                                                        <td><?php echo htmlspecialchars($row['provider_name']); ?></td>
                                                        <td><?php echo date('d M Y', strtotime($row['service_date'])); ?></td>
                                                        <td>KES <?php echo number_format($row['claim_amount'], 2); ?></td>
                                                        <td>
                                                            <?php
                                                            $badgeClass = 'bg-primary';
                                                            if ($row['claim_status'] == 'APPROVED')
                                                                $badgeClass = 'bg-success';
                                                            if ($row['claim_status'] == 'REJECTED')
                                                                $badgeClass = 'bg-danger';
                                                            if ($row['claim_status'] == 'PENDING')
                                                                $badgeClass = 'bg-warning text-dark';
                                                            ?>
                                                            <span
                                                                class="badge <?php echo $badgeClass; ?>"><?php echo htmlspecialchars($row['claim_status']); ?></span>
                                                        </td>
                                                        <td>
                                                            <a href="view_claim.php?id=<?php echo $row['claim_id']; ?>"
                                                                class="btn btn-sm btn-info"><i class="ri-eye-line"></i> View</a>
                                                        </td>
                                                    </tr>
                                                <?php endwhile; ?>
                                            <?php endif; ?>
                                        </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 () {
            $('#claimsTable').DataTable();
        });
    </script>
</body>

</html>