<?php
/**
 * My Beneficiaries
 * Member's beneficiaries list
 * 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'];

$beneficiaries = $conn->query("SELECT * FROM beneficiaries WHERE member_id = $member_id ORDER BY created_at DESC")->fetch_all(MYSQLI_ASSOC);

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Beneficiaries')); ?>
    <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, #2563eb 0%, #1e40af 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>My Beneficiaries</h2>
                                <p>Manage your beneficiaries and nominees</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="add_beneficiary.php" class="btn btn-primary">
                                <i class="ri-user-add-line me-2"></i>Add Beneficiary
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Beneficiaries List</h4>
                                </div>
                                <div class="page-card-body">
                                    <?php if (empty($beneficiaries)): ?>
                                        <div class="alert alert-info">
                                            <i class="ri-information-line me-2"></i>
                                            No beneficiaries registered. <a href="add_beneficiary.php">Add a beneficiary</a> to ensure your benefits are properly allocated.
                                        </div>
                                    <?php else: ?>
                                        <div class="table-responsive">
                                            <table class="data-table">
                                                <thead>
                                                    <tr>
                                                        <th>Name</th>
                                                        <th>Relationship</th>
                                                        <th>Allocation %</th>
                                                        <th>National ID</th>
                                                        <th>Phone</th>
                                                        <th>Date Added</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php foreach ($beneficiaries as $ben): ?>
                                                        <tr>
                                                            <td><strong><?php echo htmlspecialchars($ben['first_name'] . ' ' . $ben['last_name']); ?></strong></td>
                                                            <td><?php echo htmlspecialchars($ben['relationship']); ?></td>
                                                            <td><?php echo number_format($ben['allocation_percentage'], 2); ?>%</td>
                                                            <td><?php echo htmlspecialchars($ben['national_id'] ?? 'N/A'); ?></td>
                                                            <td><?php echo htmlspecialchars($ben['phone_number'] ?? 'N/A'); ?></td>
                                                            <td><?php echo date('d M Y', strtotime($ben['created_at'])); ?></td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
