<?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="es-ES">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="author" content="RGVIP">
    <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=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <title>SpoTube Premium Panel - Login</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            position: relative;
            overflow-x: hidden;
        }

        /* Fondo animado con partículas mejoradas */
        #particles-canvas {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 0;
            opacity: 0.6;
        }

        /* Overlay de gradiente dinámico - Colores Spotify (verde) */
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: radial-gradient(circle at 20% 50%, rgba(29, 185, 84, 0.15) 0%, transparent 50%),
                        radial-gradient(circle at 80% 80%, rgba(30, 215, 96, 0.1) 0%, transparent 50%);
            z-index: 0;
            animation: gradientShift 10s ease-in-out infinite;
        }

        @keyframes gradientShift {
            0%, 100% { opacity: 0.5; }
            50% { opacity: 1; }
        }

        /* Tarjeta principal con glassmorphism 3D */
        .login-container {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 450px;
            margin: 20px;
            animation: floatIn 0.8s cubic-bezier(0.34, 1.2, 0.64, 1);
        }

        @keyframes floatIn {
            from {
                opacity: 0;
                transform: translateY(50px) scale(0.95);
            }
            to {
                opacity: 1;
                transform: translateY(0) scale(1);
            }
        }

        .login-card {
            background: rgba(18, 18, 18, 0.92);
            backdrop-filter: blur(20px);
            border-radius: 32px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            box-shadow: 
                0 25px 50px -12px rgba(0, 0, 0, 0.5),
                0 0 0 1px rgba(255, 255, 255, 0.05) inset;
            overflow: hidden;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .login-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 30px 60px -12px rgba(0, 0, 0, 0.6);
        }

        /* Header con gradiente estilo Spotify */
        .card-header {
            background: linear-gradient(135deg, #1DB954 0%, #1ed760 50%, #1aa34a 100%);
            background-size: 200% 200%;
            animation: gradientBG 3s ease infinite;
            padding: 35px 30px;
            text-align: center;
            position: relative;
            overflow: hidden;
        }

        @keyframes gradientBG {
            0%, 100% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
        }

        .card-header::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: rotate 20s linear infinite;
        }

        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }

        /* Logo animado - Mezcla Spotify y YouTube */
        .spoptube-brand {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
            margin-bottom: 10px;
            position: relative;
            z-index: 1;
        }

        .spoptube-brand i:first-child {
            font-size: 45px;
            color: #1DB954;
            filter: drop-shadow(0 0 20px rgba(29, 185, 84, 0.5));
            animation: iconPulse 2s ease-in-out infinite;
        }

        .spoptube-brand i:last-child {
            font-size: 42px;
            color: #FF0000;
            filter: drop-shadow(0 0 20px rgba(255, 0, 0, 0.4));
            animation: iconPulse 2s ease-in-out infinite 0.3s;
        }

        @keyframes iconPulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }

        .brand-text {
            font-size: 32px;
            font-weight: 800;
            letter-spacing: -0.5px;
            background: linear-gradient(135deg, #1DB954, #FF0000);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            text-shadow: none;
        }

        .premium-badge {
            display: inline-block;
            background: linear-gradient(135deg, #1DB954, #1ed760);
            padding: 6px 20px;
            border-radius: 40px;
            font-size: 12px;
            font-weight: 800;
            letter-spacing: 1px;
            color: #000;
            margin: 8px 0;
            position: relative;
            z-index: 1;
            animation: badgeGlow 2s ease-in-out infinite;
        }

        @keyframes badgeGlow {
            0%, 100% { box-shadow: 0 0 5px rgba(29, 185, 84, 0.5); }
            50% { box-shadow: 0 0 20px rgba(29, 185, 84, 0.8); }
        }

        /* Alertas modernas */
        .alert-modern {
            padding: 14px 18px;
            border-radius: 16px;
            margin-bottom: 24px;
            font-size: 13px;
            display: flex;
            align-items: center;
            gap: 12px;
            animation: slideInLeft 0.4s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

        @keyframes slideInLeft {
            from {
                opacity: 0;
                transform: translateX(-20px);
            }
            to {
                opacity: 1;
                transform: translateX(0);
            }
        }

        .alert-error {
            background: rgba(255, 68, 68, 0.15);
            border-left: 4px solid #ff4444;
            color: #ff9999;
        }

        .alert-success {
            background: rgba(29, 185, 84, 0.15);
            border-left: 4px solid #1DB954;
            color: #1DB954;
        }

        .alert-info {
            background: rgba(68, 136, 255, 0.15);
            border-left: 4px solid #4488ff;
            color: #99bbff;
        }

        /* Campos de entrada con efecto neón estilo Spotify */
        .form-group {
            margin-bottom: 25px;
        }

        .form-label {
            display: block;
            margin-bottom: 10px;
            color: #b3b3b3;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        .input-wrapper {
            position: relative;
        }

        .input-icon {
            position: absolute;
            left: 18px;
            top: 50%;
            transform: translateY(-50%);
            color: #666;
            font-size: 18px;
            transition: all 0.3s ease;
            pointer-events: none;
        }

        .form-input {
            width: 100%;
            padding: 16px 18px 16px 52px;
            background: rgba(0, 0, 0, 0.5);
            border: 2px solid rgba(255, 255, 255, 0.1);
            border-radius: 16px;
            color: #fff;
            font-size: 15px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .form-input:focus {
            outline: none;
            border-color: #1DB954;
            background: rgba(0, 0, 0, 0.7);
            box-shadow: 0 0 0 4px rgba(29, 185, 84, 0.1);
        }

        .form-input:focus + .input-icon {
            color: #1DB954;
            transform: translateY(-50%) scale(1.1);
        }

        /* Botón estilo Spotify */
        .login-btn {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #1DB954, #1aa34a);
            border: none;
            border-radius: 40px;
            color: white;
            font-size: 16px;
            font-weight: 700;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            position: relative;
            overflow: hidden;
            transition: all 0.3s ease;
            margin-top: 10px;
        }

        .login-btn::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 0;
            height: 0;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.3);
            transform: translate(-50%, -50%);
            transition: width 0.6s, height 0.6s;
        }

        .login-btn:hover::before {
            width: 300px;
            height: 300px;
        }

        .login-btn:hover {
            transform: scale(1.01);
            box-shadow: 0 10px 30px rgba(29, 185, 84, 0.4);
        }

        .login-btn:active {
            transform: scale(0.98);
        }

        /* Grid de características con efecto glass */
        .features-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 12px;
            margin-top: 32px;
            padding-top: 28px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }

        .feature-item {
            text-align: center;
            padding: 12px 8px;
            background: rgba(255, 255, 255, 0.03);
            border-radius: 14px;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .feature-item:hover {
            background: rgba(29, 185, 84, 0.1);
            transform: translateY(-3px);
        }

        .feature-item i {
            font-size: 24px;
            color: #1DB954;
            margin-bottom: 8px;
            display: block;
            transition: transform 0.3s ease;
        }

        .feature-item:hover i {
            transform: scale(1.1);
        }

        .feature-item span {
            font-size: 11px;
            color: #b3b3b3;
            font-weight: 500;
        }

        /* Footer */
        .card-footer {
            margin-top: 28px;
            text-align: center;
            padding-top: 20px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }

        .footer-text {
            color: #666;
            font-size: 11px;
            margin-bottom: 8px;
        }

        .footer-link {
            color: #1DB954;
            text-decoration: none;
            font-size: 12px;
            font-weight: 600;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }

        .footer-link:hover {
            color: #1ed760;
            gap: 8px;
        }

        /* Animaciones adicionales */
        @keyframes shimmer {
            0% { background-position: -1000px 0; }
            100% { background-position: 1000px 0; }
        }

        .loading-shimmer {
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent);
            background-size: 1000px 100%;
            animation: shimmer 1.5s infinite;
        }

        /* Responsive */
        @media (max-width: 480px) {
            .login-container {
                margin: 15px;
            }
            
            .card-header {
                padding: 25px 20px;
            }
            
            .brand-text {
                font-size: 24px;
            }
            
            .spoptube-brand i:first-child,
            .spoptube-brand i:last-child {
                font-size: 28px;
            }
            
            .features-grid {
                gap: 8px;
            }
            
            .feature-item {
                padding: 10px 5px;
            }
        }
    
        /* Animaciones desactivadas por configuración */
        *, *::before, *::after {
            animation: none !important;
            transition: none !important;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <div class="card-header">
                <div class="spoptube-brand">
                    <i class="fab fa-spotify"></i>
                    <i class="fab fa-youtube"></i>
                    <div class="brand-text">SpoTube</div>
                </div>
                <div class="premium-badge">PREMIUM GENERATOR</div>
                <div style="font-size: 12px; opacity: 0.9; margin-top: 8px;">Premium Codes · Music Streaming</div>
            </div>

            <div style="padding: 32px;">
                <?php 
                if (isset($_GET['error'])) {
                    $messages = [
                        'login_failed' => 'Invalid username or password!',
                        'no_credits' => 'Insufficient credits! Please contact administrator.',
                        'blocked' => 'Your account has been blocked! Contact administrator.',
                        'expired' => 'Your account has expired! Please renew.'
                    ];
                    if (isset($messages[$_GET['error']])) {
                        echo '<div class="alert-modern alert-error">
                            <i class="fas fa-exclamation-triangle"></i>
                            <span>' . $messages[$_GET['error']] . '</span>
                        </div>';
                    }
                }
                
                if (isset($_GET['info'])) {
                    $infos = [
                        'logout' => 'Successfully logged out! See you soon.',
                        'registered' => 'Account created successfully! Please login.'
                    ];
                    if (isset($infos[$_GET['info']])) {
                        echo '<div class="alert-modern alert-success">
                            <i class="fas fa-check-circle"></i>
                            <span>' . $infos[$_GET['info']] . '</span>
                        </div>';
                    }
                }
                ?>

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

                    <div class="form-group">
                        <label class="form-label">Password</label>
                        <div class="input-wrapper">
                            <i class="fas fa-lock input-icon"></i>
                            <input type="password" name="password" class="form-input" placeholder="Enter your password" required>
                            <i class="fas fa-eye-slash toggle-password" style="position: absolute; right: 18px; top: 50%; transform: translateY(-50%); cursor: pointer; color: #666; z-index: 2;"></i>
                        </div>
                    </div>

                    <button type="submit" name="login" class="login-btn" id="loginBtn">
                        <i class="fas fa-headphones-alt"></i>
                        <span>Access SpoTube Panel</span>
                    </button>
                </form>

                <div class="features-grid">
                    <div class="feature-item">
                        <i class="fas fa-music"></i>
                        <span>Unlimited Music</span>
                    </div>
                    <div class="feature-item">
                        <i class="fas fa-video"></i>
                        <span>Video & Audio</span>
                    </div>
                    <div class="feature-item">
                        <i class="fas fa-download"></i>
                        <span>Offline Mode</span>
                    </div>
                </div>

                <div class="card-footer">
                    <div class="footer-text">© 2025 SpoTube Panel - All rights reserved</div>
                    <a href="https://apps-rgvip.com/" target="_blank" class="footer-link">
                        <i class="fas fa-headset"></i>
                        <span>Need Support?</span>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {

            // Toggle password visibility
            $('.toggle-password').click(function() {
                const input = $(this).prev('input');
                const type = input.attr('type') === 'password' ? 'text' : 'password';
                input.attr('type', type);
                $(this).toggleClass('fa-eye-slash fa-eye');
            });

            // Form submission con animación mejorada
            $('#loginForm').on('submit', function(e) {
                const btn = $('#loginBtn');
                const originalHtml = btn.html();
                
                btn.html('<i class="fas fa-spinner"></i><span>Authenticating...</span>');
                btn.css('opacity', '0.8');
                btn.css('pointer-events', 'none');
                
                // Si quieres mantener la animación pero dejar que el formulario envíe normalmente
                // no uses setTimeout con preventDefault, o descomenta la línea siguiente
                // e.preventDefault();
                
                // Si quieres simular delay (solo visual, quita el preventDefault de arriba)
                // setTimeout(() => {
                //     btn.html(originalHtml);
                //     btn.css('opacity', '1');
                //     btn.css('pointer-events', 'auto');
                // }, 2000);
            });

        });
    </script>
</body>
</html>