<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once '../config/database.php';
require_once '../includes/functions.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (empty($username) || empty($password)) {
        $error = "لطفاً نام کاربری و رمز عبور را وارد کنید";
    } else {
        try {
            $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
            $stmt->execute([$username]);
            $user = $stmt->fetch(PDO::FETCH_ASSOC);
            
            if ($user && password_verify($password, $user['password'])) {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_level'] = $user['level'];
                $_SESSION['user_name'] = $user['fullname'];
                
                logSystemActivity($user['id'], 'login', "کاربر {$user['fullname']} وارد سیستم شد");
                
                switch ($user['level']) {
                    case 'admin': header('Location: ../admin/dashboard.php'); break;
                    case 'expert': header('Location: ../expert/dashboard.php'); break;
                    case 'representative': header('Location: ../representative/dashboard.php'); break;
                    case 'reporter': header('Location: ../admin/reports.php'); break;
                    default: header('Location: ../index.php'); break;
                }
                exit;
            } else {
                $error = "❌ نام کاربری یا رمز عبور اشتباه است";
            }
        } catch(PDOException $e) {
            $error = "❌ خطا در اتصال به دیتابیس: " . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ورود | سامانه جامع مهدی</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/portal/assets/css/style.css">
    <style>
        html, body {
            height: 100%;
            margin: 0;
        }
        body {
            display: flex;
            flex-direction: column;
            font-family: 'Vazirmatn', 'Tahoma', sans-serif;
            background: #f8fafc;
        }
        .login-wrapper {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 50%, #6d28d9 100%);
            padding: 40px 20px;
        }
        .login-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 24px;
            padding: 45px 40px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
            max-width: 420px;
            width: 100%;
            text-align: center;
        }
        .login-card .logo-icon {
            font-size: 3.5rem;
            color: #4f46e5;
            margin-bottom: 10px;
        }
        .login-card h4 {
            font-size: 1.6rem;
            font-weight: 700;
            color: #0f172a;
            margin-bottom: 5px;
        }
        .login-card .subtitle {
            font-size: 0.9rem;
            color: #94a3b8;
            margin-bottom: 25px;
        }
        .login-card .form-control {
            padding-right: 42px;
            height: 48px;
            border-radius: 10px;
            font-size: 0.95rem;
        }
        .login-card .input-wrapper {
            position: relative;
        }
        .login-card .input-wrapper .input-icon {
            position: absolute;
            right: 14px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            font-size: 1.1rem;
        }
        .login-card .btn-login {
            background: #4f46e5;
            color: white;
            border: none;
            padding: 12px;
            border-radius: 10px;
            font-size: 1.05rem;
            font-weight: 600;
            width: 100%;
            transition: all 0.3s ease;
            margin-top: 8px;
        }
        .login-card .btn-login:hover {
            background: #4338ca;
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(79, 70, 229, 0.3);
        }
        .login-card .footer-text {
            margin-top: 18px;
            font-size: 0.8rem;
            color: #94a3b8;
        }
        .login-card .footer-text strong {
            color: #4f46e5;
        }
        .login-card .back-home {
            display: inline-block;
            margin-top: 12px;
            color: #64748b;
            text-decoration: none;
            font-size: 0.85rem;
            transition: color 0.2s ease;
        }
        .login-card .back-home:hover {
            color: #4f46e5;
        }
        .footer-custom {
            background: rgba(255,255,255,0.1);
            backdrop-filter: blur(5px);
            padding: 10px 0;
            color: rgba(255,255,255,0.6);
            font-size: 0.75rem;
            text-align: center;
            border-top: 1px solid rgba(255,255,255,0.08);
            flex-shrink: 0;
        }
        .footer-custom p {
            margin: 0;
        }
        @media (max-width: 480px) {
            .login-card {
                padding: 30px 20px;
            }
            .login-card h4 {
                font-size: 1.3rem;
            }
        }
    </style>
</head>
<body>

    <div class="login-wrapper">
        <div class="login-card">
            <div class="logo-icon">
                <i class="bi bi-mortarboard-fill"></i>
            </div>
            <h4>🔐 ورود به سامانه</h4>
            <div class="subtitle">سامانه جامع مهدی</div>

            <?php if(!empty($error)): ?>
                <div class="alert alert-danger text-start" style="font-size: 0.85rem; padding: 10px 14px; border-radius: 10px;">
                    <i class="bi bi-exclamation-circle me-1"></i> <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>

            <form method="POST" action="">
                <div class="mb-3 input-wrapper">
                    <i class="bi bi-person input-icon"></i>
                    <input type="text" name="username" class="form-control" placeholder="نام کاربری" required>
                </div>
                <div class="mb-3 input-wrapper">
                    <i class="bi bi-lock input-icon"></i>
                    <input type="password" name="password" class="form-control" placeholder="رمز عبور" required>
                </div>
                <button type="submit" class="btn-login">
                    <i class="bi bi-box-arrow-in-right"></i> ورود
                </button>
            </form>

            <div class="footer-text">
                کاربر پیش‌فرض: <strong>admin</strong> / <strong>password</strong>
            </div>
            <a href="/portal/index.php" class="back-home">
                <i class="bi bi-house"></i> بازگشت به صفحه اصلی
            </a>
        </div>
    </div>

    <footer class="footer-custom">
        <div class="container">
            <p>© <?php echo date('Y') - 621; ?> - سامانه جامع مهدی | تمامی حقوق محفوظ است</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>