<?php

declare(strict_types=1);

/**
 * @var string $next
 * @var string $msgBad
 * @var string $stickyUsername
 * @var string $stickyDisplay
 * @var string $stickyEmail
 * @var string $csrf
 * @var bool $disabled
 * @var bool $needVerifyEmailUi
 * @var bool $needEmailUi
 * @var bool $reverseOnlyUi
 * @var bool $tokenOnlyUi
 * @var bool $bothVerifyUi
 * @var bool $needManualApprovalUi
 */
?>
<main class="fate-auth-page" id="main">
    <h1 class="fate-auth-page__title">註冊會員</h1>
    <p class="fate-auth-page__sub">建立帳號後可記錄偏好與頭像。帳號至少 3 字、密碼至少 8 字。
        <?php if ($needManualApprovalUi && $needEmailUi): ?>
            <strong>註冊完成後尚待管理員審核，通過後即可登入。</strong>
        <?php elseif ($bothVerifyUi): ?>
            <strong>註冊後會寄驗證信，亦可從您的信箱回寄到驗證信箱完成驗證（擇一）。</strong>
        <?php elseif ($reverseOnlyUi): ?>
            <strong>註冊後請依指示從您的信箱寄一封信到驗證信箱，系統收信比對後即完成驗證。</strong>
        <?php elseif ($tokenOnlyUi): ?>
            <strong>註冊後會寄驗證信到 Email，請點信內連結或輸入驗證碼完成驗證。</strong>
        <?php elseif ($needVerifyEmailUi): ?>
            <strong>註冊後請依站方設定完成 Email 驗證。</strong>
        <?php endif; ?>
    </p>

    <?php if ($disabled): ?>
        <div class="fate-auth-msg fate-auth-msg--bad">站方已暫停開放線上註冊。</div>
    <?php elseif (!fate_admin_member_auth_ready()): ?>
        <div class="fate-auth-msg fate-auth-msg--bad">會員功能尚未設定完成，請稍後再試。</div>
    <?php elseif (!$needEmailUi): ?>
        <div class="fate-auth-msg fate-auth-msg--bad">會員 Email 欄位尚未就緒。站長請匯入 <code>fate/sql/install.sql</code> 建立完整資料表。</div>
    <?php elseif ($msgBad !== ''): ?>
        <div class="fate-auth-msg fate-auth-msg--bad"><?= htmlspecialchars($msgBad, ENT_QUOTES, 'UTF-8') ?></div>
    <?php endif; ?>

    <?php if (!$disabled && fate_admin_member_auth_ready()): ?>
        <div class="fate-auth-card">
            <form method="post" action="register.php" id="register-form" autocomplete="off" novalidate>
                <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>">
                <input type="hidden" name="next" value="<?= htmlspecialchars($next, ENT_QUOTES, 'UTF-8') ?>">
                <label for="username">帳號</label>
                <input type="text" name="username" id="username" maxlength="80"
                       title="3～80 字，可含中文、英文、數字與 ._-，不可空格" autocomplete="username" required inputmode="text"
                       value="<?= htmlspecialchars($stickyUsername, ENT_QUOTES, 'UTF-8') ?>">
                <p class="field-check" id="username-check" hidden role="status" aria-live="polite"></p>
                <label for="password">密碼</label>
                <input type="password" name="password" id="password" autocomplete="new-password" minlength="8" maxlength="72" required>
                <label for="password_confirm">確認密碼</label>
                <input type="password" name="password_confirm" id="password_confirm" autocomplete="new-password" minlength="8" maxlength="72" required>
                <label for="display_name">暱稱（選填，最多 <?= (int) fate_member_display_name_max_chars() ?> 字；未填將以帳號顯示）</label>
                <input type="text" name="display_name" id="display_name" maxlength="<?= (int) fate_member_display_name_max_chars() ?>" autocomplete="nickname"
                       value="<?= htmlspecialchars($stickyDisplay, ENT_QUOTES, 'UTF-8') ?>">
                <p class="field-check" id="display_name-check" hidden role="status" aria-live="polite"></p>
                <?php if ($needEmailUi): ?>
                    <label for="email"><?php
                        if ($needManualApprovalUi) {
                            echo 'Email（必填）';
                        } elseif ($reverseOnlyUi) {
                            echo 'Email（必填，作為驗證寄件信箱）';
                        } elseif ($tokenOnlyUi) {
                            echo 'Email（必填，用於接收驗證信）';
                        } else {
                            echo 'Email（必填）';
                        }
                        ?></label>
                    <input type="email" name="email" id="email" maxlength="254" autocomplete="email" required inputmode="email"
                           value="<?= htmlspecialchars($stickyEmail, ENT_QUOTES, 'UTF-8') ?>">
                    <p class="field-check" id="email-check" hidden role="status" aria-live="polite"></p>
                <?php endif; ?>
                <button type="submit" id="register-submit" <?= $disabled ? 'disabled' : '' ?>><?= ($tokenOnlyUi && !$needManualApprovalUi) ? '建立帳號並寄出驗證信' : '建立帳號' ?></button>
            </form>
        </div>
    <?php endif; ?>

    <p class="fate-auth-back">
        <?php if (!$disabled): ?><a href="login.php?next=<?= rawurlencode($next) ?>">已有帳號？登入</a> · <?php endif; ?>
        <a href="./">返回首頁</a>
    </p>
</main>
