<?php
/**
 * Login Page - SufarmaERP
 * Gated administrative access page.
 */
if (session_status() === PHP_SESSION_NONE) {
    ini_set('session.cookie_lifetime', 0);
    session_start();
}

// Redirect if already logged in
if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {
    header('Location: modules/general/dashboard.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $userCode = isset($_POST['user_code']) ? trim($_POST['user_code']) : '';

    if ($userCode === 'Mayra777') {
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['user_id'] = 12; // Mayra
        $_SESSION['usuario_id'] = 12;
        $_SESSION['nombre'] = 'Mayra';
        $_SESSION['usuario'] = 'Mayra777';
        
        header('Location: modules/general/dashboard.php');
        exit;
    } elseif ($userCode === 'Wil2121') {
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['user_id'] = 13; // Wilson
        $_SESSION['usuario_id'] = 13;
        $_SESSION['nombre'] = 'Wilson';
        $_SESSION['usuario'] = 'Wil2121';
        
        header('Location: modules/general/dashboard.php');
        exit;
    } else {
        $error = 'Código de usuario incorrecto o sin privilegios de administrador.';
    }
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sufarma ERP - Acceso Administrativo</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=League+Spartan:wght@100..900&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
        
        :root {
            --bg-gradient: linear-gradient(135deg, #0B0119 0%, #020e17 50%, #0B0119 100%);
            --surface: rgba(2, 14, 23, 0.85); /* Darkest Ocean Blue with opacity */
            --border-color: rgba(210, 200, 221, 0.15); /* Lavender Plum border */
            --text-primary: #FFFFFF; /* White */
            --text-light: #D2C8DD; /* Lavender Plum */
            --accent-color: #D2C8DD; /* Lavender Plum */
            --accent-hover: #052137; /* Darker Ocean Blue */
            --accent-glow: rgba(5, 33, 55, 0.35);
            --danger: #EF4444;
            --radius-lg: 1.25rem;
            --font-sans: 'Poppins', -apple-system, BlinkMacSystemFont, sans-serif;
        }

        body {
            font-family: var(--font-sans);
            background: var(--bg-gradient);
            color: var(--text-primary);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 1rem;
            margin: 0;
            overflow: hidden;
        }

        .login-container {
            width: 100%;
            max-width: 420px;
            perspective: 1000px;
        }

        .login-card {
            background: var(--surface);
            backdrop-filter: blur(20px) saturate(180%);
            -webkit-backdrop-filter: blur(20px) saturate(180%);
            border: 1px solid var(--border-color);
            border-radius: var(--radius-lg);
            padding: 2.5rem 2rem;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.6);
            display: flex;
            flex-direction: column;
            gap: 1.75rem;
            animation: cardEntrance 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
        }

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

        .login-header {
            text-align: center;
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
        }

        .logo-icon {
            font-size: 2.5rem;
            background: linear-gradient(135deg, #D2C8DD, #6B5A7C);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 0.5rem;
        }

        .login-title {
            font-family: 'League Spartan', sans-serif;
            font-size: 1.65rem;
            font-weight: 700;
            letter-spacing: -0.025em;
            background: linear-gradient(to right, #ffffff, #D2C8DD);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .login-subtitle {
            font-size: 0.825rem;
            color: var(--text-light);
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
        }

        .input-wrapper {
            position: relative;
            display: flex;
            align-items: center;
        }

        .input-icon {
            position: absolute;
            left: 1rem;
            color: var(--text-light);
            font-size: 0.95rem;
        }

        .input-text {
            width: 100%;
            background: rgba(17, 24, 39, 0.5);
            border: 1px solid var(--border-color);
            border-radius: 0.5rem;
            padding: 0.8rem 1rem 0.8rem 2.75rem;
            color: var(--text-primary);
            font-size: 0.95rem;
            outline: none;
            transition: all 0.3s ease;
        }

        .input-text:focus {
            border-color: var(--accent-color);
            box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.15);
            background: rgba(17, 24, 39, 0.8);
        }

        .btn-submit {
            background: var(--accent-color);
            color: #052e16;
            font-size: 0.95rem;
            font-weight: 600;
            padding: 0.85rem;
            border-radius: 0.5rem;
            border: none;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            transition: all 0.3s ease;
            margin-top: 0.5rem;
        }

        .btn-submit:hover {
            background: var(--accent-hover);
            box-shadow: 0 0 15px var(--accent-glow);
        }

        .error-message {
            background: rgba(239, 68, 68, 0.1);
            border: 1px solid rgba(239, 68, 68, 0.25);
            color: #f87171;
            padding: 0.75rem 1rem;
            border-radius: 0.5rem;
            font-size: 0.8rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            animation: shake 0.35s ease-in-out;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-4px); }
            75% { transform: translateX(4px); }
        }

        .footer-note {
            text-align: center;
            font-size: 0.725rem;
            color: var(--text-light);
            margin-top: 1rem;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <div class="login-header">
                <i class="fas fa-shield-halved logo-icon"></i>
                <h1 class="login-title">Sufarma ERP</h1>
                <p class="login-subtitle">Ingrese su código administrativo para acceder al sistema</p>
            </div>

            <?php if (!empty($error)): ?>
                <div class="error-message">
                    <i class="fas fa-triangle-exclamation"></i>
                    <span><?= htmlspecialchars($error) ?></span>
                </div>
            <?php endif; ?>

            <form method="POST" action="" style="display: flex; flex-direction: column; gap: 1.25rem;">
                <div class="form-group">
                    <label class="form-label" style="font-size: 0.8rem; color: var(--text-light); font-weight: 500;" for="user_code">Código de Acceso</label>
                    <div class="input-wrapper">
                        <i class="fas fa-key input-icon"></i>
                        <input type="password" class="input-text" id="user_code" name="user_code" placeholder="••••••••" required autofocus autocomplete="off">
                    </div>
                </div>

                <button type="submit" class="btn-submit">
                    <i class="fas fa-sign-in-alt"></i> Entrar al Sistema
                </button>
            </form>
        </div>
        <div class="footer-note">
            <i class="fas fa-lock" style="margin-right: 3px;"></i> Conexión encriptada y segura.
        </div>
    </div>
</body>
</html>
