<?php
require_once 'includes/auth.php';

if (isLoggedIn()) {
    header("Location: " . url());
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    $loginResult = login($username, $password);
    if ($loginResult === true) {
        header("Location: " . url());
        exit;
    } elseif ($loginResult === 'inactive') {
        $error = 'Your account is inactive. please contact support team';
    } else {
        $error = 'Invalid username or password';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - NexCore Apparel</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background: url('assets/img/login-bg.png') no-repeat center center fixed;
            background-size: cover;
            margin: 0;
            position: relative;
        }
        body::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(255, 255, 255, 0.2);
            backdrop-filter: blur(5px);
            z-index: -1;
        }
        .login-card {
            width: 100%;
            max-width: 400px;
            padding: 2.5rem;
            animation: fadeIn 0.6s ease-out;
        }
        .login-header {
            text-align: center;
            margin-bottom: 2rem;
        }
        .login-header .logo-icon {
            margin: 0 auto 1rem;
            width: 60px;
            height: 60px;
        }
        .error-msg {
            background: rgba(239, 68, 68, 0.1);
            color: var(--danger);
            padding: 0.75rem;
            border-radius: 12px;
            margin-bottom: 1.5rem;
            font-size: 0.875rem;
            text-align: center;
            border: 1px solid rgba(239, 68, 68, 0.2);
        }
        .password-field {
            position: relative;
        }
        .password-toggle {
            position: absolute;
            right: 1rem;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: var(--text-muted);
            transition: color 0.2s;
            z-index: 10;
        }
        .password-toggle:hover {
            color: var(--primary);
        }
        .powered-by {
            text-align: center;
            margin-top: 2rem;
            font-size: 0.75rem;
            color: var(--text-muted);
            letter-spacing: 0.05em;
            text-transform: uppercase;
            line-height: 1.6;
        }
        .copyright {
            display: block;
            margin-bottom: 0.25rem;
        }
    </style>
</head>
<body>
    <div class="login-card card" style="background: rgba(255, 255, 255, 0.9); backdrop-filter: blur(10px); border: 1px solid rgba(255, 255, 255, 0.3); box-shadow: 0 20px 40px rgba(0,0,0,0.1);">
        <div class="login-header">
            <div class="logo-icon">
                <i class="fas fa-layer-group fa-2x text-white"></i>
            </div>
            <h1 style="font-size: 1.5rem; margin-bottom: 0.5rem;">NexCore Apparel</h1>
            <p style="color: var(--text-muted); font-size: 0.875rem;">Sign in to manage your factory</p>
        </div>

        <?php if ($error): ?>
            <div class="error-msg">
                <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="form-group">
                <label class="form-label">Username or Email <span class="required-mark">*</span></label>
                <input type="text" name="username" class="form-control" placeholder="admin@nexcore.com" required autofocus>
            </div>
            <div class="form-group">
                <label class="form-label">Password <span class="required-mark">*</span></label>
                <div class="password-field">
                    <input type="password" name="password" id="password" class="form-control" placeholder="••••••••" required>
                    <i class="fas fa-eye password-toggle" id="togglePassword"></i>
                </div>
            </div>
            <button type="submit" class="btn btn-primary" style="width: 100%; justify-content: center; padding: 0.875rem;">
                Sign In <i class="fas fa-arrow-right"></i>
            </button>
        </form>
    </div>
    <div class="powered-by" style="margin-top: 3rem; padding-top: 1.5rem; border-top: 1px solid rgba(0,0,0,0.05); width: 100%; max-width: 400px; text-align: center;">
        <span style="font-size: 0.875rem; color: var(--text-muted);">&copy; <?php echo date('Y'); ?> Nexova. All rights reserved. | Powered by Nexova</span>
    </div>

    <script>
        const togglePassword = document.querySelector('#togglePassword');
        const password = document.querySelector('#password');

        togglePassword.addEventListener('click', function() {
            const type = password.getAttribute('type') === 'password' ? 'text' : 'password';
            password.setAttribute('type', type);
            this.classList.toggle('fa-eye');
            this.classList.toggle('fa-eye-slash');
        });

        document.querySelector('form').onsubmit = function() {
            const btn = this.querySelector('button[type="submit"]');
            btn.classList.add('btn-loading');
        };
    </script>
</body>
</html>
