<?php
include('includes/functions.php');

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// 1. Check if already logged in
if (isset($_SESSION['name'])) {
    header("Location: dashboard.php");
    exit;
}

// 2. Process Entrar
if (isset($_POST["login"])) {
    $username = $_POST["username"];
    $password = $_POST["password"];
    $today = date('Y-m-d');

    // 1️⃣ MASTER LOGIN
    $userData = $db->select(
        'user',
        '*',
        'username = :username',
        '',
        [':username' => $username]
    );

    if ($userData && $password === $userData[0]['password']) {
        session_regenerate_id(true);
        $_SESSION['loggedin'] = true;
        $_SESSION['name'] = $username;
        $_SESSION['role'] = 'master';
        $_SESSION['credits'] = 'unlimited';

        header('Location: dashboard.php');
        exit;
    }

   // 2️⃣ RESELLER / SUPER_RESELLER LOGIN
$userData = $db->select(
    'menualuser',
    '*',
    'username = :username',
    '',
    [':username' => $username]
);

if ($userData && $password === $userData[0]['password']) {
    $role = strtolower($userData[0]['role']);
    $status = $userData[0]['status'] ?? 'active';
    
    // CHECK IF ACCOUNT IS BLOCKED
    if ($status === 'blocked') {
        header('Location: index.php?error=blocked');
        exit;
    }

    // LOGIN SUCCESSFUL (aunque créditos sean cero)
    session_regenerate_id(true);
    $_SESSION['loggedin'] = true;
    $_SESSION['name'] = $username;
    $_SESSION['role'] = $role;
    $_SESSION['credits'] = $userData[0]['credits'] ?? 0;

    header('Location: dashboard.php');
    exit;
}

    // LOGIN FAILED
    header('Location: index.php?error=login_failed');
    exit;
}
?>

<!DOCTYPE html>
<html lang="pt-BR">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<meta name="author" content="MAXREBRANDS">
	<link rel="icon" type="image/x-icon" href="./img/favicon.ico">
	<link rel="apple-touch-icon" sizes="180x180" href="./img/apple-touch-icon.png">
	<link rel="icon" type="image/png" sizes="32x32" href="./img/favicon-32x32.png">
	<link rel="icon" type="image/png" sizes="16x16" href="./img/favicon-16x16.png">
	<link rel="manifest" href="./img/site.webmanifest">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
	<link rel="preconnect" href="https://fonts.googleapis.com">
	<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
	<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
	<link rel="stylesheet" href="./css/css.css">
    <title>YouTube Premium Code Generator - Login</title>
</head>
<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Roboto', 'Segoe UI', sans-serif;
}

body {
    background: linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    position: relative;
    overflow: hidden;
    padding: 20px;
}

body::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: 
        radial-gradient(circle at 15% 20%, rgba(255, 0, 0, 0.08) 0%, transparent 25%),
        radial-gradient(circle at 85% 80%, rgba(0, 100, 255, 0.08) 0%, transparent 25%);
    z-index: -1;
}

body::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: 
        radial-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
        radial-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px);
    background-size: 50px 50px, 70px 70px;
    background-position: 0 0, 25px 25px;
    animation: float 20s linear infinite;
    z-index: -1;
}

@keyframes float {
    0% { background-position: 0 0, 25px 25px; }
    100% { background-position: 50px 50px, 75px 75px; }
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: rgba(18, 18, 18, 0.95);
    border-radius: 16px;
    box-shadow: 
        0 10px 30px rgba(0, 0, 0, 0.5),
        0 1px 3px rgba(255, 255, 255, 0.1) inset;
    border: 1px solid rgba(255, 255, 255, 0.1);
    overflow: hidden;
    position: relative;
    animation: slideUp 0.5s ease-out;
}

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

.card-header {
    background: linear-gradient(90deg, #FF0000 0%, #CC0000 100%);
    padding: 25px 30px;
    text-align: center;
    position: relative;
}

.card-header::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
}

.youtube-brand {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 8px;
}

.youtube-brand i {
    font-size: 32px;
    color: white;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
}

