<?php
/**
 * Payouts Management
 * Unified payout system for pension and life insurance
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('view_payouts');

$status = $_GET['status'] ?? '';
$payout_type = $_GET['type'] ?? '';

// Build query
$where = ["1=1"];
$params = [];
$types = '';

if (!empty($status)) {
    $where[] = "p.payout_status = ?";
    $params[] = $status;
    $types .= 's';
}

if (!empty($payout_type)) {
    $where[] = "p.payout_type = ?";
    $params[] = $payout_type;
    $types .= 's';
}

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

$sql = "SELECT p.*, m.member_number, m.first_name, m.last_name
        FROM payouts p
        LEFT JOIN members m ON p.member_id = m.member_id
        WHERE $whereClause
        ORDER BY p.created_at DESC
        LIMIT 50";

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

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Payouts 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, #14b8a6 0%, #0d9488 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Payouts Management</h2>
                                <p>Process and track all system payouts</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="process_payout.php" class="btn btn-primary">
                                <i class="ri-secure-payment-line me-2"></i>Process New Payout
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Payouts List</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="mb-3">
                                        <a href="payouts.php" class="btn btn-sm <?php echo empty($status) && empty($payout_type) ? 'btn-primary' : 'btn-outline-primary'; ?>">All</a>
                                        <a href="payouts.php?status=PENDING" class="btn btn-sm <?php echo $status === 'PENDING' ? 'btn-warning' : 'btn-outline-warning'; ?>">Pending</a>
                                        <a href="payouts.php?type=PENSION_WITHDRAWAL" class="btn btn-sm <?php echo $payout_type === 'PENSION_WITHDRAWAL' ? 'btn-info' : 'btn-outline-info'; ?>">Pension</a>
                                        <a href="payouts.php?type=LIFE_CLAIM" class="btn btn-sm <?php echo $payout_type === 'LIFE_CLAIM' ? 'btn-success' : 'btn-outline-success'; ?>">Life Claims</a>
                                    </div>
                                    
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Date</th>
                                                    <th>Type</th>
                                                    <th>Member</th>
                                                    <th>Amount (KES)</th>
                                                    <th>Payment Method</th>
                                                    <th>Status</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($payouts)): ?>
                                                    <tr><td colspan="7" class="text-center">No payouts found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($payouts as $payout): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($payout['created_at'])); ?></td>
                                                            <td>
                                                                <span class="badge bg-info">
                                                                    <?php echo htmlspecialchars(str_replace('_', ' ', $payout['payout_type'])); ?>
                                                                </span>
                                                            </td>
                                                            <td><?php echo htmlspecialchars($payout['member_number'] ? $payout['member_number'] . ' - ' . $payout['first_name'] . ' ' . $payout['last_name'] : 'N/A'); ?></td>
                                                            <td>KES <?php echo number_format($payout['payout_amount'], 2); ?></td>
                                                            <td><?php echo htmlspecialchars(str_replace('_', ' ', $payout['payment_method'])); ?></td>
                                                            <td>
                                                                <span class="badge bg-<?php 
                                                                    echo $payout['payout_status'] === 'COMPLETED' ? 'success' : 
                                                                         ($payout['payout_status'] === 'PROCESSING' ? 'info' : 
                                                                         ($payout['payout_status'] === 'FAILED' ? 'danger' : 'warning')); 
                                                                ?>">
                                                                    <?php echo htmlspecialchars($payout['payout_status']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <a href="payout_details.php?id=<?php echo $payout['payout_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>
