<?php
session_start();
require_once 'php/adminconfig.php';

// Enhanced login and role check
if (!isset($_SESSION['admin_id']) || $_SESSION['user_role'] !== 'admin') {
    header('Location: signin-admin.php');
    exit();
}

$success_message = '';
$error_message = '';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        $user_id = (int)$_POST['user_id'];
        $form_type = $_POST['form_type'];
        
        // Validate user exists
        $check_user = $conn->prepare("SELECT id, firstname, lastname FROM users WHERE id = ?");
        $check_user->execute([$user_id]);
        $user = $check_user->fetch();
        
        if (!$user) {
            throw new Exception('User not found');
        }
        
        if ($form_type === 'crypto') {
            // Handle crypto details form
            $bitcoin_address = trim($_POST['bitcoin_address']);
            $etherium_address = trim($_POST['etherium_address']);
            $usdt_address = trim($_POST['usdt_address']);
            $usdc_address = trim($_POST['usdc_address']);
            $solana_address = trim($_POST['solana_address']);
            
            $stmt = $conn->prepare("
                UPDATE users SET 
                bitcoin_address = ?, 
                etherium_address = ?, 
                usdt_address = ?, 
                usdc_address = ?, 
                solana_address = ? 
                WHERE id = ?
            ");
            
            $stmt->execute([
                $bitcoin_address, 
                $etherium_address, 
                $usdt_address, 
                $usdc_address, 
                $solana_address, 
                $user_id
            ]);
            
            $success_message = "Crypto details updated successfully for {$user['firstname']} {$user['lastname']}";
            
        } elseif ($form_type === 'bank') {
            // Handle bank details form
            $bank_name = trim($_POST['bank_name']);
            $holder_name = trim($_POST['holder_name']);
            $status = trim($_POST['status']);
            $account_number = trim($_POST['account_number']);
            $payment_amount = trim($_POST['payment_amount']);
            $bsbn = trim($_POST['bsbn']);
            
            $stmt = $conn->prepare("
                UPDATE users SET 
                bank_name = ?, 
                holder_name = ?, 
                status = ?, 
                account_number = ?, 
                payment_amount = ?, 
                BSBN = ? 
                WHERE id = ?
            ");
            
            $stmt->execute([
                $bank_name, 
                $holder_name, 
                $status, 
                $account_number, 
                $payment_amount, 
                $bsbn, 
                $user_id
            ]);
            
            $success_message = "Bank details updated successfully for {$user['firstname']} {$user['lastname']}";
        }
        
    } catch (Exception $e) {
        $error_message = $e->getMessage();
    }
}

