<?php
use App\Core\{Csrf, Lang, Session};
use function App\Core\{__, e, url};

$errors = Session::flash('auth_errors') ?: [];
$old = Session::flash('old_input') ?: [];
$selectedLang = $old['language'] ?? Lang::current();
?>
<section class="auth-card auth-card-wide">
    <h1><?= e(__('register')) ?></h1>
    <p class="auth-copy"><?= e(__('register_intro')) ?></p>

    <?php if ($errors): ?>
        <div class="auth-errors" role="alert">
            <?php foreach ($errors as $error): ?><p><?= e($error) ?></p><?php endforeach; ?>
        </div>
    <?php endif; ?>

    <form method="post" action="<?= url('/kayit') ?>" novalidate>
        <?= Csrf::field() ?>
        <div class="form-grid">
            <label>
                <?= e(__('nickname')) ?>
                <input name="nickname" autocomplete="name" required value="<?= e($old['nickname'] ?? '') ?>">
            </label>
            <label>
                <?= e(__('username')) ?>
                <input name="username" autocomplete="username" required value="<?= e($old['username'] ?? '') ?>">
            </label>
        </div>
        <label>
            <?= e(__('email')) ?>
            <input name="email" type="email" autocomplete="email" required value="<?= e($old['email'] ?? '') ?>">
        </label>
        <div class="form-grid">
            <label>
                <?= e(__('password')) ?>
                <input name="password" type="password" autocomplete="new-password" required>
            </label>
            <label>
                <?= e(__('password_confirmation')) ?>
                <input name="password_confirmation" type="password" autocomplete="new-password" required>
            </label>
        </div>
        <label>
            <?= e(__('language_preference')) ?>
            <select name="language">
                <?php foreach (($config['supported_languages'] ?? ['tr', 'en', 'de', 'fr']) as $lang): ?>
                    <option value="<?= e($lang) ?>" <?= $selectedLang === $lang ? 'selected' : '' ?>><?= e(strtoupper($lang)) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="check-row">
            <input name="privacy_accepted" type="checkbox" value="1" required>
            <span><?= e(__('privacy_accept_prefix')) ?> <a href="<?= url('/sayfa/gizlilik-politikasi') ?>"><?= e(__('privacy_policy')) ?></a></span>
        </label>
        <button class="pill primary" type="submit"><?= e(__('register')) ?></button>
    </form>

    <div class="auth-links">
        <a href="<?= url('/giris-yap') ?>"><?= e(__('already_registered')) ?></a>
    </div>
</section>
