<?php
/**
 * Bonuses Management
 * Manage life insurance bonus allocations
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('manage_bonuses');

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

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

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

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

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

$sql = "SELECT b.*, lp.policy_number, m.member_number, m.first_name, m.last_name
        FROM bonuses b
        INNER JOIN life_policies lp ON b.policy_id = lp.policy_id
        INNER JOIN members m ON lp.member_id = m.member_id
        WHERE $whereClause
        ORDER BY b.allocation_date DESC, b.created_at DESC";

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

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Bonuses 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">
                    <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>Bonuses Management</h2>
                                <p>Manage life insurance bonus allocations</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="allocate_bonus.php" class="btn btn-primary">
                                <i class="ri-gift-line me-2"></i>Allocate Bonus
                            </a>
                            <form method="GET" class="d-inline-flex gap-2 ms-2">
                                <input type="number" name="year" class="form-control" style="width: 120px;" 
                                       value="<?php echo $year; ?>" placeholder="Year">
                                <button type="submit" class="btn btn-secondary">Filter</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">Bonus Allocations</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Allocation Date</th>
                                                    <th>Policy</th>
                                                    <th>Member</th>
                                                    <th>Bonus Amount (KES)</th>
                                                    <th>Bonus Type</th>
                                                    <th>Policy Year</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($bonuses)): ?>
                                                    <tr><td colspan="7" class="text-center">No bonuses found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($bonuses as $bonus): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($bonus['allocation_date'])); ?></td>
                                                            <td><strong><?php echo htmlspecialchars($bonus['policy_number']); ?></strong></td>
                                                            <td><?php echo htmlspecialchars($bonus['member_number'] . ' - ' . $bonus['first_name'] . ' ' . $bonus['last_name']); ?></td>
                                                            <td><strong>KES <?php echo number_format($bonus['bonus_amount'], 2); ?></strong></td>
                                                            <td><span class="badge bg-info"><?php echo htmlspecialchars($bonus['bonus_type'] ?? 'REVERSIONARY'); ?></span></td>
                                                            <td><?php echo $bonus['policy_year'] ?? 'N/A'; ?></td>
                                                            <td>
                                                                <a href="bonus_details.php?id=<?php echo $bonus['bonus_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>
