<?php
session_start();
require_once '../includes/db.php';
require_once '../includes/functions.php';
require_once '../includes/session_manager.php';

redirectIfNotLoggedIn();

$userId = $_SESSION['user_id'];
$error = '';
$success = '';

// Get user's pending withdrawals
$pendingWithdrawals = $pdo->prepare("
    SELECT pt.*, a.account_number, a.balance 
    FROM pending_transactions pt
    JOIN accounts a ON pt.account_id = a.account_id
    WHERE pt.user_id = ? AND pt.transaction_type = 'withdrawal' 
    AND pt.sms_step NOT IN ('approved', 'rejected', 'cancelled')
    ORDER BY pt.created_at DESC
");
$pendingWithdrawals->execute([$userId]);
$pendingWithdrawals = $pendingWithdrawals->fetchAll();

// Get user's completed withdrawals
$completedWithdrawals = $pdo->prepare("
    SELECT pt.*, a.account_number 
    FROM pending_transactions pt
    JOIN accounts a ON pt.account_id = a.account_id
    WHERE pt.user_id = ? AND pt.transaction_type = 'withdrawal' 
    AND pt.sms_step IN ('approved', 'rejected')
    ORDER BY pt.created_at DESC 
    LIMIT 20
");
$completedWithdrawals->execute([$userId]);
$completedWithdrawals = $completedWithdrawals->fetchAll();

// Get user info for sidebar
$stmt = $pdo->prepare("SELECT u.*, a.account_number, a.balance FROM users u JOIN accounts a ON u.user_id = a.user_id WHERE u.user_id = ?");
$stmt->execute([$userId]);
$user = $stmt->fetch();
$profilePic = $user['profile_picture'] ? '../uploads/' . $user['profile_picture'] : '../assets/images/default-avatars.png';
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title>Pending Withdrawals - Oakshire Finance</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;400;500;600;700;800&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --primary-navy: #0a2540;
            --primary-dark: #0a1c2f;
            --primary-blue: #1a4d8c;
            --success-green: #2e7d32;
            --error-red: #dc3545;
            --warning-orange: #f39c12;
            --info-blue: #17a2b8;
            --gray-50: #f8f9fc;
            --gray-100: #f1f3f9;
            --gray-200: #e9ecef;
            --gray-600: #6c757d;
            --gray-700: #495057;
            --white: #ffffff;
            --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
            --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.05);
            --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: var(--gray-50);
            color: var(--gray-700);
        }

        .dashboard-wrapper {
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 280px;
            background: var(--white);
            border-right: 1px solid var(--gray-200);
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
            overflow-y: auto;
            transition: transform 0.3s ease;
            z-index: 100;
        }

        .sidebar-header {
            padding: 24px;
            border-bottom: 1px solid var(--gray-200);
            text-align: center;
        }

        .logo {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
        }

        .logo-icon {
            width: 40px;
            height: 40px;
            background: var(--primary-navy);
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon i {
            font-size: 20px;
            color: white;
        }

        .logo-text {
            font-size: 22px;
            font-weight: 700;
            color: var(--primary-navy);
        }

        .profile-section {
            padding: 24px;
            text-align: center;
            border-bottom: 1px solid var(--gray-200);
        }

        .profile-avatar {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            margin: 0 auto 12px;
            background: linear-gradient(135deg, var(--primary-navy), var(--primary-blue));
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
        }

        .profile-avatar img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .profile-section h5 {
            font-size: 16px;
            font-weight: 600;
            margin-bottom: 4px;
        }

        .profile-section p {
            font-size: 12px;
            color: var(--gray-500);
        }

        .nav-menu {
            padding: 16px;
        }

        .nav-item {
            margin-bottom: 4px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            border-radius: 12px;
            text-decoration: none;
            color: var(--gray-700);
            transition: var(--transition);
            font-weight: 500;
            font-size: 14px;
        }

        .nav-link i {
            width: 20px;
            font-size: 18px;
        }

        .nav-link:hover {
            background: var(--gray-100);
            color: var(--primary-blue);
        }

        .nav-link.active {
            background: var(--primary-navy);
            color: white;
        }

        .logout-section {
            padding: 16px;
            border-top: 1px solid var(--gray-200);
            margin-top: auto;
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            background: var(--white);
        }

        .logout-btn {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            border-radius: 12px;
            text-decoration: none;
            color: var(--error-red);
            font-weight: 500;
        }

        .main-content {
            flex: 1;
            margin-left: 280px;
            min-height: 100vh;
        }

        .top-header {
            background: var(--white);
            border-bottom: 1px solid var(--gray-200);
            padding: 16px 32px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 99;
        }

        .page-title h1 {
            font-size: 24px;
            font-weight: 700;
            color: var(--gray-700);
        }

        .content-area {
            padding: 32px;
        }

        .card {
            background: var(--white);
            border-radius: 20px;
            border: 1px solid var(--gray-200);
            padding: 24px;
            margin-bottom: 24px;
        }

        .card h2 {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .table-container {
            overflow-x: auto;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            min-width: 600px;
        }

        .data-table th,
        .data-table td {
            padding: 16px 20px;
            text-align: left;
            border-bottom: 1px solid var(--gray-200);
        }

        .data-table th {
            background: var(--gray-50);
            font-weight: 600;
            font-size: 12px;
            text-transform: uppercase;
            color: var(--gray-600);
        }

        .data-table td {
            font-size: 13px;
        }

        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 11px;
            font-weight: 600;
        }

        .status-pending {
            background: rgba(243, 156, 18, 0.1);
            color: var(--warning-orange);
        }

        .status-verified {
            background: rgba(46, 125, 50, 0.1);
            color: var(--success-green);
        }

        .status-completed {
            background: rgba(46, 125, 50, 0.1);
            color: var(--success-green);
        }

        .status-rejected {
            background: rgba(220, 53, 69, 0.1);
            color: var(--error-red);
        }

        .status-waiting {
            background: rgba(23, 162, 184, 0.1);
            color: var(--info-blue);
        }

        .btn-sm {
            padding: 6px 12px;
            background: var(--primary-navy);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 12px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }

        .btn-sm:hover {
            background: var(--primary-dark);
        }

        .empty-state {
            text-align: center;
            padding: 48px;
            color: var(--gray-500);
        }

        .empty-state i {
            font-size: 48px;
            margin-bottom: 16px;
            opacity: 0.5;
        }

        .hamburger {
            display: none;
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
        }

        @media (max-width: 1024px) {
            .sidebar {
                transform: translateX(-100%);
                z-index: 1000;
            }

            .sidebar.mobile-open {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
            }

            .hamburger {
                display: block;
            }
        }

        @media (max-width: 768px) {
            .content-area {
                padding: 20px;
            }

            .top-header {
                padding: 12px 20px;
            }

            .page-title h1 {
                font-size: 20px;
            }

            .card {
                padding: 16px;
            }
        }
    </style>
    <link rel="stylesheet" href="../assets/css/nexus-theme.css">
</head>

<body>
    <div class="dashboard-wrapper">
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <a href="dashboard.php" class="logo">
                    <div class="logo-icon"><i class="fas fa-building-columns"></i></div>
                    <span class="logo-text">Oakshire Finance</span>
                </a>
            </div>
            <div class="profile-section">
                <div class="profile-avatar"><img src="<?= $profilePic ?>" alt="Profile"></div>
                <h5><?= htmlspecialchars($user['full_name']) ?></h5>
                <p><?= htmlspecialchars($user['account_number']) ?></p>
            </div>
            <nav class="nav-menu">
                <div class="nav-item"><a href="dashboard.php" class="nav-link"><i class="fas fa-chart-line"></i>
                        Dashboard</a></div>
                <div class="nav-item"><a href="deposit.php" class="nav-link"><i class="fas fa-arrow-down"></i>
                        Deposit</a></div>
                <div class="nav-item"><a href="withdraw.php" class="nav-link"><i class="fas fa-arrow-up"></i>
                        Withdraw</a></div>
                <div class="nav-item"><a href="transfer.php" class="nav-link"><i class="fas fa-exchange-alt"></i>
                        Transfer</a></div>
                <div class="nav-item"><a href="pending-withdrawals.php" class="nav-link active"><i
                            class="fas fa-clock"></i> Pending Requests</a></div>
                <div class="nav-item"><a href="profile.php" class="nav-link"><i class="fas fa-user-circle"></i>
                        Settings</a></div>
            </nav>
            <div class="logout-section"><a href="../logout.php" class="logout-btn"><i class="fas fa-sign-out-alt"></i>
                    Logout</a></div>
        </aside>

        <main class="main-content">
            <header class="top-header">
                <button class="hamburger" id="hamburger"><i class="fas fa-bars"></i></button>
                <div class="page-title">
                    <h1>Pending Withdrawal Requests</h1>
                </div>
            </header>

            <div class="content-area">
                <?php if ($success): ?>
                    <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?= $success ?></div>
                <?php endif; ?>
                <?php if ($error): ?>
                    <div class="alert alert-danger"><i class="fas fa-exclamation-circle"></i> <?= $error ?></div>
                <?php endif; ?>

                <div class="card">
                    <h2><i class="fas fa-clock"></i> Active Requests</h2>
                    <?php if (empty($pendingWithdrawals)): ?>
                        <div class="empty-state">
                            <i class="fas fa-check-circle"></i>
                            <p>No pending withdrawal requests</p>
                        </div>
                    <?php else: ?>
                        <div class="table-container">
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <th>Amount</th>
                                        <th>Status</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($pendingWithdrawals as $pending): ?>
                                        <tr>
                                            <td><?= date('M d, Y h:i A', strtotime($pending['created_at'])) ?>54d
                                            <td><strong>$<?= number_format($pending['amount'], 2) ?></strong>54d
                                            <td>
                                                <?php
                                                $status_text = '';
                                                $status_class = 'status-pending';
                                                if ($pending['sms_step'] == 'step1_pending') {
                                                    $status_text = 'Waiting for Admin';
                                                    $status_class = 'status-waiting';
                                                } elseif ($pending['sms_step'] == 'step1_sent') {
                                                    $status_text = 'Awaiting Code 1';
                                                    $status_class = 'status-pending';
                                                } elseif ($pending['sms_step'] == 'step1_verified') {
                                                    $status_text = '✓ Code 1 Verified';
                                                    $status_class = 'status-verified';
                                                } elseif ($pending['sms_step'] == 'step2_sent') {
                                                    $status_text = 'Awaiting Code 2';
                                                    $status_class = 'status-pending';
                                                } elseif ($pending['sms_step'] == 'step2_verified') {
                                                    $status_text = '✓✓ Codes 1-2 Verified';
                                                    $status_class = 'status-verified';
                                                } elseif ($pending['sms_step'] == 'step3_sent') {
                                                    $status_text = 'Awaiting Code 3';
                                                    $status_class = 'status-pending';
                                                } elseif ($pending['sms_step'] == 'step3_verified') {
                                                    $status_text = '✓✓✓ All Codes Verified!';
                                                    $status_class = 'status-verified';
                                                } elseif ($pending['sms_step'] == 'ready_for_admin') {
                                                    $status_text = 'Pending Admin Approval';
                                                    $status_class = 'status-waiting';
                                                }
                                                ?>
                                                <span class="status-badge <?= $status_class ?>"><?= $status_text ?></span>
                                                54d
                                            <td>
                                                <a href="verify-withdrawal.php?id=<?= $pending['pending_id'] ?>" class="btn-sm">
                                                    <i class="fas fa-arrow-right"></i> Verify
                                                </a>
                                                54d
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>

                <div class="card">
                    <h2><i class="fas fa-history"></i> Request History</h2>
                    <?php if (empty($completedWithdrawals)): ?>
                        <div class="empty-state">
                            <i class="fas fa-receipt"></i>
                            <p>No withdrawal history</p>
                        </div>
                    <?php else: ?>
                        <div class="table-container">
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <th>Amount</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($completedWithdrawals as $completed): ?>
                                        <tr>
                                            <td><?= date('M d, Y', strtotime($completed['created_at'])) ?>54d
                                            <td>$<?= number_format($completed['amount'], 2) ?>54d
                                            <td>
                                                <span
                                                    class="status-badge <?= $completed['sms_step'] == 'approved' ? 'status-completed' : 'status-rejected' ?>">
                                                    <?= ucfirst($completed['sms_step']) ?>
                                                </span>
                                                54d
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </main>
    </div>

    <script>
        const hamburger = document.getElementById('hamburger');
        const sidebar = document.getElementById('sidebar');
        if (hamburger) hamburger.addEventListener('click', () => sidebar.classList.toggle('mobile-open'));

        // Auto-refresh every 30 seconds to check for new codes
        setTimeout(function () {
            location.reload();
        }, 30000);
    </script>
</body>

</html>