<?= $this->extend('layouts/dashboard') ?>

<?= $this->section('content') ?>
<section class="qb-form-shell">
    <div class="max-w-3xl">
        <span class="qb-chip qb-chip--soft">Cadastro de ativo</span>
        <h1 class="mt-4 text-3xl font-semibold text-white"><?= esc($site ? 'Editar parque eólico' : 'Novo parque eólico') ?></h1>
        <p class="mt-3 text-sm leading-7 text-slate-400">Regista a estrutura base do parque eólico ou do site técnico Quantum com contexto operacional e geográfico.</p>
    </div>

    <form action="<?= esc($action) ?>" method="post" class="qb-form-grid mt-6">
        <?= csrf_field() ?>
        <?php if ($isPlatformAdmin): ?>
            <label class="qb-form-field">
                <span>Organização</span>
                <select name="organization_id">
                    <?php foreach ($organizations as $organization): ?>
                        <option value="<?= esc($organization['id']) ?>" <?= (int) old('organization_id', $site['organization_id'] ?? 0) === (int) $organization['id'] ? 'selected' : '' ?>>
                            <?= esc($organization['name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </label>
        <?php endif; ?>
        <label class="qb-form-field">
            <span>Nome</span>
            <input name="name" value="<?= esc(old('name', $site['name'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Código do parque</span>
            <input name="site_code" value="<?= esc(old('site_code', $site['site_code'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Tipo</span>
            <select name="site_type">
                <?php foreach (['wind_farm', 'site', 'substation', 'service_hub'] as $type): ?>
                    <option value="<?= esc($type) ?>" <?= old('site_type', $site['site_type'] ?? 'wind_farm') === $type ? 'selected' : '' ?>><?= esc($type) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>País</span>
            <input name="country_code" value="<?= esc(old('country_code', $site['country_code'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Região</span>
            <input name="region" value="<?= esc(old('region', $site['region'] ?? '')) ?>">
        </label>
        <label class="qb-form-field qb-form-field--full">
            <span>Localização</span>
            <input name="location" value="<?= esc(old('location', $site['location'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Latitude</span>
            <input name="latitude" value="<?= esc(old('latitude', $site['latitude'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Longitude</span>
            <input name="longitude" value="<?= esc(old('longitude', $site['longitude'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Data de entrada em serviço</span>
            <input type="date" name="commissioned_at" value="<?= esc(old('commissioned_at', $site['commissioned_at'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Estado</span>
            <select name="status">
                <?php foreach (['active', 'inactive', 'pending', 'maintenance'] as $status): ?>
                    <option value="<?= esc($status) ?>" <?= old('status', $site['status'] ?? 'active') === $status ? 'selected' : '' ?>><?= esc($status) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <div class="qb-form-field qb-form-field--full flex flex-wrap gap-3 pt-2">
            <button type="submit" class="qb-button-primary">Guardar parque eólico</button>
            <a href="<?= site_url('sites') ?>" class="qb-button-secondary">Cancelar</a>
        </div>
    </form>
</section>
<?= $this->endSection() ?>
