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

<?= $this->section('content') ?>
<section class="qb-form-shell">
    <div class="max-w-3xl">
        <span class="qb-chip qb-chip--soft">Ativo industrial</span>
        <h1 class="mt-4 text-3xl font-semibold text-white"><?= esc($turbine ? 'Editar turbina' : 'Nova turbina') ?></h1>
        <p class="mt-3 text-sm leading-7 text-slate-400">Configura a turbina com identificação técnica, fabricante, modelo e parâmetros de operação.</p>
    </div>

    <form action="<?= esc($action) ?>" method="post" class="qb-form-grid mt-6">
        <?= csrf_field() ?>
        <label class="qb-form-field">
            <span>Parque eólico</span>
            <select name="site_id">
                <?php foreach ($sites as $site): ?>
                    <option value="<?= esc($site['id']) ?>" <?= (int) old('site_id', $turbine['site_id'] ?? 0) === (int) $site['id'] ? 'selected' : '' ?>>
                        <?= esc($site['name']) ?><?= ! empty($site['organization_name']) ? ' - ' . esc($site['organization_name']) : '' ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>Código</span>
            <input name="turbine_code" value="<?= esc(old('turbine_code', $turbine['turbine_code'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Nome</span>
            <input name="name" value="<?= esc(old('name', $turbine['name'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Fabricante</span>
            <input name="manufacturer" value="<?= esc(old('manufacturer', $turbine['manufacturer'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Modelo</span>
            <input name="model" value="<?= esc(old('model', $turbine['model'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Serial</span>
            <input name="serial_number" value="<?= esc(old('serial_number', $turbine['serial_number'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Potência kW</span>
            <input name="rated_power_kw" value="<?= esc(old('rated_power_kw', $turbine['rated_power_kw'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Altura do cubo (m)</span>
            <input name="hub_height_m" value="<?= esc(old('hub_height_m', $turbine['hub_height_m'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Diâmetro do rotor (m)</span>
            <input name="rotor_diameter_m" value="<?= esc(old('rotor_diameter_m', $turbine['rotor_diameter_m'] ?? '')) ?>">
        </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', $turbine['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', $turbine['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 turbina</button>
            <a href="<?= site_url('turbines') ?>" class="qb-button-secondary">Cancelar</a>
        </div>
    </form>
</section>
<?= $this->endSection() ?>
