<?php
declare(strict_types=1);

/** @var array{ok:?string,err:?string} $flash */
/** @var ?array{id:int,name:string,site_url:string,notes:?string} $project */
/** @var list<string> $properties */

$isNew = $project === null;
$pageTitle = $isNew ? __('projects.new_title') : __('projects.edit_title');
$pageHeading = $isNew ? __('projects.new_heading') : __('projects.edit_heading');
$activeNav = 'projects';
$hideHero = true;
$csrf = \SeoScope\Auth\AdminCsrf::token();
$name = $project['name'] ?? '';
$siteUrl = $project['site_url'] ?? '';
$notes = $project['notes'] ?? '';
$gscProperty = $project['gsc_property'] ?? '';
ob_start();
?>
<div class="dashboard-card section-card">
        <form method="post" action="<?= seoscope_h($isNew ? seoscope_url('/projects/new') : seoscope_url('/projects/edit')) ?>">
            <input type="hidden" name="_csrf" value="<?= seoscope_h($csrf) ?>">
            <?php if (!$isNew): ?>
                <input type="hidden" name="id" value="<?= (int) $project['id'] ?>">
            <?php endif; ?>
            <div class="mb-3">
                <label class="form-label"><?= seoscope_h(__('projects.field_name')) ?></label>
                <input type="text" name="name" class="form-control" required value="<?= seoscope_h($name) ?>">
            </div>
            <div class="mb-3">
                <label class="form-label"><?= seoscope_h(__('projects.field_url')) ?></label>
                <input type="url" name="site_url" class="form-control" required value="<?= seoscope_h($siteUrl) ?>" placeholder="https://">
            </div>
            <div class="mb-3">
                <label class="form-label"><?= seoscope_h(__('projects.field_notes')) ?></label>
                <textarea name="notes" class="form-control" rows="3"><?= seoscope_h($notes) ?></textarea>
            </div>
            <div class="mb-3">
                <label class="form-label"><?= seoscope_h(__('projects.field_gsc_property')) ?></label>
                <select name="gsc_property" class="form-select">
                    <option value=""><?= seoscope_h(__('projects.gsc_none')) ?></option>
                    <?php foreach (($properties ?? []) as $prop): ?>
                        <option value="<?= seoscope_h((string) $prop) ?>" <?= ((string) $prop === (string) $gscProperty) ? 'selected' : '' ?>>
                            <?= seoscope_h((string) $prop) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <div class="text-muted small mt-2"><?= seoscope_h(__('projects.gsc_help')) ?></div>
            </div>
            <button type="submit" class="btn btn-upgrade"><?= seoscope_h(__('projects.save')) ?></button>
            <a href="<?= seoscope_h(seoscope_url('/projects')) ?>" class="btn btn-link"><?= seoscope_h(__('projects.cancel')) ?></a>
        </form>
</div>
<?php
$content = ob_get_clean();
require __DIR__ . '/layout_admin.php';
