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

<?= $this->section('content') ?>
<?php
$photos = $photos ?? [];
$sectionLabels = [
    'general' => 'Evidencia general',
    'antes' => 'Antes de la intervención',
    'durante' => 'Durante la intervención',
    'despues' => 'Después de la intervención',
];
$groupedPhotos = [];
foreach ($photos as $photo) {
    $groupedPhotos[$photo['section'] ?? 'general'][] = $photo;
}
?>
<section class="card stack-md">
    <div class="section-head">
        <div>
            <h3><?= esc($report['folio'] ?? '#' . $report['id']) ?> · <?= esc($report['title']) ?></h3>
            <p><?= esc($client['nombre'] ?? '') ?></p>
        </div>
        <div class="actions-row">
            <a class="btn btn-primary" href="<?= site_url('reports/' . $report['id'] . '/pdf') ?>" target="_blank">Descargar PDF</a>
        </div>
    </div>

    <form method="post" action="<?= site_url('reports/' . $report['id'] . '/update') ?>" class="stack-md">
        <?= csrf_field() ?>
        <div>
            <label>Título</label>
            <input type="text" name="title" value="<?= esc($report['title']) ?>">
        </div>
        <div>
            <label>Trabajos realizados / observaciones</label>
            <textarea name="notas" rows="4"><?= esc($report['notas'] ?? '') ?></textarea>
        </div>
        <div>
            <label>Status</label>
            <select name="status">
                <option value="draft" <?= $report['status'] === 'draft' ? 'selected' : '' ?>>Borrador</option>
                <option value="sent" <?= $report['status'] === 'sent' ? 'selected' : '' ?>>Enviado</option>
            </select>
        </div>
        <button class="btn btn-primary" type="submit">Guardar</button>
    </form>

    <h4>Inspecciones incluidas</h4>
    <?php if ($inspections === []): ?>
        <p class="muted">Sin inspecciones vinculadas.</p>
    <?php else: ?>
        <ul>
            <?php foreach ($inspections as $i): ?>
                <li><a href="<?= site_url('inspections/' . $i['id']) ?>"><?= esc($i['folio']) ?></a> — <?= esc($i['sistema'] ?? '') ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>

    <div class="section-head">
        <div>
            <h4>Evidencia fotográfica</h4>
            <p>Estas fotos aparecerán como galería al final del PDF del reporte.</p>
        </div>
    </div>

    <form method="post" action="<?= site_url('reports/' . $report['id'] . '/photos') ?>" enctype="multipart/form-data" class="stack-sm">
        <?= csrf_field() ?>
        <div class="grid grid-3">
            <div>
                <label>Sección</label>
                <select name="section">
                    <?php foreach ($sectionLabels as $key => $label): ?>
                        <option value="<?= esc($key) ?>"><?= esc($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label>Descripción opcional</label>
                <input type="text" name="caption" placeholder="Ej. Hidrante antes de la modificación">
            </div>
            <div>
                <label>Fotos</label>
                <input type="file" name="photos[]" accept="image/*" multiple required>
            </div>
        </div>
        <button class="btn btn-primary" type="submit">Agregar fotos</button>
    </form>

    <?php if ($photos === []): ?>
        <p class="muted">Aún no hay fotos en este reporte.</p>
    <?php else: ?>
        <div class="report-gallery stack-md">
            <?php foreach ($sectionLabels as $section => $label): ?>
                <?php if (empty($groupedPhotos[$section])): continue; endif; ?>
                <div class="stack-sm">
                    <h5><?= esc($label) ?></h5>
                    <div class="gallery-grid">
                        <?php foreach ($groupedPhotos[$section] as $photo): ?>
                            <figure class="gallery-item">
                                <img src="<?= esc(site_url('reports/' . $report['id'] . '/photos/' . $photo['id'])) ?>" alt="<?= esc($photo['caption'] ?: $label) ?>">
                                <?php if (! empty($photo['caption'])): ?>
                                    <figcaption><?= esc($photo['caption']) ?></figcaption>
                                <?php endif; ?>
                                <form method="post" action="<?= site_url('reports/' . $report['id'] . '/photos/' . $photo['id'] . '/delete') ?>">
                                    <?= csrf_field() ?>
                                    <button class="link-button" type="submit">Eliminar</button>
                                </form>
                            </figure>
                        <?php endforeach; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>

    <p class="muted">Verificación pública: <a href="<?= site_url('verify/' . $report['verify_token']) ?>" target="_blank"><?= esc(site_url('verify/' . $report['verify_token'])) ?></a></p>
</section>
<?= $this->endSection() ?>
