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

<?= $this->section('content') ?>
<section class="qb-form-shell">
    <div class="max-w-3xl">
        <span class="qb-chip qb-chip--soft">Registo de intervenção</span>
        <h1 class="mt-4 text-3xl font-semibold text-white"><?= esc($intervention ? 'Editar intervenção técnica' : 'Nova intervenção técnica') ?></h1>
        <p class="mt-3 text-sm leading-7 text-slate-400">Registo base de manutenção Quantum com foco em auditoria, governança operacional e rastreabilidade.</p>
    </div>

    <form action="<?= esc($action) ?>" method="post" class="qb-form-grid mt-6">
        <?= csrf_field() ?>
        <label class="qb-form-field">
            <span>Codigo</span>
            <input name="intervention_code" value="<?= esc(old('intervention_code', $intervention['intervention_code'] ?? '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Titulo</span>
            <input name="title" value="<?= esc(old('title', $intervention['title'] ?? '')) ?>">
        </label>
        <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', $intervention['site_id'] ?? 0) === (int) $site['id'] ? 'selected' : '' ?>>
                        <?= esc($site['name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>Turbina</span>
            <select name="turbine_id">
                <option value="">Sem turbina especifica</option>
                <?php foreach ($turbines as $turbine): ?>
                    <option value="<?= esc($turbine['id']) ?>" <?= (int) old('turbine_id', $intervention['turbine_id'] ?? 0) === (int) $turbine['id'] ? 'selected' : '' ?>>
                        <?= esc($turbine['turbine_code']) ?> - <?= esc($turbine['site_name'] ?? '') ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>Tipo</span>
            <select name="intervention_type">
                <?php foreach (['preventive', 'corrective', 'inspection', 'audit', 'emergency'] as $type): ?>
                    <option value="<?= esc($type) ?>" <?= old('intervention_type', $intervention['intervention_type'] ?? 'preventive') === $type ? 'selected' : '' ?>><?= esc($type) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>Modo</span>
            <select name="maintenance_mode">
                <?php foreach (['scheduled', 'corrective', 'condition_based', 'outage'] as $mode): ?>
                    <option value="<?= esc($mode) ?>" <?= old('maintenance_mode', $intervention['maintenance_mode'] ?? 'scheduled') === $mode ? 'selected' : '' ?>><?= esc($mode) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>Prioridade</span>
            <select name="priority">
                <?php foreach (['low', 'medium', 'high', 'critical'] as $priority): ?>
                    <option value="<?= esc($priority) ?>" <?= old('priority', $intervention['priority'] ?? 'medium') === $priority ? 'selected' : '' ?>><?= esc($priority) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>Estado</span>
            <select name="status">
                <?php foreach (['planned', 'in_progress', 'completed', 'cancelled', 'on_hold'] as $status): ?>
                    <option value="<?= esc($status) ?>" <?= old('status', $intervention['status'] ?? 'planned') === $status ? 'selected' : '' ?>><?= esc($status) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>Responsavel</span>
            <select name="assigned_to_user_id">
                <option value="">Sem responsavel</option>
                <?php foreach ($users as $user): ?>
                    <option value="<?= esc($user['id']) ?>" <?= (int) old('assigned_to_user_id', $intervention['assigned_to_user_id'] ?? 0) === (int) $user['id'] ? 'selected' : '' ?>>
                        <?= esc($user['name']) ?> (<?= esc($user['role']) ?>)
                    </option>
                <?php endforeach; ?>
            </select>
        </label>
        <label class="qb-form-field">
            <span>Referencia externa</span>
            <input name="external_reference" value="<?= esc(old('external_reference', $intervention['external_reference'] ?? '')) ?>">
        </label>
        <label class="qb-form-field qb-form-field--full">
            <span>Descricao</span>
            <textarea name="description" rows="4"><?= esc(old('description', $intervention['description'] ?? '')) ?></textarea>
        </label>
        <label class="qb-form-field">
            <span>Data de reporte</span>
            <input type="datetime-local" name="reported_at" value="<?= esc(old('reported_at', isset($intervention['reported_at']) && $intervention['reported_at'] ? str_replace(' ', 'T', substr($intervention['reported_at'], 0, 16)) : '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Inicio planeado</span>
            <input type="datetime-local" name="scheduled_start_at" value="<?= esc(old('scheduled_start_at', isset($intervention['scheduled_start_at']) && $intervention['scheduled_start_at'] ? str_replace(' ', 'T', substr($intervention['scheduled_start_at'], 0, 16)) : '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Fim planeado</span>
            <input type="datetime-local" name="scheduled_end_at" value="<?= esc(old('scheduled_end_at', isset($intervention['scheduled_end_at']) && $intervention['scheduled_end_at'] ? str_replace(' ', 'T', substr($intervention['scheduled_end_at'], 0, 16)) : '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Inicio real</span>
            <input type="datetime-local" name="started_at" value="<?= esc(old('started_at', isset($intervention['started_at']) && $intervention['started_at'] ? str_replace(' ', 'T', substr($intervention['started_at'], 0, 16)) : '')) ?>">
        </label>
        <label class="qb-form-field">
            <span>Conclusao</span>
            <input type="datetime-local" name="completed_at" value="<?= esc(old('completed_at', isset($intervention['completed_at']) && $intervention['completed_at'] ? str_replace(' ', 'T', substr($intervention['completed_at'], 0, 16)) : '')) ?>">
        </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 intervenção</button>
            <a href="<?= site_url('maintenance') ?>" class="qb-button-secondary">Cancelar</a>
        </div>
    </form>
</section>
<?= $this->endSection() ?>
