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

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

// Require login and appropriate permissions
requireLogin();
requirePermission('view_schemes');

// Initialize services
$pensionService = new PensionService($conn);

// Handle actions
$action = $_GET['action'] ?? 'list';
$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($action === 'create') {
        $result = $pensionService->createScheme($_POST);
        if ($result['success']) {
            $message = $result['message'];
            $messageType = 'success';
            $action = 'list';
        } else {
            $message = $result['message'];
            $messageType = 'danger';
        }
    } elseif ($action === 'update') {
        $scheme_id = $_POST['scheme_id'] ?? 0;
        // Update logic here
        $message = 'Scheme updated successfully';
        $messageType = 'success';
        $action = 'list';
    }
}

// Get filters
$filters = [
    'scheme_type' => $_GET['scheme_type'] ?? '',
    'search' => $_GET['search'] ?? ''
];

// Get schemes list
$schemes = $pensionService->getSchemes($filters);

// Include dashboard header
$pageTitle = 'Pension Schemes';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Pension Schemes')); ?>
    <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, #10b981 0%, #059669 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Pension Schemes Management</h2>
                                <p>Manage and configure pension schemes (IPP, Umbrella, Occupational)</p>
                            </div>
                        </div>
                    </div>

                    <?php if ($message): ?>
                    <div class="alert alert-<?php echo $messageType; ?> alert-dismissible fade show" role="alert">
                        <?php echo htmlspecialchars($message); ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                    <?php endif; ?>

                    <!-- Action Buttons -->
                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="schemes.php?action=create" class="btn btn-primary">
                                <i class="ri-building-line me-2"></i>Add New Scheme
                            </a>
                        </div>
                    </div>

                    <?php if ($action === 'create' || $action === 'edit'): ?>
                        <!-- Scheme Form -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-card">
                                    <div class="page-card-header">
                                        <h4 class="page-card-title"><?php echo $action === 'create' ? 'Add New Scheme' : 'Edit Scheme'; ?></h4>
                                    </div>
                                    <div class="page-card-body">
                                        <form method="POST" id="scheme-form">
                                            <?php if ($action === 'edit'): ?>
                                                <input type="hidden" name="scheme_id" value="<?php echo $_GET['id'] ?? ''; ?>">
                                            <?php endif; ?>
                                            
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Scheme Code <span class="text-danger">*</span></label>
                                                    <input type="text" name="scheme_code" class="form-control" required>
                                                </div>
                                                
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Scheme Name <span class="text-danger">*</span></label>
                                                    <input type="text" name="scheme_name" class="form-control" required>
                                                </div>
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-md-4 mb-3">
                                                    <label class="form-label">Scheme Type <span class="text-danger">*</span></label>
                                                    <select name="scheme_type" class="form-select" required>
                                                        <option value="">Select Type</option>
                                                        <option value="OCCUPATIONAL">Occupational</option>
                                                        <option value="UMBRELLA">Umbrella</option>
                                                        <option value="INDIVIDUAL_PENSION_PLAN">Individual Pension Plan (IPP)</option>
                                                    </select>
                                                </div>
                                                
                                                <div class="col-md-4 mb-3">
                                                    <label class="form-label">Retirement Age <span class="text-danger">*</span></label>
                                                    <input type="number" name="retirement_age" class="form-control" value="60" required>
                                                </div>
                                                
                                                <div class="col-md-4 mb-3">
                                                    <label class="form-label">RBA Registration Number</label>
                                                    <input type="text" name="rba_registration_number" class="form-control">
                                                </div>
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-12 mb-3">
                                                    <h5 class="mb-3">Early Exit Rules</h5>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Max Cash Percentage (%)</label>
                                                    <input type="number" name="early_exit_max_cash_percentage" 
                                                           class="form-control" step="0.01" value="50.00" 
                                                           min="0" max="100">
                                                    <small class="text-muted">Occupational/Umbrella: 50%, IPP: up to 100%</small>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Balance Action</label>
                                                    <select name="early_exit_balance_action" class="form-select">
                                                        <option value="DEFER">Defer</option>
                                                        <option value="TRANSFER">Transfer</option>
                                                        <option value="ANNUITY">Annuity</option>
                                                    </select>
                                                </div>
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-12 mb-3">
                                                    <h5 class="mb-3">Retirement Withdrawal Rules</h5>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Commutation Percentage (%)</label>
                                                    <input type="number" name="retirement_commutation_percentage" 
                                                           class="form-control" step="0.01" 
                                                           min="0" max="100">
                                                    <small class="text-muted">Max % for lump sum at retirement</small>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Balance Action</label>
                                                    <select name="retirement_balance_action" class="form-select">
                                                        <option value="ANNUITY">Annuity</option>
                                                        <option value="PROGRAMMED_WITHDRAWAL">Programmed Withdrawal</option>
                                                        <option value="DEFER">Defer</option>
                                                    </select>
                                                </div>
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Investment Return Allocation Frequency</label>
                                                    <select name="investment_return_allocation_frequency" class="form-select">
                                                        <option value="MONTHLY">Monthly</option>
                                                        <option value="QUARTERLY" selected>Quarterly</option>
                                                        <option value="ANNUALLY">Annually</option>
                                                    </select>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Trust Deed Reference</label>
                                                    <input type="text" name="trust_deed_reference" class="form-control">
                                                </div>
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-12">
                                                    <button type="submit" class="btn btn-primary">
                                                        <i class="ri-save-line me-2"></i>Save Scheme
                                                    </button>
                                                    <a href="schemes.php" class="btn btn-secondary">Cancel</a>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php else: ?>
                        <!-- Schemes List -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-card">
                                    <div class="page-card-header">
                                        <h4 class="page-card-title">Schemes List</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <!-- Filters -->
                                        <form method="GET" class="mb-3">
                                            <div class="row">
                                                <div class="col-md-4 mb-2">
                                                    <input type="text" name="search" class="form-control" 
                                                           placeholder="Search schemes..." value="<?php echo htmlspecialchars($filters['search']); ?>">
                                                </div>
                                                <div class="col-md-3 mb-2">
                                                    <select name="scheme_type" class="form-select">
                                                        <option value="">All Types</option>
                                                        <option value="OCCUPATIONAL" <?php echo $filters['scheme_type'] === 'OCCUPATIONAL' ? 'selected' : ''; ?>>Occupational</option>
                                                        <option value="UMBRELLA" <?php echo $filters['scheme_type'] === 'UMBRELLA' ? 'selected' : ''; ?>>Umbrella</option>
                                                        <option value="INDIVIDUAL_PENSION_PLAN" <?php echo $filters['scheme_type'] === 'INDIVIDUAL_PENSION_PLAN' ? 'selected' : ''; ?>>IPP</option>
                                                    </select>
                                                </div>
                                                <div class="col-md-2 mb-2">
                                                    <button type="submit" class="btn btn-primary w-100">Filter</button>
                                                </div>
                                                <div class="col-md-2 mb-2">
                                                    <a href="schemes.php" class="btn btn-secondary w-100">Reset</a>
                                                </div>
                                            </div>
                                        </form>
                                        
                                        <!-- Schemes Table -->
                                        <div class="table-responsive">
                                            <table class="data-table">
                                                <thead>
                                                    <tr>
                                                        <th>Scheme Code</th>
                                                        <th>Scheme Name</th>
                                                        <th>Type</th>
                                                        <th>Retirement Age</th>
                                                        <th>Members</th>
                                                        <th>Employers</th>
                                                        <th>RBA Number</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php if (empty($schemes)): ?>
                                                        <tr>
                                                            <td colspan="8" class="text-center">No schemes found</td>
                                                        </tr>
                                                    <?php else: ?>
                                                        <?php foreach ($schemes as $scheme): ?>
                                                            <tr>
                                                                <td><?php echo htmlspecialchars($scheme['scheme_code']); ?></td>
                                                                <td><?php echo htmlspecialchars($scheme['scheme_name']); ?></td>
                                                                <td>
                                                                    <span class="badge bg-info">
                                                                        <?php echo htmlspecialchars($scheme['scheme_type']); ?>
                                                                    </span>
                                                                </td>
                                                                <td><?php echo $scheme['retirement_age']; ?> years</td>
                                                                <td><?php echo $scheme['total_members'] ?? 0; ?></td>
                                                                <td><?php echo $scheme['total_employers'] ?? 0; ?></td>
                                                                <td><?php echo htmlspecialchars($scheme['rba_registration_number'] ?? 'N/A'); ?></td>
                                                                <td>
                                                                    <a href="scheme_details.php?id=<?php echo $scheme['scheme_id']; ?>" 
                                                                       class="btn btn-sm btn-info">
                                                                        <i class="ri-eye-line"></i>
                                                                    </a>
                                                                    <a href="schemes.php?action=edit&id=<?php echo $scheme['scheme_id']; ?>" 
                                                                       class="btn btn-sm btn-primary">
                                                                        <i class="ri-edit-line"></i>
                                                                    </a>
                                                                    <a href="scheme_rules.php?id=<?php echo $scheme['scheme_id']; ?>" 
                                                                       class="btn btn-sm btn-success">
                                                                        <i class="ri-settings-3-line"></i>
                                                                    </a>
                                                                </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>
