<?php
/**
 * My Policies
 * Member's life insurance policies
 * 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'];

$policies = $conn->query("SELECT lp.*, lip.product_name, lip.product_type 
                         FROM life_policies lp
                         LEFT JOIN life_insurance_products lip ON lp.product_id = lip.product_id
                         WHERE lp.member_id = $member_id
                         ORDER BY lp.created_at DESC")->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'My Policies';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Policies')); ?>
    <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>My Life Insurance Policies</h2>
                                <p>View your life insurance policies</p>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Policies List</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Policy Number</th>
                                                    <th>Product</th>
                                                    <th>Sum Assured</th>
                                                    <th>Premium</th>
                                                    <th>Status</th>
                                                    <th>Start Date</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($policies)): ?>
                                                    <tr><td colspan="7" class="text-center">No policies found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($policies as $policy): ?>
                                                        <tr>
                                                            <td><strong><?php echo htmlspecialchars($policy['policy_number']); ?></strong></td>
                                                            <td><?php echo htmlspecialchars($policy['product_name'] ?? 'N/A'); ?></td>
                                                            <td>KES <?php echo number_format($policy['sum_assured'], 2); ?></td>
                                                            <td>KES <?php echo number_format($policy['premium_amount'], 2); ?></td>
                                                            <td><span class="status-badge status-<?php echo strtolower($policy['policy_status']); ?>"><?php echo htmlspecialchars($policy['policy_status']); ?></span></td>
                                                            <td><?php echo date('d M Y', strtotime($policy['policy_start_date'])); ?></td>
                                                            <td>
                                                                <a href="policy_details.php?id=<?php echo $policy['policy_id']; ?>" class="btn btn-sm btn-info">
                                                                    <i class="ri-eye-line"></i>
                                                                </a>
                                                            </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>
