<?php

declare(strict_types=1);

/**
 * @var string $next
 * @var string $reason
 * @var string $msgOk
 * @var string $err
 * @var string $stickyLoginUsername
 */

$loginManualApproval = function_exists('fate_member_manual_approval_enabled') && fate_member_manual_approval_enabled();
?>
<main class="fate-auth-page" id="main">
    <h1 class="fate-auth-page__title">會員登入</h1>
    <p class="fate-auth-page__sub">使用您註冊的帳號與密碼登入。<?= $loginManualApproval ? '新帳號須待管理員審核通過後方可登入。' : '新帳號註冊後即可登入，並依提示完成 Email 驗證。' ?></p>

    <?php if ($reason === 'badconfig'): ?>
        <div class="fate-auth-msg fate-auth-msg--warn">會員功能尚未設定完成，請稍後再試。</div>
    <?php endif; ?>
    <?php if ($msgOk !== ''): ?>
        <div class="fate-auth-msg fate-auth-msg--ok"><?= htmlspecialchars($msgOk, ENT_QUOTES, 'UTF-8') ?></div>
    <?php endif; ?>
    <?php if ($err === 'db'): ?>
        <div class="fate-auth-msg fate-auth-msg--bad">無法連線資料庫。</div>
    <?php elseif ($err === 'missing_table'): ?>
        <div class="fate-auth-msg fate-auth-msg--bad">找不到會員資料表。</div>
    <?php elseif ($err === 'bad_column'): ?>
        <div class="fate-auth-msg fate-auth-msg--bad">會員資料設定異常，請聯絡站方。</div>
    <?php elseif ($err === 'pending_approval'): ?>
        <div class="fate-auth-msg fate-auth-msg--warn">此帳號尚待管理員審核，通過後即可登入。</div>
    <?php elseif ($err === 'inactive'): ?>
        <div class="fate-auth-msg fate-auth-msg--bad">此帳號已停權，無法登入。</div>
    <?php elseif ($err === 'bad'): ?>
        <div class="fate-auth-msg fate-auth-msg--bad">帳號或密碼錯誤。</div>
    <?php endif; ?>

    <?php if (!fate_admin_member_auth_ready()): ?>
        <div class="fate-auth-msg fate-auth-msg--warn">會員功能尚未設定完成。</div>
    <?php else: ?>
        <div class="fate-auth-card">
            <form method="post" action="login.php" autocomplete="username">
                <input type="hidden" name="next" value="<?= htmlspecialchars($next, ENT_QUOTES, 'UTF-8') ?>">
                <label for="username">帳號</label>
                <input type="text" name="username" id="username" required autofocus maxlength="80" autocomplete="username"
                       value="<?= htmlspecialchars($stickyLoginUsername, ENT_QUOTES, 'UTF-8') ?>">
                <label for="password">密碼</label>
                <input type="password" name="password" id="password" required autocomplete="current-password">
                <label class="fate-auth-remember">
                    <input type="checkbox" name="remember" value="1" checked>
                    保持登入（約 30 天，建議勾選）
                </label>
                <button type="submit">登入</button>
            </form>
        </div>
    <?php endif; ?>
    <p class="fate-auth-back">
        <a href="register.php?next=<?= htmlspecialchars(rawurlencode($next), ENT_QUOTES, 'UTF-8') ?>">尚無帳號？註冊</a>
        · <a href="login.php?next=<?= rawurlencode('./member_profile.php') ?>">已註冊待驗證？登入後至我的帳號</a>
        · <a href="./">返回首頁</a>
    </p>
</main>
