<?php
/**
 * Life Insurance Claims Management
 * Kenyan Pension & Life Insurance Management System
 */

require_once 'layouts/main.php';
require_once 'includes/auth.php';
require_once 'classes/LifeInsuranceService.php';

requireLogin();
requirePermission('view_claims');

$lifeService = new LifeInsuranceService($conn);
$action = $_GET['action'] ?? 'list';
$claim_type = $_GET['type'] ?? '';
$status = $_GET['status'] ?? '';

// Build query
$where = ["1=1"];
$params = [];
$types = '';

if (!empty($claim_type)) {
    $where[] = "lc.claim_type = ?";
    $params[] = $claim_type;
    $types .= 's';
}

if (!empty($status)) {
    $where[] = "lc.claim_status = ?";
    $params[] = $status;
    $types .= 's';
}

$whereClause = implode(' AND ', $where);

$sql = "SELECT lc.*, lp.policy_number, m.member_number, m.first_name, m.last_name
        FROM life_claims lc
        INNER JOIN life_policies lp ON lc.policy_id = lp.policy_id
        LEFT JOIN members m ON lp.member_id = m.member_id
        WHERE $whereClause
        ORDER BY lc.submitted_date DESC, lc.created_at DESC
        LIMIT 50";

$stmt = $conn->prepare($sql);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$claims = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'Life Insurance Claims';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Life Insurance Claims')); ?>
    <link rel="stylesheet" href="assets/css/pension-dashboard.css">
    <?php include 'layouts/head-css.php'; ?>
</head>

<body data-sidebar="dark">
    <div id="layout-wrapper">
        <?php include 'layouts/sidebar.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Header -->
                    <div class="welcome-header" style="background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Life Insurance Claims</h2>
                                <p>Process death, maturity, and surrender claims</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="process_claim.php" class="btn btn-primary">
                                <i class="ri-file-list-3-line me-2"></i>Process New Claim
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Claims List</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="mb-3">
                                        <a href="claims.php" class="btn btn-sm <?php echo empty($status) && empty($claim_type) ? 'btn-primary' : 'btn-outline-primary'; ?>">All</a>
                                        <a href="claims.php?status=PENDING" class="btn btn-sm <?php echo $status === 'PENDING' ? 'btn-warning' : 'btn-outline-warning'; ?>">Pending</a>
                                        <a href="claims.php?type=DEATH" class="btn btn-sm <?php echo $claim_type === 'DEATH' ? 'btn-danger' : 'btn-outline-danger'; ?>">Death</a>
                                        <a href="claims.php?type=MATURITY" class="btn btn-sm <?php echo $claim_type === 'MATURITY' ? 'btn-info' : 'btn-outline-info'; ?>">Maturity</a>
                                        <a href="surrender_requests.php" class="btn btn-sm btn-outline-secondary">Surrenders</a>
                                    </div>
                                    
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Submitted Date</th>
                                                    <th>Policy Number</th>
                                                    <th>Member</th>
                                                    <th>Claim Type</th>
                                                    <th>Claim Amount</th>
                                                    <th>Status</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($claims)): ?>
                                                    <tr><td colspan="7" class="text-center">No claims found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($claims as $claim): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($claim['submitted_date'])); ?></td>
                                                            <td><?php echo htmlspecialchars($claim['policy_number']); ?></td>
                                                            <td><?php echo htmlspecialchars($claim['member_number'] ? $claim['member_number'] . ' - ' . $claim['first_name'] . ' ' . $claim['last_name'] : 'N/A'); ?></td>
                                                            <td>
                                                                <span class="badge bg-<?php echo $claim['claim_type'] === 'DEATH' ? 'danger' : 'info'; ?>">
                                                                    <?php echo htmlspecialchars($claim['claim_type']); ?>
                                                                </span>
                                                            </td>
                                                            <td>KES <?php echo number_format($claim['claim_amount'], 2); ?></td>
                                                            <td>
                                                                <span class="badge bg-<?php 
                                                                    echo $claim['claim_status'] === 'PAID' ? 'success' : 
                                                                         ($claim['claim_status'] === 'APPROVED' ? 'info' : 
                                                                         ($claim['claim_status'] === 'REJECTED' ? 'danger' : 'warning')); 
                                                                ?>">
                                                                    <?php echo htmlspecialchars($claim['claim_status']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <a href="claim_details.php?id=<?php echo $claim['claim_id']; ?>" class="btn btn-sm btn-info">
                                                                    <i class="ri-eye-line"></i>
                                                                </a>
                                                                <?php if ($claim['claim_status'] === 'PENDING'): ?>
                                                                    <a href="process_claim.php?id=<?php echo $claim['claim_id']; ?>" class="btn btn-sm btn-primary">
                                                                        <i class="ri-check-line"></i>
                                                                    </a>
                                                                <?php endif; ?>
                                                            </td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
