<?php
/**
 * File Claim
 * Member self-service claim filing
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
if (!isMember()) {
    header('Location: access_denied.php');
    exit();
}

$member_id = $_SESSION['member_id'] ?? $_SESSION['user_id'];
$message = '';
$messageType = '';

// Get member's active policies
$policies = $conn->query("SELECT lp.*, lip.product_name
                         FROM life_policies lp
                         LEFT JOIN life_insurance_products lip ON lp.product_id = lip.product_id
                         WHERE lp.member_id = $member_id AND lp.policy_status = 'ACTIVE'")->fetch_all(MYSQLI_ASSOC);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $policy_id = $_POST['policy_id'];
    $claim_type = $_POST['claim_type'];
    $claim_amount = $_POST['claim_amount'] ?? null;
    $incident_date = $_POST['incident_date'] ?? null;
    $description = $_POST['description'] ?? null;
    
    $sql = "INSERT INTO life_claims (policy_id, member_id, claim_type, claim_amount, incident_date, 
                                    claim_description, submitted_date, claim_status)
            VALUES (?, ?, ?, ?, ?, ?, CURDATE(), 'PENDING')";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("iissds",
        $policy_id,
        $member_id,
        $claim_type,
        $claim_amount,
        $incident_date,
        $description
    );
    
    if ($stmt->execute()) {
        $message = 'Claim filed successfully. Your claim is under review.';
        $messageType = 'success';
    } else {
        $message = 'Failed to file claim';
        $messageType = 'danger';
    }
}

$pageTitle = 'File Claim';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'File Claim')); ?>
    <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">
                    <div class="welcome-header" style="background: linear-gradient(135deg, #ec4899 0%, #db2777 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>File Life Insurance Claim</h2>
                                <p>Submit a claim for your life insurance policy</p>
                            </div>
                        </div>
                    </div>

                    <?php if ($message): ?>
                    <div class="alert alert-<?php echo $messageType; ?> alert-dismissible fade show">
                        <?php echo htmlspecialchars($message); ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                    <?php endif; ?>

                    <?php if (empty($policies)): ?>
                        <div class="alert alert-warning">
                            <i class="ri-alert-line me-2"></i>
                            You have no active life insurance policies. Please contact the administrator to purchase a policy.
                        </div>
                    <?php else: ?>
                        <div class="row">
                            <div class="col-md-8">
                                <div class="page-card">
                                    <div class="page-card-header">
                                        <h4 class="page-card-title">Claim Information</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <form method="POST">
                                            <div class="mb-3">
                                                <label class="form-label">Policy <span class="text-danger">*</span></label>
                                                <select name="policy_id" class="form-select" required>
                                                    <option value="">Select Policy</option>
                                                    <?php foreach ($policies as $policy): ?>
                                                        <option value="<?php echo $policy['policy_id']; ?>">
                                                            <?php echo htmlspecialchars($policy['policy_number'] . ' - ' . ($policy['product_name'] ?? 'N/A') . ' (KES ' . number_format($policy['sum_assured'], 0) . ')'); ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </div>
                                            
                                            <div class="mb-3">
                                                <label class="form-label">Claim Type <span class="text-danger">*</span></label>
                                                <select name="claim_type" class="form-select" required>
                                                    <option value="">Select Type</option>
                                                    <option value="DEATH">Death</option>
                                                    <option value="MATURITY">Maturity</option>
                                                    <option value="DISABILITY">Disability</option>
                                                    <option value="CRITICAL_ILLNESS">Critical Illness</option>
                                                </select>
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Claim Amount (KES)</label>
                                                    <input type="number" name="claim_amount" class="form-control" step="0.01" min="0">
                                                    <small class="text-muted">Leave blank for automatic calculation</small>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Incident Date</label>
                                                    <input type="date" name="incident_date" class="form-control">
                                                </div>
                                            </div>
                                            
                                            <div class="mb-3">
                                                <label class="form-label">Description</label>
                                                <textarea name="description" class="form-control" rows="4" 
                                                          placeholder="Provide details about the claim..."></textarea>
                                            </div>
                                            
                                            <div class="alert alert-info">
                                                <i class="ri-information-line me-2"></i>
                                                <strong>Note:</strong> You may be required to submit supporting documents. 
                                                A claims officer will contact you for additional information.
                                            </div>
                                            
                                            <button type="submit" class="btn btn-primary">
                                                <i class="ri-send-plane-line me-2"></i>Submit Claim
                                            </button>
                                            <a href="my_policies.php" class="btn btn-secondary">Cancel</a>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
