<?php
if (!defined('INDEX_AUTH')) {
    die("Direct access not allowed");
}

// 1. Ambil slug tanpa fallback 'pusat'
$lib_slug = $_GET['lib'] ?? $_SESSION['multi_opac']['last_library_slug'] ?? null;

// 2. Ambil error login dari session jika ada
$login_error = $_SESSION['login_error'] ?? null;
unset($_SESSION['login_error']);

// 3. Susun URL Action Form secara akurat
$form_action = buildLibraryUrl('member-login', $lib_slug);
?>

<div class="row justify-content-center my-4">
    <div class="col-md-5 col-lg-4">
        <div class="card shadow-sm border-0 rounded-4">
            <div class="card-body p-4">
                <div class="text-center mb-4">
                    <i class="fa-solid fa-user-circle fa-3x text-primary mb-2"></i>
                    <h4 class="fw-bold mb-1">Masuk Anggota</h4>
                    <p class="text-muted small">Silakan masuk ke akun perpustakaan Anda</p>
                </div>

                <?php if ($login_error): ?>
                    <div class="alert alert-danger rounded-3 py-2 px-3 small mb-3">
                        <i class="fa-solid fa-circle-exclamation me-1"></i> <?= htmlspecialchars($login_error) ?>
                    </div>
                <?php endif; ?>

                <form action="<?= $form_action ?>" method="post">
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">ID Anggota / Username</label>
                        <input type="text" name="memberID" class="form-control rounded-3" required placeholder="Masukkan ID Anggota" autofocus>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Kata Sandi</label>
                        <input type="password" name="password" class="form-control rounded-3" required placeholder="Masukkan kata sandi">
                    </div>

                    <button type="submit" name="logMe" value="1" class="btn btn-primary w-100 rounded-3 py-2 fw-bold">
                        <i class="fa-solid fa-right-to-bracket me-1"></i> Masuk
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>