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

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

requireLogin();
requirePermission('view_withdrawals');

$action = $_GET['action'] ?? 'list';
$status = $_GET['status'] ?? '';

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

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

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

$sql = "SELECT wr.*, m.member_number, m.first_name, m.last_name, ps.scheme_name
        FROM withdrawal_requests wr
        INNER JOIN members m ON wr.member_id = m.member_id
        LEFT JOIN pension_schemes ps ON wr.scheme_id = ps.scheme_id
        WHERE $whereClause
        ORDER BY wr.requested_date DESC, wr.created_at DESC
        LIMIT 50";

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

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Withdrawals 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, #8b5cf6 0%, #7c3aed 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Withdrawals Management</h2>
                                <p>Process and manage pension withdrawal requests</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="process_withdrawal.php" class="btn btn-primary">
                                <i class="ri-money-dollar-circle-line me-2"></i>Process New Withdrawal
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Withdrawals List</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="mb-3">
                                        <a href="withdrawals.php" class="btn btn-sm <?php echo empty($status) ? 'btn-primary' : 'btn-outline-primary'; ?>">All</a>
                                        <a href="withdrawals.php?status=PENDING" class="btn btn-sm <?php echo $status === 'PENDING' ? 'btn-warning' : 'btn-outline-warning'; ?>">Pending</a>
                                        <a href="withdrawals.php?status=APPROVED" class="btn btn-sm <?php echo $status === 'APPROVED' ? 'btn-info' : 'btn-outline-info'; ?>">Approved</a>
                                        <a href="withdrawals.php?status=PROCESSED" class="btn btn-sm <?php echo $status === 'PROCESSED' ? 'btn-success' : 'btn-outline-success'; ?>">Processed</a>
                                    </div>
                                    
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Request Date</th>
                                                    <th>Member</th>
                                                    <th>Type</th>
                                                    <th>Requested Amount</th>
                                                    <th>Calculated Amount</th>
                                                    <th>Status</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($withdrawals)): ?>
                                                    <tr><td colspan="7" class="text-center">No withdrawals found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($withdrawals as $withdrawal): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($withdrawal['requested_date'])); ?></td>
                                                            <td><?php echo htmlspecialchars($withdrawal['member_number'] . ' - ' . $withdrawal['first_name'] . ' ' . $withdrawal['last_name']); ?></td>
                                                            <td>
                                                                <span class="badge bg-info">
                                                                    <?php echo htmlspecialchars($withdrawal['withdrawal_type']); ?>
                                                                </span>
                                                            </td>
                                                            <td>KES <?php echo number_format($withdrawal['requested_amount'], 2); ?></td>
                                                            <td>KES <?php echo number_format($withdrawal['calculated_amount'] ?? 0, 2); ?></td>
                                                            <td>
                                                                <span class="badge bg-<?php 
                                                                    echo $withdrawal['status'] === 'PROCESSED' ? 'success' : 
                                                                         ($withdrawal['status'] === 'APPROVED' ? 'info' : 
                                                                         ($withdrawal['status'] === 'REJECTED' ? 'danger' : 'warning')); 
                                                                ?>">
                                                                    <?php echo htmlspecialchars($withdrawal['status']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <a href="withdrawal_details.php?id=<?php echo $withdrawal['request_id']; ?>" class="btn btn-sm btn-info">
                                                                    <i class="ri-eye-line"></i>
                                                                </a>
                                                                <?php if ($withdrawal['status'] === 'PENDING'): ?>
                                                                    <a href="process_withdrawal.php?id=<?php echo $withdrawal['request_id']; ?>" class="btn btn-sm btn-primary">
                                                                        <i class="ri-check-line"></i>
                                                                    </a>
                                                                <?php endif; ?>
                                                            </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>