// Fetch all users for dropdown
try {
    $users_stmt = $conn->prepare("SELECT id, firstname, lastname, email FROM users WHERE role = 'user' ORDER BY firstname, lastname");
    $users_stmt->execute();
    $users = $users_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $users = [];
    $error_message = "Error fetching users: " . $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Management - Admin Panel</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/admin.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

       

        .container {
            max-width: 1200px;
            margin: -30px 10px 10px 10px;
        }

        .header {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 8px;
            padding: 30px;
            margin-bottom: 10px;
            text-align: center;
            animation: slideDown 0.6s ease-out;
        }

        .header h1 {
            font-size: 10px;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 10px;
        }

        .header p {
            color: #666;
            font-size: 1.1rem;
        }

        .forms-container {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-bottom: 30px;
        }

        .form-card {
            background: rgba(255, 255, 255, 0.5);
            backdrop-filter: blur(10px);
            border-radius: 10px;
            padding: 30px;
            transition: all 0.3s ease;
            animation: slideUp 0.8s ease-out;
        }

        .form-card:hover {
            transform: translateY(-10px);
            box-shadow: 0 30px 60px rgba(0, 0, 0, 0.15);
        }

        .form-header {
            display: flex;
            align-items: center;
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }

        .form-icon {
            width: 50px;
            height: 50px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.5rem;
            color: white;
            margin-right: 15px;
        }

        .crypto-icon {
            background: linear-gradient(135deg, #f093fb, #f5576c);
        }

        .bank-icon {
            background: linear-gradient(135deg, #4facfe, #00f2fe);
        }

        .form-title {
            font-size: 1.5rem;
            font-weight: 600;
            color: #333;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #555;
            display: flex;
            align-items: center;
        }

        .form-group label i {
            margin-right: 8px;
            color: #667eea;
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            font-size: 1rem;
            transition: all 0.3s ease;
            background: #fafafa;
        }

        .form-control:focus {
            outline: none;
            border-color: #667eea;
            background: white;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            transform: translateY(-2px);
        }

        .form-control:hover {
            border-color: #667eea;
            background: white;
        }

        select.form-control {
            cursor: pointer;
        }

        .btn {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 10px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            position: relative;
            overflow: hidden;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .btn:active {
            transform: translateY(0);
        }

        .btn i {
            margin-right: 8px;
        }

        .btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: left 0.5s;
        }

        .btn:hover::before {
            left: 100%;
        }

        .alert {
            padding: 15px 20px;
            border-radius: 10px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            animation: fadeIn 0.5s ease-out;
        }

        .alert i {
            margin-right: 10px;
            font-size: 1.2rem;
        }

        .alert-success {
            background: linear-gradient(135deg, #d4edda, #c3e6cb);
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background: linear-gradient(135deg, #f8d7da, #f5c6cb);
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .user-info {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 20px;
            border-left: 4px solid #667eea;
        }

        .loading {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            backdrop-filter: blur(5px);
            z-index: 1000;
            align-items: center;
            justify-content: center;
        }

        .loading-spinner {
            width: 50px;
            height: 50px;
            border: 4px solid rgba(255, 255, 255, 0.3);
            border-top: 4px solid white;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
            }
            to {
                opacity: 1;
            }
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .container {
                padding: 10px;
            }

            .forms-container {
                grid-template-columns: 1fr;
                gap: 20px;
            }

            .header h1 {
                font-size: 1rem;
            }

            .form-card {
                padding: 20px;
            }

            .form-header {
                flex-direction: column;
                text-align: center;
            }

            .form-icon {
                margin-right: 0;
                margin-bottom: 10px;
            }
        }

        @media (max-width: 480px) {
            .header {
                padding: 20px;
            }

            .header h1 {
                font-size: 1.8rem;
            }

            .form-card {
                padding: 15px;
            }
        }

        /* Crypto specific styling */
        .crypto-addresses {
            display: grid;
            gap: 15px;
        }

        .crypto-item {
            position: relative;
        }

        .crypto-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 3px;
            height: 20px;
            background: linear-gradient(135deg, #f093fb, #f5576c);
            border-radius: 2px;
        }

        .crypto-item label {
            padding-left: 15px;
        }

        /* Bank specific styling */
        .bank-details {
            display: grid;
            gap: 15px;
        }

        .bank-item {
            position: relative;
        }

        .bank-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 3px;
            height: 20px;
            background: linear-gradient(135deg, #4facfe, #00f2fe);
            border-radius: 2px;
        }

        .bank-item label {
            padding-left: 15px;
        }

        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px 20px;
            border-radius: 10px;
            color: white;
            font-weight: 500;
            z-index: 1001;
            transform: translateX(100%);
            transition: transform 0.3s ease;
        }

        .notification.show {
            transform: translateX(0);
        }

        .notification.success {
            background: linear-gradient(135deg, #28a745, #20c997);
        }

        .notification.error {
            background: linear-gradient(135deg, #dc3545, #fd7e14);
        }
    </style>
</head>
<body>


    <?php include "required/admin-nav.php" ?>

    <div class="pc">
    <div class="loading" id="loading">
        <div class="loading-spinner"></div>
    </div>

    <div class="container">
        <div class="header">
            <h1><i class="fas fa-credit-card"></i> Payment Management</h1>
        </div>

        <?php if ($success_message): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i>
                <?php echo htmlspecialchars($success_message); ?>
            </div>
        <?php endif; ?>

        <?php if ($error_message): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-triangle"></i>
                <?php echo htmlspecialchars($error_message); ?>
            </div>
        <?php endif; ?>

        <div class="forms-container">
            <!-- Crypto Details Form -->
            <div class="form-card">
                <div class="form-header">
                    <div class="form-icon crypto-icon">
                        <i class="fab fa-bitcoin"></i>
                    </div>
                    <div>
                        <h2 class="form-title">Crypto Addresses</h2>
                        <p style="color: #666; margin-top: 5px;">Update cryptocurrency wallet addresses</p>
                    </div>
                </div>

                <form method="POST" onsubmit="showLoading()">
                    <input type="hidden" name="form_type" value="crypto">
                    
                    <div class="form-group">
                        <label for="crypto_user_id">
                            <i class="fas fa-user"></i>
                            Select User
                        </label>
                        <select name="user_id" id="crypto_user_id" class="form-control" required>
                            <option value="">Choose a user...</option>
                            <?php foreach ($users as $user): ?>
                                <option value="<?php echo $user['id']; ?>">
                                    <?php echo htmlspecialchars($user['firstname'] . ' ' . $user['lastname'] . ' (' . $user['email'] . ')'); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="crypto-addresses">
                        <div class="crypto-item">
                            <div class="form-group">
                                <label for="bitcoin_address">
                                    <i class="fab fa-bitcoin" style="color: #f7931a;"></i>
                                    Bitcoin Address
                                </label>
                                <input type="text" name="bitcoin_address" id="bitcoin_address" class="form-control" 
                                       placeholder="Enter Bitcoin wallet address">
                            </div>
                        </div>

                        <div class="crypto-item">
                            <div class="form-group">
                                <label for="etherium_address">
                                    <i class="fab fa-ethereum" style="color: #627eea;"></i>
                                    Ethereum Address
                                </label>
                                <input type="text" name="etherium_address" id="etherium_address" class="form-control" 
                                       placeholder="Enter Ethereum wallet address">
                            </div>
                        </div>

                        <div class="crypto-item">
                            <div class="form-group">
                                <label for="usdt_address">
                                    <i class="fas fa-dollar-sign" style="color: #26a17b;"></i>
                                    USDT Address
                                </label>
                                <input type="text" name="usdt_address" id="usdt_address" class="form-control" 
                                       placeholder="Enter USDT wallet address">
                            </div>
                        </div>

                        <div class="crypto-item">
                            <div class="form-group">
                                <label for="usdc_address">
                                    <i class="fas fa-coins" style="color: #2775ca;"></i>
                                    USDC Address
                                </label>
                                <input type="text" name="usdc_address" id="usdc_address" class="form-control" 
                                       placeholder="Enter USDC wallet address">
                            </div>
                        </div>

                        <div class="crypto-item">
                            <div class="form-group">
                                <label for="solana_address">
                                    <i class="fas fa-sun" style="color: #9945ff;"></i>
                                    Solana Address
                                </label>
                                <input type="text" name="solana_address" id="solana_address" class="form-control" 
                                       placeholder="Enter Solana wallet address">
                            </div>
                        </div>
                    </div>

                    <button type="submit" class="btn">
                        <i class="fas fa-save"></i>
                        Update Crypto Details
                    </button>
                </form>
            </div>

            <!-- Bank Details Form -->
            <div class="form-card">
                <div class="form-header">
                    <div class="form-icon bank-icon">
                        <i class="fas fa-university"></i>
                    </div>
                    <div>
                        <h2 class="form-title">Bank Details</h2>
                        <p style="color: #666; margin-top: 5px;">Update banking information</p>
                    </div>
                </div>

                <form method="POST" onsubmit="showLoading()">
                    <input type="hidden" name="form_type" value="bank">
                    
                    <div class="form-group">
                        <label for="bank_user_id">
                            <i class="fas fa-user"></i>
                            Select User
                        </label>
                        <select name="user_id" id="bank_user_id" class="form-control" required>
                            <option value="">Choose a user...</option>
                            <?php foreach ($users as $user): ?>
                                <option value="<?php echo $user['id']; ?>">
                                    <?php echo htmlspecialchars($user['firstname'] . ' ' . $user['lastname'] . ' (' . $user['email'] . ')'); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="bank-details">
                        <div class="bank-item">
                            <div class="form-group">
                                <label for="bank_name">
                                    <i class="fas fa-university"></i>
                                    Bank Name
                                </label>
                                <input type="text" name="bank_name" id="bank_name" class="form-control" 
                                       placeholder="Enter bank name">
                            </div>
                        </div>

                        <div class="bank-item">
                            <div class="form-group">
                                <label for="holder_name">
                                    <i class="fas fa-user-tie"></i>
                                    Account Holder Name
                                </label>
                                <input type="text" name="holder_name" id="holder_name" class="form-control" 
                                       placeholder="Enter account holder name">
                            </div>
                        </div>

                        <div class="bank-item">
                            <div class="form-group">
                                <label for="status">
                                    <i class="fas fa-info-circle"></i>
                                    Status
                                </label>
                                <select name="status" id="status" class="form-control">
                                    <option value="">Select status...</option>
                                    <option value="Active">Active</option>
                                    <option value="Pending">Pending</option>
                                    <option value="Suspended">Suspended</option>
                                    <option value="Inactive">Inactive</option>
                                </select>
                            </div>
                        </div>

                        <div class="bank-item">
                            <div class="form-group">
                                <label for="account_number">
                                    <i class="fas fa-hashtag"></i>
                                    Account Number
                                </label>
                                <input type="text" name="account_number" id="account_number" class="form-control" 
                                       placeholder="Enter account number">
                            </div>
                        </div>

                        <div class="bank-item">
                            <div class="form-group">
                                <label for="payment_amount">
                                    <i class="fas fa-dollar-sign"></i>
                                    Payment Amount
                                </label>
                                <input type="number" step="0.01" name="payment_amount" id="payment_amount" class="form-control" 
                                       placeholder="Enter payment amount">
                            </div>
                        </div>

                        <div class="bank-item">
                            <div class="form-group">
                                <label for="bsbn">
                                    <i class="fas fa-code"></i>
                                    BSB Number
                                </label>
                                <input type="text" name="bsbn" id="bsbn" class="form-control" 
                                       placeholder="Enter BSB number">
                            </div>
                        </div>
                    </div>

                    <button type="submit" class="btn">
                        <i class="fas fa-save"></i>
                        Update Bank Details
                    </button>
                </form>
            </div>
        </div>
    </div>

</div>

    <script>
        function showLoading() {
            document.getElementById('loading').style.display = 'flex';
        }

        function hideLoading() {
            document.getElementById('loading').style.display = 'none';
        }

        function showNotification(message, type = 'success') {
            const notification = document.createElement('div');
            notification.className = `notification ${type}`;
            notification.textContent = message;
            
            document.body.appendChild(notification);
            
            setTimeout(() => {
                notification.classList.add('show');
            }, 100);
            
            setTimeout(() => {
                notification.classList.remove('show');
                setTimeout(() => {
                    document.body.removeChild(notification);
                }, 300);
            }, 4000);
        }

        // Add form validation
        document.addEventListener('DOMContentLoaded', function() {
            const forms = document.querySelectorAll('form');
            
            forms.forEach(form => {
                form.addEventListener('submit', function(e) {
                    const userSelect = this.querySelector('select[name="user_id"]');
                    if (!userSelect.value) {
                        e.preventDefault();
                        showNotification('Please select a user first', 'error');
                        hideLoading();
                        return false;
                    }
                });
            });

            // Add input animations
            const inputs = document.querySelectorAll('.form-control');
            inputs.forEach(input => {
                input.addEventListener('focus', function() {
                    this.parentElement.style.transform = 'scale(1.02)';
                });
                
                input.addEventListener('blur', function() {
                    this.parentElement.style.transform = 'scale(1)';
                });
            });

            // Auto-hide alerts after 5 seconds
            const alerts = document.querySelectorAll('.alert');
            alerts.forEach(alert => {
                setTimeout(() => {
                    alert.style.opacity = '0';
                    alert.style.transform = 'translateY(-20px)';
                    setTimeout(() => {
                        alert.remove();
                    }, 300);
                }, 5000);
            });
        });

        // Prevent double submission
        let formSubmitted = false;
        document.addEventListener('submit', function(e) {
            if (formSubmitted) {
                e.preventDefault();
                return false;
            }
            formSubmitted = true;
            setTimeout(() => {
                formSubmitted = false;
            }, 3000);
        });
    </script>
</body>
</html>