<?php
require_once __DIR__ . '/includes/header.php';

if (isLoggedIn()) {
    redirect('user_dashboard.php');
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username && $password) {
        $stmt = db()->prepare("SELECT * FROM users WHERE username = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['is_super_admin'] = (int)$user['is_super_admin'];
            
            // Redirect to dashboard
            redirect('user_dashboard.php');
        } else {
            $error = "اسم المستخدم أو كلمة المرور غير صحيحة";
        }
    } else {
        $error = "يرجى إدخال اسم المستخدم وكلمة المرور";
    }
}
?>

<div class="row justify-content-center align-items-center" style="min-height: 80vh;">
    <div class="col-md-4 col-lg-3">
        <div class="card shadow-sm border-0">
            <div class="card-body p-4">
                <div class="text-center mb-4">
                    <a href="index.php" class="text-decoration-none text-dark d-block">
                    <?php if (!empty($sys_settings['site_logo'])): ?>
                        <img src="<?php echo htmlspecialchars($sys_settings['site_logo']); ?>" alt="Logo" class="img-fluid mb-3" style="max-height: 80px;">
                    <?php endif; ?>
                    <h4 class="fw-bold mb-0"><?php echo htmlspecialchars($sys_settings['site_name']); ?></h4>
                    <p class="text-danger mb-0 fw-bold">نظام إدارة الفرق التطوعية ولجان الزكاة</p>
                    </a>
                    <p class="text-muted small">يرجى إدخال بيانات الاعتماد الخاصة بك</p>
                </div>

                <?php if ($error): ?>
                    <div class="alert alert-danger py-2 small" role="alert">
                        <?php echo $error; ?>
                    </div>
                <?php endif; ?>

                <form method="POST" action="">
                    <div class="mb-3">
                        <label for="username" class="form-label small fw-semibold">اسم المستخدم</label>
                        <input type="text" class="form-control" id="username" name="username" required tabindex="1" autofocus>
                    </div>
                    
                    <div class="mb-2">
                        <div class="d-flex justify-content-between align-items-center mb-1">
                            <label for="password" class="form-label small fw-semibold mb-0">كلمة المرور</label>
                            <a href="forgot_password.php" class="text-decoration-none small text-primary" tabindex="4">نسيت كلمة المرور؟</a>
                        </div>
                        <input type="password" class="form-control" id="password" name="password" required tabindex="2">
                    </div>

                    <div class="mt-4">
                        <button type="submit" class="btn btn-primary w-100 fw-bold py-2" tabindex="3">تسجيل الدخول</button>
                    </div>
                    
                    <?php if ($sys_settings['allow_registration']): ?>
                    <div class="mt-3 text-center">
                        <p class="small text-muted">ليس لديك حساب؟ <a href="register.php" class="text-decoration-none text-primary">إنشاء حساب جديد</a></p>
                    </div>
                    <?php endif; ?>
                </form>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>