<?php
/**
 * Annuity Management
 * Manage retirement annuities and programmed withdrawals
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('manage_annuities');

$status = $_GET['status'] ?? '';

$where = ["1=1"];
$params = [];
$types = '';

if ($status) {
    $where[] = "a.status = ?";
    $params[] = $status;
    $types .= 's';
}

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

// Note: This assumes an annuities table exists. If not, this would need to be created.
// For now, we'll show a placeholder structure.
$sql = "SELECT a.*, m.member_number, m.first_name, m.last_name, ps.scheme_name
        FROM annuities a
        INNER JOIN members m ON a.member_id = m.member_id
        LEFT JOIN pension_schemes ps ON m.scheme_id = ps.scheme_id
        WHERE $whereClause
        ORDER BY a.start_date DESC
        LIMIT 100";

// Check if annuities table exists, if not show message
$tableExists = $conn->query("SHOW TABLES LIKE 'annuities'")->num_rows > 0;

$pageTitle = 'Annuity Management';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Annuity Management')); ?>
    <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, #06b6d4 0%, #0891b2 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Annuity Management</h2>
                                <p>Manage retirement annuities and programmed withdrawals</p>
                            </div>
                        </div>
                    </div>

                    <?php if (!$tableExists): ?>
                        <div class="alert alert-warning">
                            <i class="ri-alert-line me-2"></i>
                            <strong>Note:</strong> The annuities table needs to be created in the database. 
                            This feature will be available once the table is set up.
                        </div>
                    <?php else: ?>
                        <div class="row mb-3">
                            <div class="col-12">
                                <form method="GET" class="d-inline-flex gap-2">
                                    <select name="status" class="form-select" style="width: auto;">
                                        <option value="">All Status</option>
                                        <option value="ACTIVE" <?php echo $status === 'ACTIVE' ? 'selected' : ''; ?>>Active</option>
                                        <option value="SUSPENDED" <?php echo $status === 'SUSPENDED' ? 'selected' : ''; ?>>Suspended</option>
                                        <option value="COMPLETED" <?php echo $status === 'COMPLETED' ? 'selected' : ''; ?>>Completed</option>
                                    </select>
                                    <button type="submit" class="btn btn-primary">Filter</button>
                                </form>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-12">
                                <div class="page-card">
                                    <div class="page-card-header">
                                        <h4 class="page-card-title">Annuities List</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <div class="table-responsive">
                                            <table class="data-table">
                                                <thead>
                                                    <tr>
                                                        <th>Member</th>
                                                        <th>Scheme</th>
                                                        <th>Monthly Amount</th>
                                                        <th>Start Date</th>
                                                        <th>End Date</th>
                                                        <th>Status</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td colspan="7" class="text-center">No annuities found</td>
                                                    </tr>
                                                </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>
