<?php
/**
 * Allocate Fund Returns
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('allocate_fund_returns');

$pensionService = new PensionService($conn);
$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $result = $pensionService->allocateFundReturns($_POST);
    if ($result['success']) {
        $message = $result['message'];
        $messageType = 'success';
    } else {
        $message = $result['message'];
        $messageType = 'danger';
    }
}

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

$pageTitle = 'Allocate Fund Returns';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Allocate Fund Returns')); ?>
    <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, #ec4899 0%, #db2777 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Allocate Fund Returns</h2>
                                <p>Distribute investment returns to member accounts</p>
                            </div>
                        </div>
                    </div>

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

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Allocate Investment Returns to Members</h4>
                                </div>
                                <div class="page-card-body">
                                    <form method="POST" id="allocate-form">
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label class="form-label">Scheme <span class="text-danger">*</span></label>
                                                <select name="scheme_id" id="scheme_id" class="form-select" required>
                                                    <option value="">Select Scheme</option>
                                                    <?php foreach ($schemes as $scheme): ?>
                                                        <option value="<?php echo $scheme['scheme_id']; ?>">
                                                            <?php echo htmlspecialchars($scheme['scheme_name']); ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <label class="form-label">Allocation Frequency</label>
                                                <select name="allocation_frequency" class="form-select">
                                                    <option value="MONTHLY">Monthly</option>
                                                    <option value="QUARTERLY" selected>Quarterly</option>
                                                    <option value="ANNUALLY">Annually</option>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-4 mb-3">
                                                <label class="form-label">Return Period Start <span class="text-danger">*</span></label>
                                                <input type="date" name="return_period_start" class="form-control" required>
                                            </div>
                                            <div class="col-md-4 mb-3">
                                                <label class="form-label">Return Period End <span class="text-danger">*</span></label>
                                                <input type="date" name="return_period_end" class="form-control" required>
                                            </div>
                                            <div class="col-md-4 mb-3">
                                                <label class="form-label">Return Percentage (%) <span class="text-danger">*</span></label>
                                                <input type="number" name="return_percentage" class="form-control" step="0.0001" required>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <label class="form-label">Total Return Amount (KES) <span class="text-danger">*</span></label>
                                                <input type="number" name="return_amount" class="form-control" step="0.01" required>
                                                <small class="text-muted">Total amount to be allocated to all members in the scheme</small>
                                            </div>
                                        </div>
                                        
                                        <div class="alert alert-info">
                                            <i class="ri-information-line me-2"></i>
                                            <strong>Note:</strong> Returns will be allocated proportionally to members based on their accrued benefits. 
                                            The system will automatically calculate each member's share.
                                        </div>
                                        
                                        <button type="submit" class="btn btn-primary">
                                            <i class="ri-save-line me-2"></i>Allocate Returns
                                        </button>
                                        <a href="fund_returns.php" class="btn btn-secondary">Cancel</a>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
