<?php ob_start(); ?>

<?php admin_content_header(__('admin.totp.title'), 'settings'); ?>

<?php if ($flash = flash('success')): ?>
<div class="alert alert-success"><?= e($flash) ?></div>
<?php endif; ?>
<?php if ($flash = flash('error')): ?>
<div class="alert alert-error"><?= e($flash) ?></div>
<?php endif; ?>

<div class="form-grid">
    <div class="form-main">

        <?php if (!empty($totp_enabled)): ?>
        <!-- 2FA is already enabled -->
        <div class="form-card">
            <h4><?= __('admin.totp.status') ?></h4>
            <p style="color:var(--admin-success,#22c55e);font-weight:600;">
                <?= icon('check-circle') ?> <?= __('admin.totp.enabled_status') ?>
            </p>
            <p style="color:var(--gray-600);font-size:0.9rem;margin-top:0.5rem;">
                <?= __('admin.totp.enabled_info') ?>
            </p>

            <form method="POST" action="/admin/2fa/disable" style="margin-top:1.5rem;">
                <?= $csrf->field() ?>
                <div class="form-group">
                    <label for="disable_code"><?= __('admin.totp.disable_code_label') ?></label>
                    <input type="text" id="disable_code" name="totp_code"
                           autocomplete="one-time-code" inputmode="numeric"
                           maxlength="6" placeholder="000000"
                           style="max-width:200px;">
                </div>
                <button type="submit" class="btn btn-danger"
                        onclick="return confirm('<?= __('admin.totp.disable_confirm') ?>')">
                    <?= __('admin.totp.disable_btn') ?>
                </button>
            </form>
        </div>

        <?php if (!empty($recovery_count)): ?>
        <div class="form-card">
            <h4><?= __('admin.totp.recovery_codes') ?></h4>
            <p style="color:var(--gray-600);font-size:0.9rem;">
                <?= __('admin.totp.recovery_remaining', ['count' => $recovery_count]) ?>
            </p>
            <form method="POST" action="/admin/2fa/regenerate-codes" style="margin-top:1rem;">
                <?= $csrf->field() ?>
                <div class="form-group">
                    <label for="regen_code"><?= __('admin.totp.regen_code_label') ?></label>
                    <input type="text" id="regen_code" name="totp_code"
                           autocomplete="one-time-code" inputmode="numeric"
                           maxlength="6" placeholder="000000"
                           style="max-width:200px;">
                </div>
                <button type="submit" class="btn btn-secondary">
                    <?= __('admin.totp.regenerate_btn') ?>
                </button>
            </form>
        </div>
        <?php endif; ?>

        <?php elseif (!empty($show_recovery_codes)): ?>
        <!-- Show newly generated recovery codes -->
        <div class="form-card">
            <h4 style="color:var(--admin-success);"><?= icon('check-circle') ?> <?= __('admin.totp.setup_complete') ?></h4>
            <p style="color:var(--gray-600);font-size:0.9rem;margin-bottom:1rem;">
                <?= __('admin.totp.recovery_save_warning') ?>
            </p>
            <div class="totp-recovery-codes" style="background:var(--gray-50,#f9fafb);padding:1rem;border-radius:8px;font-family:monospace;font-size:1rem;line-height:2;columns:2;">
                <?php foreach ($recovery_codes as $code): ?>
                <div><?= e($code) ?></div>
                <?php endforeach; ?>
            </div>
            <p style="margin-top:1rem;">
                <a href="/admin/2fa" class="btn btn-primary"><?= __('admin.totp.done_btn') ?></a>
            </p>
        </div>

        <?php else: ?>
        <!-- Setup form: show QR code and enable -->
        <div class="form-card">
            <h4><?= __('admin.totp.setup_heading') ?></h4>
            <p style="color:var(--gray-600);font-size:0.9rem;margin-bottom:1rem;">
                <?= __('admin.totp.setup_instructions') ?>
            </p>

            <div style="text-align:center;margin:1.5rem 0;">
                <?= $qr_svg ?>
            </div>

            <div class="form-group">
                <label><?= __('admin.totp.secret_label') ?></label>
                <div style="background:var(--gray-50,#f9fafb);padding:0.75rem 1rem;border-radius:6px;font-family:monospace;font-size:1rem;letter-spacing:0.15em;word-break:break-all;user-select:all;">
                    <?= e($totp_secret) ?>
                </div>
                <small class="form-hint"><?= __('admin.totp.secret_hint') ?></small>
            </div>

            <form method="POST" action="/admin/2fa/enable">
                <?= $csrf->field() ?>
                <input type="hidden" name="secret" value="<?= e($totp_secret) ?>">
                <div class="form-group">
                    <label for="verify_code"><?= __('admin.totp.verify_code_label') ?></label>
                    <input type="text" id="verify_code" name="totp_code"
                           required autocomplete="one-time-code" inputmode="numeric"
                           maxlength="6" placeholder="000000"
                           style="max-width:200px;font-size:1.2rem;letter-spacing:0.2em;">
                    <small class="form-hint"><?= __('admin.totp.verify_code_hint') ?></small>
                </div>
                <button type="submit" class="btn btn-primary">
                    <?= __('admin.totp.enable_btn') ?>
                </button>
            </form>
        </div>
        <?php endif; ?>

    </div>

    <div class="form-sidebar">
        <div class="form-card">
            <h4><?= __('admin.totp.what_is_2fa') ?></h4>
            <p style="color:var(--gray-600);font-size:0.85rem;line-height:1.6;">
                <?= __('admin.totp.what_is_2fa_text') ?>
            </p>
        </div>
    </div>
</div>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
