<?php
/**
 * My Premiums
 * Member's premium payment history
 * 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'];

// Get member's policies
$policies = $conn->query("SELECT lp.policy_id, lp.policy_number, 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")->fetch_all(MYSQLI_ASSOC);

$policy_ids = array_column($policies, 'policy_id');
$premiums = [];

if (!empty($policy_ids)) {
    $placeholders = implode(',', array_fill(0, count($policy_ids), '?'));
    $sql = "SELECT pr.*, lp.policy_number, lip.product_name
            FROM premiums pr
            INNER JOIN life_policies lp ON pr.policy_id = lp.policy_id
            LEFT JOIN life_insurance_products lip ON lp.product_id = lip.product_id
            WHERE pr.policy_id IN ($placeholders)
            ORDER BY pr.premium_due_date DESC, pr.created_at DESC";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param(str_repeat('i', count($policy_ids)), ...$policy_ids);
    $stmt->execute();
    $premiums = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Premiums')); ?>
    <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 Premium Payments</h2>
                                <p>View your life insurance premium payment history</p>
                            </div>
                        </div>
                    </div>

                    <?php if (empty($policies)): ?>
                        <div class="alert alert-info">
                            <i class="ri-information-line me-2"></i>
                            You have no life insurance policies. <a href="policies.php">View available policies</a>
                        </div>
                    <?php else: ?>
                        <div class="row">
                            <div class="col-12">
                                <div class="page-card">
                                    <div class="page-card-header">
                                        <h4 class="page-card-title">Premium Payment History</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <div class="table-responsive">
                                            <table class="data-table">
                                                <thead>
                                                    <tr>
                                                        <th>Policy</th>
                                                        <th>Product</th>
                                                        <th>Due Date</th>
                                                        <th>Amount (KES)</th>
                                                        <th>Payment Status</th>
                                                        <th>Payment Date</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php if (empty($premiums)): ?>
                                                        <tr><td colspan="7" class="text-center">No premium payments found</td></tr>
                                                    <?php else: ?>
                                                        <?php foreach ($premiums as $premium): ?>
                                                            <tr>
                                                                <td><strong><?php echo htmlspecialchars($premium['policy_number']); ?></strong></td>
                                                                <td><?php echo htmlspecialchars($premium['product_name'] ?? 'N/A'); ?></td>
                                                                <td><?php echo date('d M Y', strtotime($premium['premium_due_date'])); ?></td>
                                                                <td><strong>KES <?php echo number_format($premium['premium_amount'], 2); ?></strong></td>
                                                                <td>
                                                                    <span class="status-badge status-<?php echo strtolower($premium['payment_status']); ?>">
                                                                        <?php echo htmlspecialchars($premium['payment_status']); ?>
                                                                    </span>
                                                                </td>
                                                                <td><?php echo $premium['payment_date'] ? date('d M Y', strtotime($premium['payment_date'])) : 'N/A'; ?></td>
                                                                <td>
                                                                    <?php if ($premium['payment_status'] === 'PENDING' || $premium['payment_status'] === 'OVERDUE'): ?>
                                                                        <a href="pay_premium.php?id=<?php echo $premium['premium_id']; ?>" class="btn btn-sm btn-success">
                                                                            <i class="ri-bank-card-line"></i> Pay
                                                                        </a>
                                                                    <?php endif; ?>
                                                                </td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    <?php endif; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
