<?php
// login.php - صفحه ورود با طراحی بر اساس تصویر

header('Content-Type: text/html; charset=UTF-8');

require_once 'config/database.php';
require_once 'includes/db.php';
require_once 'includes/auth.php';
require_once 'includes/settings.php';

if (isLoggedIn()) {
    redirectToDashboard();
}

// ===== دریافت تنظیمات سامانه =====
$settings = getSettings();
$siteName = $settings['site_name'] ?? 'سامانه مدیریت اساتید';
$institutionName = $settings['institution_name'] ?? 'دانشگاه علوم و فناوری';
$logoPath = !empty($settings['logo']) ? $settings['logo'] : '';
$developerName = $settings['footer_developer'] ?? '';
$footerText = $settings['footer_text'] ?? '';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (login($username, $password)) {
        redirectToDashboard();
    } else {
        $error = 'نام کاربری یا رمز عبور اشتباه است';
    }
}
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ورود | <?= htmlspecialchars($siteName) ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        /* ============================================================
           RESET & BASE
           ============================================================ */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Vazirmatn', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            padding: 20px;
            direction: rtl;
        }
        
        /* ============================================================
           BACKGROUND ANIMATION
           ============================================================ */
        .bg-animation {
            position: fixed;
            inset: 0;
            z-index: 0;
            overflow: hidden;
            pointer-events: none;
        }
        
        .bg-animation .orb {
            position: absolute;
            border-radius: 50%;
            filter: blur(80px);
            opacity: 0.3;
            animation: floatOrb 20s infinite ease-in-out;
        }
        
        .bg-animation .orb:nth-child(1) {
            width: 400px;
            height: 400px;
            background: #667eea;
            top: -100px;
            right: -100px;
            animation-delay: 0s;
        }
        
        .bg-animation .orb:nth-child(2) {
            width: 300px;
            height: 300px;
            background: #764ba2;
            bottom: -80px;
            left: -80px;
            animation-delay: -7s;
        }
        
        .bg-animation .orb:nth-child(3) {
            width: 200px;
            height: 200px;
            background: #f093fb;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            animation-delay: -14s;
        }
        
        @keyframes floatOrb {
            0%, 100% { transform: translate(0, 0) scale(1); }
            25% { transform: translate(60px, -40px) scale(1.1); }
            50% { transform: translate(-30px, 60px) scale(0.9); }
            75% { transform: translate(40px, 30px) scale(1.05); }
        }
        
        /* ============================================================
           LOGIN CONTAINER (Glassmorphism)
           ============================================================ */
        .login-container {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 440px;
            padding: 48px 40px 36px;
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(24px);
            -webkit-backdrop-filter: blur(24px);
            border: 1px solid rgba(255, 255, 255, 0.08);
            border-radius: 32px;
            box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.05);
            animation: slideUp 0.6s ease-out;
        }
        
        @keyframes slideUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        /* ============================================================
           LOGO
           ============================================================ */
        .logo-wrapper {
            text-align: center;
            margin-bottom: 32px;
        }
        
        .logo-icon {
            width: 80px;
            height: 80px;
            border-radius: 20px;
            margin: 0 auto 16px;
            overflow: hidden;
            box-shadow: 0 12px 40px rgba(102, 126, 234, 0.3);
            animation: pulseLogo 3s infinite ease-in-out;
        }
        
        .logo-icon img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            display: block;
        }
        
        .logo-icon .default-logo {
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #667eea, #764ba2);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 36px;
            color: #fff;
        }
        
        @keyframes pulseLogo {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }
        
        .logo-wrapper h1 {
            color: #fff;
            font-size: 22px;
            font-weight: 700;
            letter-spacing: 0.5px;
        }
        
        .logo-wrapper h1 span {
            color: #818cf8;
        }
        
        .logo-wrapper .subtitle {
            color: rgba(255, 255, 255, 0.35);
            font-size: 13px;
            margin-top: 4px;
            font-weight: 300;
        }
        
        /* ============================================================
           FORM
           ============================================================ */
        .input-group {
            position: relative;
            margin-bottom: 18px;
        }
        
        .input-group .input-icon {
            position: absolute;
            right: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255, 255, 255, 0.2);
            font-size: 18px;
            transition: color 0.3s;
            pointer-events: none;
        }
        
        .input-group input {
            width: 100%;
            padding: 14px 48px 14px 16px;
            background: rgba(255, 255, 255, 0.06);
            border: 2px solid rgba(255, 255, 255, 0.06);
            border-radius: 14px;
            color: #fff;
            font-size: 15px;
            font-family: 'Vazirmatn', sans-serif;
            transition: all 0.3s ease;
            outline: none;
        }
        
        .input-group input::placeholder {
            color: rgba(255, 255, 255, 0.25);
        }
        
        .input-group input:focus {
            border-color: rgba(102, 126, 234, 0.6);
            background: rgba(255, 255, 255, 0.08);
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.08);
        }
        
        .input-group input:focus + .input-icon,
        .input-group input:focus ~ .input-icon {
            color: rgba(102, 126, 234, 0.6);
        }
        
        /* ============================================================
           BUTTON
           ============================================================ */
        .btn-login {
            width: 100%;
            padding: 15px;
            margin-top: 8px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            border: none;
            border-radius: 14px;
            color: #fff;
            font-size: 16px;
            font-weight: 700;
            font-family: 'Vazirmatn', sans-serif;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        
        .btn-login:hover {
            transform: translateY(-3px);
            box-shadow: 0 12px 40px rgba(102, 126, 234, 0.35);
        }
        
        .btn-login:active {
            transform: translateY(0);
        }
        
        /* ============================================================
           ERROR
           ============================================================ */
        .error-message {
            display: <?= $error ? 'flex' : 'none' ?>;
            align-items: center;
            gap: 10px;
            padding: 12px 16px;
            margin-bottom: 20px;
            background: rgba(239, 68, 68, 0.1);
            border: 1px solid rgba(239, 68, 68, 0.2);
            border-radius: 12px;
            color: #f87171;
            font-size: 14px;
        }
        
        .error-message i {
            font-size: 18px;
        }
        
        /* ============================================================
           FOOTER
           ============================================================ */
        .login-footer {
            text-align: center;
            margin-top: 28px;
            padding-top: 20px;
            border-top: 1px solid rgba(255, 255, 255, 0.04);
        }
        
        .login-footer .footer-text {
            color: rgba(255, 255, 255, 0.15);
            font-size: 12px;
        }
        
        .login-footer .footer-text i {
            margin-left: 6px;
        }
        
        .login-footer .footer-text .highlight {
            color: rgba(102, 126, 234, 0.6);
        }
        
        .login-footer .developer {
            color: rgba(255, 255, 255, 0.1);
            font-size: 11px;
            margin-top: 4px;
            display: <?= !empty($developerName) ? 'block' : 'none' ?>;
        }
        
        /* ============================================================
           RESPONSIVE
           ============================================================ */
        @media (max-width: 480px) {
            .login-container {
                padding: 32px 24px 28px;
                border-radius: 24px;
            }
            
            .logo-icon {
                width: 64px;
                height: 64px;
            }
            
            .logo-wrapper h1 {
                font-size: 19px;
            }
            
            .btn-login {
                font-size: 15px;
                padding: 13px;
            }
        }
    </style>
</head>
<body>

<!-- ============================================================
     BACKGROUND ANIMATION
     ============================================================ -->
<div class="bg-animation">
    <div class="orb"></div>
    <div class="orb"></div>
    <div class="orb"></div>
</div>

<!-- ============================================================
     LOGIN CONTAINER
     ============================================================ -->
<div class="login-container">
    
    <!-- ===== LOGO ===== -->
    <div class="logo-wrapper">
        <div class="logo-icon">
            <?php if (!empty($logoPath) && file_exists($logoPath)): ?>
                <img src="<?= htmlspecialchars($logoPath) ?>" alt="<?= htmlspecialchars($siteName) ?>">
            <?php else: ?>
                <div class="default-logo">
                    <i class="fas fa-graduation-cap"></i>
                </div>
            <?php endif; ?>
        </div>
        <h1><?= htmlspecialchars($siteName) ?></h1>
        <div class="subtitle"><?= htmlspecialchars($institutionName) ?></div>
    </div>
    
    <!-- ===== ERROR ===== -->
    <?php if ($error): ?>
    <div class="error-message">
        <i class="fas fa-exclamation-circle"></i>
        <?= $error ?>
    </div>
    <?php endif; ?>
    
    <!-- ===== FORM ===== -->
    <form method="POST" action="">
        <div class="input-group">
            <input type="text" name="username" placeholder="کد ملی" required>
            <span class="input-icon"><i class="fas fa-user"></i></span>
        </div>
        
        <div class="input-group">
            <input type="password" name="password" placeholder="رمز عبور" required>
            <span class="input-icon"><i class="fas fa-lock"></i></span>
        </div>
        
        <button type="submit" class="btn-login">
            <i class="fas fa-arrow-left"></i>
            ورود به سامانه
        </button>
    </form>
    
    <!-- ===== FOOTER ===== -->
    <div class="login-footer">
        <div class="footer-text">
            <i class="fas fa-shield-alt"></i>
            تمامی حقوق محفوظ است <span class="highlight">© <?= date('Y') ?></span>
        </div>
        <?php if (!empty($footerText)): ?>
            <div class="footer-text" style="margin-top:2px;font-size:11px;color:rgba(255,255,255,0.08);">
                <?= htmlspecialchars($footerText) ?>
            </div>
        <?php endif; ?>
        <?php if (!empty($developerName)): ?>
            <div class="developer">
                توسعه: <?= htmlspecialchars($developerName) ?>
            </div>
        <?php endif; ?>
    </div>
    
</div>

</body>
</html>