<?php ob_start(); ?>

<?php admin_content_header($redirect ? __('admin.redirects.edit') : __('admin.redirects.create'), 'arrow-right-left'); ?>

<form method="POST" action="<?= $redirect ? admin_url('redirects/update/' . (int) $redirect['id']) : admin_url('redirects/store') ?>">
    <?= csrf_field() ?>

    <div class="form-card">
        <div class="form-group <?= !empty($errors['source_path']) ? 'has-error' : '' ?>">
            <label for="source_path"><?= __('admin.redirects.source_path') ?> <span class="required">*</span></label>
            <input type="text" id="source_path" name="source_path"
                   value="<?= e($old['source_path'] ?? '') ?>"
                   placeholder="/alte-seite" required>
            <small class="form-hint"><?= __('admin.redirects.source_path_hint') ?></small>
            <?php if (!empty($errors['source_path'])): ?>
                <small class="form-error"><?= e($errors['source_path']) ?></small>
            <?php endif; ?>
        </div>

        <div class="form-group <?= !empty($errors['target_url']) ? 'has-error' : '' ?>">
            <label for="target_url"><?= __('admin.redirects.target_url') ?> <span class="required">*</span></label>
            <input type="text" id="target_url" name="target_url"
                   value="<?= e($old['target_url'] ?? '') ?>"
                   placeholder="/neue-seite" required>
            <small class="form-hint"><?= __('admin.redirects.target_url_hint') ?></small>
            <?php if (!empty($errors['target_url'])): ?>
                <small class="form-error"><?= e($errors['target_url']) ?></small>
            <?php endif; ?>
        </div>

        <div class="form-row">
            <div class="form-group form-group-half">
                <label for="status_code"><?= __('admin.redirects.status_code') ?></label>
                <select id="status_code" name="status_code">
                    <option value="301" <?= ($old['status_code'] ?? 301) == 301 ? 'selected' : '' ?>><?= __('admin.redirects.status_301') ?></option>
                    <option value="302" <?= ($old['status_code'] ?? 301) == 302 ? 'selected' : '' ?>><?= __('admin.redirects.status_302') ?></option>
                </select>
            </div>
            <div class="form-group form-group-half">
                <label class="toggle-label" style="margin-top: 2rem;">
                    <input type="hidden" name="is_active" value="0">
                    <input type="checkbox" name="is_active" value="1"
                           <?= ($old['is_active'] ?? 1) ? 'checked' : '' ?>>
                    <?= __('admin.redirects.is_active') ?>
                </label>
            </div>
        </div>
    </div>

    <div class="form-actions">
        <button type="submit" class="btn btn-primary"><?= __('admin.common.save') ?></button>
        <a href="<?= admin_url('redirects') ?>" class="btn"><?= __('admin.common.cancel') ?></a>
    </div>
</form>

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