<?php
/**
 * Surrender Requests Management
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('view_surrender_requests');

$lifeService = new LifeInsuranceService($conn);
$status = $_GET['status'] ?? '';

// Build query
$where = ["1=1"];
$params = [];
$types = '';

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

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

$sql = "SELECT sr.*, lp.policy_number, lp.sum_assured, m.member_number, m.first_name, m.last_name
        FROM surrender_requests sr
        INNER JOIN life_policies lp ON sr.policy_id = lp.policy_id
        LEFT JOIN members m ON lp.member_id = m.member_id
        WHERE $whereClause
        ORDER BY sr.requested_date DESC, sr.created_at DESC
        LIMIT 50";

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

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Surrender 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">
                    <!-- Page Header -->
                    <div class="welcome-header" style="background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Surrender Requests</h2>
                                <p>Process early policy termination requests</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">Surrender Requests List</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="mb-3">
                                        <a href="surrender_requests.php" class="btn btn-sm <?php echo empty($status) ? 'btn-primary' : 'btn-outline-primary'; ?>">All</a>
                                        <a href="surrender_requests.php?status=PENDING" class="btn btn-sm <?php echo $status === 'PENDING' ? 'btn-warning' : 'btn-outline-warning'; ?>">Pending</a>
                                        <a href="surrender_requests.php?status=APPROVED" class="btn btn-sm <?php echo $status === 'APPROVED' ? 'btn-info' : 'btn-outline-info'; ?>">Approved</a>
                                        <a href="surrender_requests.php?status=PROCESSED" class="btn btn-sm <?php echo $status === 'PROCESSED' ? 'btn-success' : 'btn-outline-success'; ?>">Processed</a>
                                    </div>
                                    
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Request Date</th>
                                                    <th>Policy Number</th>
                                                    <th>Member</th>
                                                    <th>Policy Year</th>
                                                    <th>Premiums Paid</th>
                                                    <th>Surrender Value</th>
                                                    <th>Status</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($surrenders)): ?>
                                                    <tr><td colspan="8" class="text-center">No surrender requests found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($surrenders as $surrender): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($surrender['requested_date'])); ?></td>
                                                            <td><?php echo htmlspecialchars($surrender['policy_number']); ?></td>
                                                            <td><?php echo htmlspecialchars($surrender['member_number'] ? $surrender['member_number'] . ' - ' . $surrender['first_name'] . ' ' . $surrender['last_name'] : 'N/A'); ?></td>
                                                            <td>Year <?php echo $surrender['policy_year_at_surrender']; ?></td>
                                                            <td>KES <?php echo number_format($surrender['total_premiums_paid'], 2); ?></td>
                                                            <td>KES <?php echo number_format($surrender['calculated_surrender_value'], 2); ?></td>
                                                            <td>
                                                                <span class="badge bg-<?php 
                                                                    echo $surrender['status'] === 'PROCESSED' ? 'success' : 
                                                                         ($surrender['status'] === 'APPROVED' ? 'info' : 'warning'); 
                                                                ?>">
                                                                    <?php echo htmlspecialchars($surrender['status']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <a href="surrender_details.php?id=<?php echo $surrender['surrender_id']; ?>" class="btn btn-sm btn-info">
                                                                    <i class="ri-eye-line"></i>
                                                                </a>
                                                                <?php if ($surrender['status'] === 'PENDING'): ?>
                                                                    <a href="process_surrender.php?id=<?php echo $surrender['surrender_id']; ?>" class="btn btn-sm btn-primary">
                                                                        <i class="ri-check-line"></i>
                                                                    </a>
                                                                <?php endif; ?>
                                                            </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>
