<?php
/**
 * Retirement Requests
 * Manage retirement withdrawal requests
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('manage_retirement_requests');

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

$where = ["wr.withdrawal_type = 'RETIREMENT'"];
$params = [];
$types = '';

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

if ($scheme_id) {
    $where[] = "wr.scheme_id = ?";
    $params[] = $scheme_id;
    $types .= 'i';
}

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

$sql = "SELECT wr.*, m.member_number, m.first_name, m.last_name, ps.scheme_name
        FROM withdrawal_requests wr
        INNER JOIN members m ON wr.member_id = m.member_id
        LEFT JOIN pension_schemes ps ON wr.scheme_id = ps.scheme_id
        WHERE $whereClause
        ORDER BY wr.requested_date DESC, wr.created_at DESC";

$stmt = $conn->prepare($sql);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$requests = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$schemes = $conn->query("SELECT scheme_id, scheme_name FROM pension_schemes WHERE is_active = 1")->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'Retirement Requests';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Retirement Requests')); ?>
    <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, #10b981 0%, #059669 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Retirement Requests</h2>
                                <p>Manage retirement withdrawal requests</p>
                            </div>
                        </div>
                    </div>

                    <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="PENDING" <?php echo $status === 'PENDING' ? 'selected' : ''; ?>>Pending</option>
                                    <option value="APPROVED" <?php echo $status === 'APPROVED' ? 'selected' : ''; ?>>Approved</option>
                                    <option value="PROCESSED" <?php echo $status === 'PROCESSED' ? 'selected' : ''; ?>>Processed</option>
                                    <option value="REJECTED" <?php echo $status === 'REJECTED' ? 'selected' : ''; ?>>Rejected</option>
                                </select>
                                <select name="scheme_id" class="form-select" style="width: auto;">
                                    <option value="">All Schemes</option>
                                    <?php foreach ($schemes as $scheme): ?>
                                        <option value="<?php echo $scheme['scheme_id']; ?>" <?php echo $scheme_id == $scheme['scheme_id'] ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($scheme['scheme_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </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">Retirement Requests</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Request Date</th>
                                                    <th>Member</th>
                                                    <th>Scheme</th>
                                                    <th>Requested Amount</th>
                                                    <th>Calculated Amount</th>
                                                    <th>Status</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($requests)): ?>
                                                    <tr><td colspan="7" class="text-center">No retirement requests found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($requests as $req): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($req['requested_date'])); ?></td>
                                                            <td><?php echo htmlspecialchars($req['member_number'] . ' - ' . $req['first_name'] . ' ' . $req['last_name']); ?></td>
                                                            <td><?php echo htmlspecialchars($req['scheme_name'] ?? 'N/A'); ?></td>
                                                            <td>KES <?php echo number_format($req['requested_amount'], 2); ?></td>
                                                            <td>KES <?php echo number_format($req['calculated_amount'] ?? 0, 2); ?></td>
                                                            <td><span class="status-badge status-<?php echo strtolower($req['status']); ?>"><?php echo htmlspecialchars($req['status']); ?></span></td>
                                                            <td>
                                                                <a href="process_retirement.php?id=<?php echo $req['request_id']; ?>" class="btn btn-sm btn-primary">
                                                                    <i class="ri-settings-3-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>
