<?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'] ?? '';

$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>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700;900&display=swap');
        
        * {
            font-family: 'Vazirmatn', sans-serif;
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .login-container {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(25px);
            border: 1px solid rgba(255, 255, 255, 0.08);
            border-radius: 40px;
            padding: 50px 40px;
            width: 100%;
            max-width: 420px;
            box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
            animation: slideUp 0.7s ease-out;
        }
        
        @keyframes slideUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .logo-wrapper {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .logo-icon {
            width: 80px;
            height: 80px;
            border-radius: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 16px;
            overflow: hidden;
            box-shadow: 0 15px 40px rgba(102, 126, 234, 0.35);
            animation: pulse 2.5s infinite;
        }
        
        .logo-icon img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
        
        .logo-icon .default-logo {
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 36px;
            color: white;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.04); }
        }
        
        .logo-wrapper h1 {
            color: white;
            font-weight: 700;
            font-size: 24px;
            margin: 0;
        }
        
        .logo-wrapper p {
            color: rgba(255,255,255,0.4);
            font-size: 13px;
            margin-top: 4px;
        }
        
        .input-group {
            position: relative;
            margin-bottom: 20px;
        }
        
        .input-group input {
            width: 100%;
            padding: 15px 20px 15px 50px;
            background: rgba(255, 255, 255, 0.06);
            border: 2px solid rgba(255, 255, 255, 0.08);
            border-radius: 16px;
            color: white;
            font-size: 15px;
            transition: all 0.3s ease;
            outline: none;
        }
        
        .input-group input:focus {
            border-color: #667eea;
            background: rgba(255, 255, 255, 0.1);
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.12);
        }
        
        .input-group input::placeholder {
            color: rgba(255, 255, 255, 0.3);
        }
        
        .input-group .icon {
            position: absolute;
            right: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255, 255, 255, 0.25);
            font-size: 18px;
        }
        
        .btn-login {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 16px;
            color: white;
            font-size: 17px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 8px;
        }
        
        .btn-login:hover {
            transform: translateY(-3px);
            box-shadow: 0 15px 40px rgba(102, 126, 234, 0.4);
        }
        
        .btn-login:active {
            transform: translateY(0);
        }
        
        .error-message {
            background: rgba(239, 68, 68, 0.12);
            border: 1px solid rgba(239, 68, 68, 0.25);
            border-radius: 14px;
            padding: 12px 16px;
            color: #f87171;
            font-size: 14px;
            margin-bottom: 20px;
            display: <?= $error ? 'flex' : 'none' ?>;
            align-items: center;
            gap: 10px;
        }
        
        .error-message i {
            font-size: 18px;
        }
        
        .login-footer {
            text-align: center;
            color: rgba(255, 255, 255, 0.2);
            font-size: 12px;
            margin-top: 28px;
        }
        
        .login-footer span {
            color: #667eea;
        }
        
        .bg-shapes {
            position: fixed;
            inset: 0;
            pointer-events: none;
            z-index: -1;
            overflow: hidden;
        }
        
        .bg-shapes .shape {
            position: absolute;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.02);
            animation: float 20s infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0) rotate(0deg) scale(1); }
            50% { transform: translateY(-80px) rotate(180deg) scale(1.1); }
        }
        
        .bg-shapes .shape:nth-child(1) { width: 300px; height: 300px; top: -80px; right: -80px; animation-delay: 0s; }
        .bg-shapes .shape:nth-child(2) { width: 200px; height: 200px; bottom: -60px; left: -60px; animation-delay: -5s; }
        .bg-shapes .shape:nth-child(3) { width: 150px; height: 150px; top: 50%; left: 50%; animation-delay: -10s; }
        
        @media (max-width: 480px) {
            .login-container {
                padding: 35px 25px;
            }
            .logo-icon {
                width: 64px;
                height: 64px;
                font-size: 28px;
            }
            .logo-wrapper h1 {
                font-size: 20px;
            }
        }
    </style>
</head>
<body>

<div class="bg-shapes">
    <div class="shape"></div>
    <div class="shape"></div>
    <div class="shape"></div>
</div>

<div class="login-container">
    <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>
        <p><?= htmlspecialchars($institutionName) ?></p>
    </div>

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

    <form method="POST" action="">
        <div class="input-group">
            <i class="fas fa-user icon"></i>
            <input type="text" name="username" placeholder="کد ملی" required>
        </div>
        
        <div class="input-group">
            <i class="fas fa-lock icon"></i>
            <input type="password" name="password" placeholder="رمز عبور" required>
        </div>

        <button type="submit" class="btn-login">
            <i class="fas fa-arrow-left ml-2"></i>
            ورود به سامانه
        </button>
    </form>

    <div class="login-footer">
        <i class="fas fa-shield-alt ml-1"></i>
        تمامی حقوق محفوظ است <span>© <?= date('Y') ?></span>
        <?php if (!empty($developerName)): ?>
            <span style="display:block;margin-top:4px;color:rgba(255,255,255,0.15);font-size:11px;">
                توسعه: <?= htmlspecialchars($developerName) ?>
            </span>
        <?php endif; ?>
    </div>
</div>

</body>
</html>