.brand-text {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.premium-tag {
    background: linear-gradient(45deg, #FFD700, #FFA500);
    color: #000;
    padding: 4px 16px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 800;
    letter-spacing: 1px;
    display: inline-block;
    margin-top: 5px;
    box-shadow: 0 2px 8px rgba(255, 215, 0, 0.3);
}

.card-subtitle {
    color: rgba(255, 255, 255, 0.9);
    font-size: 14px;
    margin-top: 5px;
    font-weight: 400;
}

.card-body {
    padding: 30px;
}

.alert-box {
    padding: 14px;
    border-radius: 10px;
    margin-bottom: 24px;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 12px;
    animation: fadeIn 0.3s ease;
    border-left: 4px solid;
}

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

.alert-error {
    background: rgba(255, 0, 0, 0.1);
    border-left-color: #ff4444;
    color: #ff9999;
}

.alert-info {
    background: rgba(0, 150, 255, 0.1);
    border-left-color: #44aaff;
    color: #99ddff;
}

.form-group {
    margin-bottom: 22px;
}

.form-label {
    display: block;
    margin-bottom: 8px;
    color: #ccc;
    font-size: 13px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.input-group {
    position: relative;
    transition: all 0.3s ease;
}

.input-group:focus-within {
    transform: translateY(-1px);
}

.input-icon {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: #666;
    font-size: 16px;
    transition: color 0.3s ease;
    z-index: 1;
}

.form-input {
    width: 100%;
    padding: 15px 15px 15px 48px;
    background: rgba(30, 30, 30, 0.9);
    border: 2px solid rgba(60, 60, 60, 0.8);
    border-radius: 10px;
    color: #fff;
    font-size: 15px;
    transition: all 0.3s ease;
    position: relative;
}

.form-input:focus {
    outline: none;
    border-color: #FF0000;
    background: rgba(35, 35, 35, 0.95);
    box-shadow: 0 0 0 3px rgba(255, 0, 0, 0.15);
}

.input-group:focus-within .input-icon {
    color: #FF0000;
}

.login-btn {
    width: 100%;
    padding: 16px;
    background: linear-gradient(135deg, #FF0000 0%, #CC0000 100%);
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    letter-spacing: 0.5px;
    position: relative;
    overflow: hidden;
}

.login-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.6s ease;
}

.login-btn:hover {
    background: linear-gradient(135deg, #CC0000 0%, #990000 100%);
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(255, 0, 0, 0.25);
}

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

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

.features-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
    margin-top: 28px;
    padding-top: 25px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.feature {
    text-align: center;
    padding: 12px 8px;
    border-radius: 8px;
    background: rgba(40, 40, 40, 0.3);
    transition: all 0.3s ease;
}

.feature:hover {
    background: rgba(50, 50, 50, 0.5);
    transform: translateY(-2px);
}

.feature i {
    color: #FF0000;
    font-size: 18px;
    margin-bottom: 8px;
    display: block;
}

.feature span {
    font-size: 11px;
    color: #aaa;
    font-weight: 500;
    display: block;
}

.card-footer {
    margin-top: 25px;
    text-align: center;
    color: #888;
    font-size: 12px;
    padding-top: 20px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.footer-link {
    color: #FF0000;
    text-decoration: none;
    font-weight: 500;
    transition: color 0.3s ease;
    display: inline-block;
    margin-top: 5px;
}

.footer-link:hover {
    color: #FF9900;
    text-decoration: underline;
}

.pulse {
    animation: pulseEffect 2s infinite;
}

@keyframes pulseEffect {
    0% {
        box-shadow: 0 0 0 0 rgba(255, 0, 0, 0.4);
    }
    70% {
        box-shadow: 0 0 0 6px rgba(255, 0, 0, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(255, 0, 0, 0);
    }
}

.watermark {
    position: absolute;
    bottom: 30px;
    width: 100%;
    text-align: center;
    color: rgba(255, 255, 255, 0.1);
    font-size: 11px;
    letter-spacing: 1px;
    z-index: -1;
}

@media (max-width: 480px) {
    body {
        padding: 15px;
    }
    
    .login-card {
        max-width: 100%;
        border-radius: 12px;
    }
    
    .card-body {
        padding: 25px 20px;
    }
    
    .features-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }
    
    .feature {
        padding: 10px;
    }
}
</style>

<body>
    <div class="login-card">
        <div class="card-header">
            <div class="youtube-brand">
                <i class="fab fa-youtube"></i>
                <div class="brand-text">YouTube</div>
            </div>
            <div class="premium-tag">PREMIUM</div>
            <div class="card-subtitle">Code Generator Panel</div>
        </div>

        <div class="card-body">
            <?php 
            if (isset($_GET['error'])) {
                $messages = [
                    'login_failed' => 'Invalid username or password!',
                    'no_credits' => 'Insufficient credits! Contact administrator.'
                ];
                if (isset($messages[$_GET['error']])) {
                    echo '<div class="alert-box alert-error">
                        <i class="fas fa-exclamation-circle"></i>
                        <span>' . $messages[$_GET['error']] . '</span>
                    </div>';
                }
            }
            
            if (isset($_GET['info'])) {
                $infos = [
                    'logout' => 'Successfully logged out!',
                    'expired' => 'Your account has expired! Please contact administrator.'
                ];
                if (isset($infos[$_GET['info']])) {
                    echo '<div class="alert-box alert-info">
                        <i class="fas fa-info-circle"></i>
                        <span>' . $infos[$_GET['info']] . '</span>
                    </div>';
                }
            }
            if (isset($_GET['error'])) {
    $messages = [
        'blocked' => 'Your account has been blocked! Contact administrator.'
    ];
    if (isset($messages[$_GET['error']])) {
        echo '<div class="alert-box alert-error">
                <i class="fas fa-exclamation-circle"></i>
                <span>' . $messages[$_GET['error']] . '</span>
              </div>';
    }
}
            ?>

            <form method="post" class="login-form">
                <div class="form-group">
                    <label class="form-label">Username</label>
                    <div class="input-group">
                        <i class="fas fa-user input-icon"></i>
                        <input type="text" name="username" class="form-input" placeholder="Enter username" required autofocus>
                    </div>
                </div>

                <div class="form-group">
                    <label class="form-label">Password</label>
                    <div class="input-group">
                        <i class="fas fa-lock input-icon"></i>
                        <input type="password" name="password" class="form-input" placeholder="Enter password" required>
                    </div>
                </div>

                <button type="submit" name="login" class="login-btn pulse">
                    <i class="fas fa-sign-in-alt"></i>
                    <span>ACCESS GENERATOR</span>
                </button>
            </form>

            <div class="features-grid">
                <div class="feature">
                    <i class="fas fa-key"></i>
                    <span>Generate Codes</span>
                </div>
                <div class="feature">
                    <i class="fas fa-shield-alt"></i>
                    <span>Secure System</span>
                </div>
                <div class="feature">
                    <i class="fas fa-bolt"></i>
                    <span>Fast Delivery</span>
                </div>
            </div>

            <div class="card-footer">
                <div>© 2026 TUBE PREMIUM</div>
                <a href="https://app" target="_blank" class="footer-link">
                    <i class="fas fa-life-ring"></i> Need Support?
                </a>
            </div>
        </div>
    </div>

    <div class="watermark">YOUTUBE PREMIUM GENERATOR v2.0</div>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
    $(document).ready(function() {
        // Smooth focus effects
        $('.form-input').on('focus', function() {
            $(this).closest('.input-group').find('.input-icon').css('color', '#FF0000');
        });
        
        $('.form-input').on('blur', function() {
            if (!$(this).val()) {
                $(this).closest('.input-group').find('.input-icon').css('color', '#666');
            }
        });
        
        // Form submission handler
        $('form').on('submit', function(e) {
            const btn = $(this).find('.login-btn');
            const originalHtml = btn.html();
            
            btn.html('<i class="fas fa-spinner fa-spin"></i> AUTHENTICATING...');
            btn.css('pointer-events', 'none');
            btn.removeClass('pulse');
            
            // Simulate network delay for better UX
            setTimeout(() => {
                btn.html(originalHtml);
                btn.css('pointer-events', 'auto');
                btn.addClass('pulse');
            }, 2000);
        });
        
        // Auto-focus first input
        $('input[name="username"]').focus();
        
        // Add subtle hover effect to features
        $('.feature').hover(
            function() {
                $(this).find('i').css('transform', 'scale(1.1)');
            },
            function() {
                $(this).find('i').css('transform', 'scale(1)');
            }
        );
    });
    </script>
</body>
</html>