<?php
/**
 * Fund Return Reports
 * Generate fund return reports
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('view_fund_return_reports');

$scheme_id = $_GET['scheme_id'] ?? '';
$year = $_GET['year'] ?? date('Y');

$where = ["1=1"];
$params = [];
$types = '';

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

if ($year) {
    $where[] = "YEAR(fr.return_period) = ?";
    $params[] = $year;
    $types .= 'i';
}

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

$sql = "SELECT fr.*, ps.scheme_name
        FROM fund_returns fr
        LEFT JOIN pension_schemes ps ON fr.scheme_id = ps.scheme_id
        WHERE $whereClause
        ORDER BY fr.return_period DESC";

$stmt = $conn->prepare($sql);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$returns = $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 = 'Fund Return Reports';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Fund Return Reports')); ?>
    <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, #06b6d4 0%, #0891b2 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Fund Return Reports</h2>
                                <p>Generate fund return performance reports</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <form method="GET" class="d-inline-flex gap-2">
                                <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>
                                <input type="number" name="year" class="form-control" style="width: 120px;" value="<?php echo $year; ?>" placeholder="Year">
                                <button type="submit" class="btn btn-primary">Filter</button>
                                <button type="button" onclick="window.print()" class="btn btn-success">
                                    <i class="ri-printer-line me-2"></i>Print
                                </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">Fund Returns Report</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Period</th>
                                                    <th>Scheme</th>
                                                    <th>Return Rate (%)</th>
                                                    <th>Total Fund Value</th>
                                                    <th>Return Amount</th>
                                                    <th>Status</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($returns)): ?>
                                                    <tr><td colspan="6" class="text-center">No fund returns found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($returns as $return): ?>
                                                        <tr>
                                                            <td><?php echo date('M Y', strtotime($return['return_period'])); ?></td>
                                                            <td><?php echo htmlspecialchars($return['scheme_name'] ?? 'N/A'); ?></td>
                                                            <td><strong><?php echo number_format($return['return_rate'], 2); ?>%</strong></td>
                                                            <td>KES <?php echo number_format($return['total_fund_value'], 2); ?></td>
                                                            <td><strong>KES <?php echo number_format($return['return_amount'], 2); ?></strong></td>
                                                            <td><span class="status-badge status-<?php echo strtolower($return['status']); ?>"><?php echo htmlspecialchars($return['status']); ?></span></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>
