<?php
//Developer: CORE TECH WEB DESIGN
include('header.php');
$page_title = WEB_NAME . " | Become a Seller";

$seller_fee = floatval($site_data['seller_registration_fee'] ?? 2000);
$is_seller = intval($user['is_seller'] ?? 0);

// Check if user already has a pending application
$pending_app = $pdo->prepare("SELECT * FROM seller_applications WHERE user_id = ? AND status = 'pending' LIMIT 1");
$pending_app->execute([$user['id']]);
$has_pending = $pending_app->fetch(PDO::FETCH_ASSOC);

$msg_success = ''; $msg_error = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'apply_seller') {
    if ($is_seller) {
        $msg_error = "You are already a verified seller.";
    } elseif ($has_pending) {
        $msg_error = "You already have a pending application. Please wait for admin review.";
    } elseif ($user['main_balance'] < $seller_fee) {
        $msg_error = "Insufficient balance. You need ₦" . number_format($seller_fee, 2) . " but have ₦" . number_format($user['main_balance'], 2) . ".";
    } else {
        $full_name = trim($_POST['full_name'] ?? '');
        $phone = trim($_POST['phone'] ?? '');
        $bank_name = trim($_POST['bank_name'] ?? '');
        $account_number = trim($_POST['account_number'] ?? '');
        $account_name = trim($_POST['account_name'] ?? '');
        $reason = trim($_POST['reason'] ?? '');

        if (empty($full_name) || empty($phone)) {
            $msg_error = "Full name and phone number are required.";
        } else {
            try {
                $pdo->beginTransaction();
                // Deduct fee
                $pdo->prepare("UPDATE users SET main_balance = main_balance - ? WHERE id = ?")->execute([$seller_fee, $user['id']]);
                // Create application
                $pdo->prepare("INSERT INTO seller_applications (user_id, full_name, phone, bank_name, account_number, account_name, reason, fee_paid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
                    ->execute([$user['id'], $full_name, $phone, $bank_name, $account_number, $account_name, $reason, $seller_fee]);
                $pdo->commit();
                $user['main_balance'] -= $seller_fee;
                $msg_success = "Application submitted! ₦" . number_format($seller_fee, 2) . " fee deducted. You'll be notified once approved.";
                $has_pending = true;
            } catch (Exception $e) {
                $pdo->rollBack();
                $msg_error = "Something went wrong. Please try again.";
            }
        }
    }
}
?>
<title><?php echo $page_title; ?></title>

<!-- Messages -->
<?php if ($msg_success): ?>
<div class="bg-white rounded-2xl border border-gray-100 p-8 text-center mb-6">
    <div class="w-16 h-16 rounded-full bg-green-100 flex items-center justify-center mx-auto mb-4">
        <i class="fa-solid fa-paper-plane text-green-500 text-2xl"></i>
    </div>
    <h3 class="text-lg font-bold text-gray-800 mb-2">Application Submitted! 🎉</h3>
    <p class="text-sm text-gray-500 mb-4"><?php echo htmlspecialchars($msg_success); ?></p>
    <div class="inline-flex items-center gap-2 bg-yellow-50 border border-yellow-200 text-yellow-800 text-xs font-bold px-4 py-2 rounded-xl">
        <i class="fa-solid fa-clock"></i> Awaiting Admin Review
    </div>
</div>
<?php include('footer.php'); exit(); ?>
<?php endif; ?>
<?php if ($msg_error): ?>
<div class="bg-red-50 border border-red-200 rounded-2xl p-4 mb-5 flex items-center gap-3">
    <div class="w-10 h-10 rounded-full bg-red-100 flex items-center justify-center flex-shrink-0"><i class="fa-solid fa-circle-exclamation text-red-500 text-lg"></i></div>
    <p class="text-sm font-bold text-red-900"><?php echo htmlspecialchars($msg_error); ?></p>
</div>
<?php endif; ?>

<?php if ($is_seller): ?>
<!-- Already a Seller -->
<section class="mb-6">
    <div class="bg-white rounded-2xl border border-gray-100 overflow-hidden">
        <div class="bg-gradient-to-r from-green-500 to-emerald-600 p-6 text-white text-center">
            <div class="w-16 h-16 rounded-full bg-white/20 flex items-center justify-center mx-auto mb-3">
                <i class="fa-solid fa-circle-check text-3xl"></i>
            </div>
            <h3 class="text-xl font-bold mb-1">Verified Seller ✓</h3>
            <p class="text-sm opacity-80">You're approved to sell accounts on the marketplace</p>
        </div>
        <div class="p-6 text-center">
            <p class="text-sm text-gray-500 mb-4">Start listing your social media accounts and earn money when they sell.</p>
            <a href="sell-account" class="inline-flex items-center gap-2 bg-accent hover:bg-orange-700 text-white text-sm font-bold px-6 py-3 rounded-xl transition-colors shadow-sm">
                <i class="fa-solid fa-upload"></i> List an Account for Sale
            </a>
        </div>
    </div>
</section>

<?php elseif ($has_pending): ?>
<!-- Pending Application -->
<section class="mb-6">
    <div class="bg-white rounded-2xl border border-gray-100 overflow-hidden">
        <div class="bg-gradient-to-r from-yellow-400 to-amber-500 p-6 text-white text-center">
            <div class="w-16 h-16 rounded-full bg-white/20 flex items-center justify-center mx-auto mb-3">
                <i class="fa-solid fa-hourglass-half text-3xl"></i>
            </div>
            <h3 class="text-xl font-bold mb-1">Under Review</h3>
            <p class="text-sm opacity-90">Your application is being reviewed</p>
        </div>
        <div class="p-6 text-center">
            <p class="text-sm text-gray-500 mb-2">Our team is reviewing your seller application. This usually takes 1-24 hours.</p>
            <p class="text-xs text-gray-400">You'll be able to list accounts once approved.</p>
        </div>
    </div>
</section>

<?php else: ?>
<!-- Application Form -->
<section class="mb-6">
    <div class="bg-gradient-to-r from-primary to-[#6b23c2] rounded-3xl p-6 text-white relative overflow-hidden mb-6">
        <div class="absolute -right-8 -top-8 w-32 h-32 bg-white opacity-5 rounded-full"></div>
        <div class="relative z-10">
            <div class="flex items-center gap-3 mb-2">
                <div class="w-10 h-10 rounded-xl bg-white/15 flex items-center justify-center">
                    <i class="fa-solid fa-user-tie text-lg"></i>
                </div>
                <div>
                    <h2 class="text-lg font-bold">Become a Seller</h2>
                    <p class="text-xs opacity-70">Sell social media accounts and earn money</p>
                </div>
            </div>
            <div class="mt-4 grid grid-cols-3 gap-3">
                <div class="bg-white/10 rounded-lg p-3 text-center">
                    <i class="fa-solid fa-file-pen text-lg mb-1"></i>
                    <p class="text-[9px] opacity-70">1. Apply</p>
                </div>
                <div class="bg-white/10 rounded-lg p-3 text-center">
                    <i class="fa-solid fa-check-circle text-lg mb-1"></i>
                    <p class="text-[9px] opacity-70">2. Get Approved</p>
                </div>
                <div class="bg-white/10 rounded-lg p-3 text-center">
                    <i class="fa-solid fa-money-bill-wave text-lg mb-1"></i>
                    <p class="text-[9px] opacity-70">3. Start Earning</p>
                </div>
            </div>
        </div>
    </div>

    <div class="bg-white rounded-2xl border border-gray-100 p-5">
        <h3 class="font-bold text-sm mb-4 flex items-center gap-2">
            <i class="fa-solid fa-file-pen text-primary"></i> Seller Application
        </h3>
        <form method="POST">
            <input type="hidden" name="action" value="apply_seller">
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-3 mb-3">
                <div>
                    <label class="text-[10px] text-gray-500 uppercase font-bold tracking-wide mb-1 block">Full Name *</label>
                    <input type="text" name="full_name" required value="<?php echo htmlspecialchars($user['full_name'] ?? ''); ?>" class="w-full border border-gray-200 rounded-xl px-3 py-2.5 text-sm focus:border-primary outline-none">
                </div>
                <div>
                    <label class="text-[10px] text-gray-500 uppercase font-bold tracking-wide mb-1 block">Phone Number *</label>
                    <input type="tel" name="phone" required value="<?php echo htmlspecialchars($user['phone'] ?? ''); ?>" class="w-full border border-gray-200 rounded-xl px-3 py-2.5 text-sm focus:border-primary outline-none">
                </div>
            </div>
            <div class="grid grid-cols-1 sm:grid-cols-3 gap-3 mb-3">
                <div>
                    <label class="text-[10px] text-gray-500 uppercase font-bold tracking-wide mb-1 block">Bank Name</label>
                    <input type="text" name="bank_name" class="w-full border border-gray-200 rounded-xl px-3 py-2.5 text-sm focus:border-primary outline-none" placeholder="e.g. GTBank">
                </div>
                <div>
                    <label class="text-[10px] text-gray-500 uppercase font-bold tracking-wide mb-1 block">Account Number</label>
                    <input type="text" name="account_number" class="w-full border border-gray-200 rounded-xl px-3 py-2.5 text-sm focus:border-primary outline-none" placeholder="0123456789">
                </div>
                <div>
                    <label class="text-[10px] text-gray-500 uppercase font-bold tracking-wide mb-1 block">Account Name</label>
                    <input type="text" name="account_name" class="w-full border border-gray-200 rounded-xl px-3 py-2.5 text-sm focus:border-primary outline-none" placeholder="John Doe">
                </div>
            </div>
            <div class="mb-4">
                <label class="text-[10px] text-gray-500 uppercase font-bold tracking-wide mb-1 block">Why do you want to sell? <span class="text-gray-300">(optional)</span></label>
                <textarea name="reason" rows="2" class="w-full border border-gray-200 rounded-xl px-3 py-2.5 text-sm focus:border-primary outline-none resize-none" placeholder="Tell us about the accounts you plan to sell..."></textarea>
            </div>
            <div class="bg-purple-50 border border-purple-100 rounded-xl p-3 mb-4 flex items-center gap-3">
                <i class="fa-solid fa-wallet text-primary"></i>
                <p class="text-xs text-purple-800">Registration fee: <strong>₦<?php echo number_format($seller_fee, 2); ?></strong> (one-time, deducted from wallet)</p>
            </div>
            <button type="submit" class="w-full bg-primary hover:bg-purple-800 text-white font-bold text-sm py-3 rounded-xl transition-colors flex items-center justify-center gap-2">
                <i class="fa-solid fa-paper-plane"></i> Submit Application & Pay ₦<?php echo number_format($seller_fee, 0); ?>
            </button>
        </form>
    </div>
</section>
<?php endif; ?>

<?php include('footer.php'); ?>
