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

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

requireLogin();
requirePermission('view_fund_returns');

$pensionService = new PensionService($conn);
$action = $_GET['action'] ?? 'list';
$message = '';
$messageType = '';

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

// Get fund returns
$returns = $conn->query("SELECT fr.*, ps.scheme_name FROM fund_returns fr INNER JOIN pension_schemes ps ON fr.scheme_id = ps.scheme_id ORDER BY fr.allocation_date DESC LIMIT 50")->fetch_all(MYSQLI_ASSOC);
$schemes = $conn->query("SELECT scheme_id, scheme_name FROM pension_schemes WHERE is_active = 1")->fetch_all(MYSQLI_ASSOC);

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Fund Returns 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">
                    <!-- 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>Fund Returns Management</h2>
                                <p>Allocate 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 mb-3">
                        <div class="col-12">
                            <a href="allocate_fund_returns.php" class="btn btn-primary">
                                <i class="ri-line-chart-line me-2"></i>Allocate Returns
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Fund Returns History</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Scheme</th>
                                                    <th>Period</th>
                                                    <th>Return %</th>
                                                    <th>Return Amount (KES)</th>
                                                    <th>Allocation Date</th>
                                                    <th>Status</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($returns)): ?>
                                                    <tr><td colspan="7" class="text-center">No fund returns found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($returns as $return): ?>
                                                        <tr>
                                                            <td><?php echo htmlspecialchars($return['scheme_name']); ?></td>
                                                            <td><?php echo date('M Y', strtotime($return['return_period_start'])) . ' - ' . date('M Y', strtotime($return['return_period_end'])); ?></td>
                                                            <td><?php echo number_format($return['return_percentage'], 2); ?>%</td>
                                                            <td>KES <?php echo number_format($return['return_amount'], 2); ?></td>
                                                            <td><?php echo date('d M Y', strtotime($return['allocation_date'])); ?></td>
                                                            <td>
                                                                <span class="badge bg-<?php echo $return['status'] === 'ALLOCATED' ? 'success' : 'warning'; ?>">
                                                                    <?php echo htmlspecialchars($return['status']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <a href="fund_return_details.php?id=<?php echo $return['return_id']; ?>" class="btn btn-sm btn-info">
                                                                    <i class="ri-eye-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>
