<?php
/**
 * My Withdrawals
 * Member's withdrawal request history
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
if (!isMember()) {
    header('Location: access_denied.php');
    exit();
}

$member_id = $_SESSION['member_id'] ?? $_SESSION['user_id'];

$withdrawals = $conn->query("SELECT * FROM withdrawal_requests WHERE member_id = $member_id ORDER BY requested_date DESC, created_at DESC")->fetch_all(MYSQLI_ASSOC);

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

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

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="request_withdrawal.php" class="btn btn-primary">
                                <i class="ri-money-dollar-circle-line me-2"></i>Request 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">Withdrawal History</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Request Date</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="6" class="text-center">No withdrawal requests found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($withdrawals as $withdrawal): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($withdrawal['requested_date'])); ?></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="status-badge status-<?php echo strtolower($withdrawal['status']); ?>"><?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>
                                                            </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>
