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

redirectIfNotAdmin();

// Fetch all pending transactions
$pendingTransactions = $pdo->prepare("
    SELECT pt.*, u.full_name, u.email, u.phone, a.account_number 
    FROM pending_transactions pt
    JOIN users u ON pt.user_id = u.user_id
    JOIN accounts a ON pt.account_id = a.account_id
    WHERE pt.sms_step NOT IN ('approved', 'rejected', 'cancelled')
    ORDER BY 
        CASE 
            WHEN pt.sms_step = 'step3_verified' THEN 1
            WHEN pt.sms_step = 'step2_verified' THEN 2
            WHEN pt.sms_step = 'step1_verified' THEN 3
            ELSE 4
        END,
        pt.created_at ASC
");
$pendingTransactions->execute();
$pendingTransactions = $pendingTransactions->fetchAll();

// Fetch completed transactions
$completedTransactions = $pdo->prepare("
    SELECT pt.*, u.full_name, u.email, a.account_number 
    FROM pending_transactions pt
    JOIN users u ON pt.user_id = u.user_id
    JOIN accounts a ON pt.account_id = a.account_id
    WHERE pt.sms_step IN ('approved', 'rejected')
    ORDER BY pt.updated_at DESC
    LIMIT 50
");
$completedTransactions->execute();
$completedTransactions = $completedTransactions->fetchAll();

// Get admin info
$adminStmt = $pdo->prepare("SELECT full_name FROM users WHERE user_id = ?");
$adminStmt->execute([$_SESSION['user_id']]);
$admin = $adminStmt->fetch();
?>

<!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 Transactions - Oakshire Finance Admin</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;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: #f8f9fc;
        }

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

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

        .sidebar-header {
            padding: 24px;
            text-align: center;
            border-bottom: 1px solid #e9ecef;
        }

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

        .logo-icon {
            width: 40px;
            height: 40px;
            background: #0a2540;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

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

        .logo-text {
            font-size: 22px;
            font-weight: 700;
            color: #0a2540;
        }

        .nav-menu {
            padding: 24px 16px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            border-radius: 12px;
            text-decoration: none;
            color: #495057;
            transition: all 0.3s;
            font-weight: 500;
        }

        .nav-link:hover {
            background: #f1f3f9;
            color: #1a4d8c;
        }

        .nav-link.active {
            background: #0a2540;
            color: white;
        }

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

        .top-header {
            background: white;
            border-bottom: 1px solid #e9ecef;
            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: #212529;
        }

        .content-area {
            padding: 32px;
        }

        .card {
            background: white;
            border-radius: 24px;
            border: 1px solid #e9ecef;
            padding: 24px;
            margin-bottom: 24px;
        }

        .card h2 {
            font-size: 20px;
            font-weight: 700;
            margin-bottom: 20px;
            color: #0a2540;
            display: flex;
            align-items: center;
            gap: 10px;
        }

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

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

        .data-table th,
        .data-table td {
            padding: 16px 20px;
            text-align: left;
            border-bottom: 1px solid #e9ecef;
        }

        .data-table th {
            background: #f8f9fc;
            font-weight: 600;
            font-size: 12px;
            text-transform: uppercase;
            color: #6c757d;
        }

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

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

        .status-step1 {
            background: rgba(23, 162, 184, 0.1);
            color: #17a2b8;
        }

        .status-step2 {
            background: rgba(23, 162, 184, 0.1);
            color: #17a2b8;
        }

        .status-step3 {
            background: rgba(243, 156, 18, 0.1);
            color: #f39c12;
        }

        .status-ready {
            background: rgba(46, 125, 50, 0.1);
            color: #2e7d32;
        }

        .status-approved {
            background: rgba(46, 125, 50, 0.1);
            color: #2e7d32;
        }

        .status-rejected {
            background: rgba(220, 53, 69, 0.1);
            color: #dc3545;
        }

        .btn-sm {
            padding: 8px 14px;
            border-radius: 8px;
            font-size: 12px;
            font-weight: 500;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            margin: 2px;
        }

        .btn-primary {
            background: #0a2540;
            color: white;
            border: none;
        }

        .btn-primary:hover {
            background: #0a1c2f;
        }

        .btn-success {
            background: rgba(46, 125, 50, 0.1);
            color: #2e7d32;
            border: 1px solid #2e7d32;
        }

        .btn-success:hover {
            background: #2e7d32;
            color: white;
        }

        .btn-danger {
            background: rgba(220, 53, 69, 0.1);
            color: #dc3545;
            border: 1px solid #dc3545;
        }

        .btn-danger:hover {
            background: #dc3545;
            color: white;
        }

        .btn-info {
            background: rgba(23, 162, 184, 0.1);
            color: #17a2b8;
            border: 1px solid #17a2b8;
        }

        .btn-info:hover {
            background: #17a2b8;
            color: white;
        }

        .alert {
            padding: 14px 16px;
            border-radius: 12px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .alert-success {
            background: rgba(46, 125, 50, 0.1);
            border-left: 3px solid #2e7d32;
            color: #2e7d32;
        }

        .alert-danger {
            background: rgba(220, 53, 69, 0.1);
            border-left: 3px solid #dc3545;
            color: #dc3545;
        }

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

        @media (max-width: 1024px) {
            .sidebar {
                transform: translateX(-100%);
                position: fixed;
                z-index: 1000;
                transition: transform 0.3s ease;
            }

            .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;
            }

            .card h2 {
                font-size: 18px;
            }

            .btn-sm {
                padding: 6px 10px;
                font-size: 11px;
            }
        }
    </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>
            <nav class="nav-menu">
                <div class="nav-item">
                    <a href="dashboard.php" class="nav-link">
                        <i class="fas fa-chart-line"></i>
                        <span>Dashboard</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-users.php" class="nav-link">
                        <i class="fas fa-users"></i>
                        <span>Manage Users</span>
                    </a>
                </div>


                <div class="nav-item"><a href="manage-balance.php" class="nav-link"><i class="fas fa-wallet"></i> Manage
                        Balance</a></div>
                <div class="nav-item">
                    <a href="manage-loans.php" class="nav-link">
                        <i class="fas fa-hand-holding-usd"></i>
                        <span>Manage Loans</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-investments.php" class="nav-link">
                        <i class="fas fa-chart-pie"></i>
                        <span>Manage Investments</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="track-investments.php" class="nav-link">
                        <i class="fas fa-chart-simple"></i>
                        <span>Users Investments</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="role.php" class="nav-link">
                        <i class="fas fa-user-tag"></i>
                        <span>Roles</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="recent_transactions.php" class="nav-link">
                        <i class="fas fa-exchange-alt"></i>
                        <span>Transactions</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="loan-history.php" class="nav-link">
                        <i class="fas fa-history"></i>
                        <span>Loan History</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="login-records.php" class="nav-link">
                        <i class="fas fa-sign-in-alt"></i>
                        <span>Login Records</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-messages.php" class="nav-link">
                        <i class="fas fa-envelope"></i>
                        <span>Contact Messages</span>
                    </a>
                </div>
            </nav>

            <div class="logout-section">
                <a href="../logout.php" class="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    <span>Logout</span>
                </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>📧 3-Step Email Verification Dashboard</h1>
                </div>
            </header>

            <div class="content-area">
                <?php if (isset($_SESSION['admin_success'])): ?>
                    <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?= $_SESSION['admin_success'] ?>
                    </div>
                    <?php unset($_SESSION['admin_success']); ?>
                <?php endif; ?>
                <?php if (isset($_SESSION['admin_error'])): ?>
                    <div class="alert alert-danger"><i class="fas fa-exclamation-circle"></i>
                        <?= $_SESSION['admin_error'] ?></div>
                    <?php unset($_SESSION['admin_error']); ?>
                <?php endif; ?>

                <div class="card">
                    <h2><i class="fas fa-hourglass-half"></i> Active Withdrawal Requests</h2>
                    <?php if (empty($pendingTransactions)): ?>
                        <p style="text-align: center; padding: 40px; color: #6c757d;">No pending withdrawal requests</p>
                    <?php else: ?>
                        <div class="table-container">
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>User / Email</th>
                                        <th>Amount</th>
                                        <th>Account</th>
                                        <th>Current Step</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($pendingTransactions as $pt): ?>
                                        <?php
                                        $step_status = '';
                                        $step_text = '';
                                        $step_number = 0;
                                        if ($pt['sms_step'] == 'step1_pending') {
                                            $step_status = 'status-step1';
                                            $step_text = '📧 Step 1: Waiting';
                                            $step_number = 1;
                                        } elseif ($pt['sms_step'] == 'step1_sent') {
                                            $step_status = 'status-step1';
                                            $step_text = '📧 Step 1: Email Sent';
                                            $step_number = 1;
                                        } elseif ($pt['sms_step'] == 'step1_verified') {
                                            $step_status = 'status-step2';
                                            $step_text = '✓ Step 1 Verified - Send Code 2';
                                            $step_number = 2;
                                        } elseif ($pt['sms_step'] == 'step2_sent') {
                                            $step_status = 'status-step2';
                                            $step_text = '📧 Step 2: Email Sent';
                                            $step_number = 2;
                                        } elseif ($pt['sms_step'] == 'step2_verified') {
                                            $step_status = 'status-step3';
                                            $step_text = '✓✓ Step 2 Verified - Send Code 3';
                                            $step_number = 3;
                                        } elseif ($pt['sms_step'] == 'step3_sent') {
                                            $step_status = 'status-step3';
                                            $step_text = '📧 Step 3: Email Sent';
                                            $step_number = 3;
                                        } elseif ($pt['sms_step'] == 'step3_verified') {
                                            $step_status = 'status-ready';
                                            $step_text = '✓✓✓ ALL VERIFIED - Ready for Approval';
                                            $step_number = 0;
                                        } elseif ($pt['sms_step'] == 'ready_for_admin') {
                                            $step_status = 'status-ready';
                                            $step_text = 'Pending Admin Approval';
                                            $step_number = 0;
                                        }
                                        ?>
                                        <tr>
                                            <td><strong>#<?= $pt['pending_id'] ?></strong>
                                            <td>
                                                <strong><?= htmlspecialchars($pt['full_name']) ?></strong><br>
                                                <small style="color: #6c757d;">📧 <?= htmlspecialchars($pt['email']) ?></small>

                                            <td><strong style="color: #dc3545;">$<?= number_format($pt['amount'], 2) ?></strong>
                                            <td><code><?= $pt['account_number'] ?></code>
                                            <td><span class="status-badge <?= $step_status ?>"><?= $step_text ?></span>
                                            <td>
                                                <?php if ($pt['sms_step'] == 'step1_pending'): ?>
                                                    <a href="send-email-code.php?id=<?= $pt['pending_id'] ?>&step=1"
                                                        class="btn-sm btn-primary">📧 Send Code 1</a>
                                                <?php elseif ($pt['sms_step'] == 'step1_verified'): ?>
                                                    <a href="send-email-code.php?id=<?= $pt['pending_id'] ?>&step=2"
                                                        class="btn-sm btn-primary">📧 Send Code 2</a>
                                                <?php elseif ($pt['sms_step'] == 'step2_verified'): ?>
                                                    <a href="send-email-code.php?id=<?= $pt['pending_id'] ?>&step=3"
                                                        class="btn-sm btn-primary">📧 Send Code 3</a>
                                                <?php elseif ($pt['sms_step'] == 'step3_verified' || $pt['sms_step'] == 'ready_for_admin'): ?>
                                                    <a href="approve-withdrawal.php?id=<?= $pt['pending_id'] ?>"
                                                        class="btn-sm btn-success"
                                                        onclick="return confirm('Approve this withdrawal?')">✓ Approve</a>
                                                    <a href="reject-withdrawal.php?id=<?= $pt['pending_id'] ?>"
                                                        class="btn-sm btn-danger"
                                                        onclick="return confirm('Reject this withdrawal?')">✗ Reject</a>
                                                <?php endif; ?>
                                                <?php if (in_array($pt['sms_step'], ['step1_sent', 'step2_sent', 'step3_sent'])): ?>
                                                    <a href="resend-email-code.php?id=<?= $pt['pending_id'] ?>&step=<?= $step_number ?>"
                                                        class="btn-sm btn-info">⟳ Resend</a>
                                                <?php endif; ?>

                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>

                <div class="card">
                    <h2><i class="fas fa-check-circle"></i> Completed Transactions</h2>
                    <?php if (empty($completedTransactions)): ?>
                        <p style="text-align: center; padding: 40px; color: #6c757d;">No completed transactions</p>
                    <?php else: ?>
                        <div class="table-container">
                            <table class="data-table">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>User</th>
                                        <th>Amount</th>
                                        <th>Status</th>
                                        <th>Date</th>
                                </thead>
                                <tbody>
                                    <?php foreach ($completedTransactions as $ct): ?>
                                        <tr>
                                            <td>#<?= $ct['pending_id'] ?>
                                            <td><?= htmlspecialchars($ct['full_name']) ?>
                                            <td>$<?= number_format($ct['amount'], 2) ?>
                                            <td><span
                                                    class="status-badge <?= $ct['sms_step'] == 'approved' ? 'status-approved' : 'status-rejected' ?>"><?= ucfirst($ct['sms_step']) ?></span>
                                            <td><?= date('M d, Y', strtotime($ct['updated_at'])) ?>
                                        </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'));
    </script>
</body>

</html>