<?php
$company = $company ?? company_profile();
$logo = $logoPath ?? null;
$statusLabels = ['draft' => 'Borrador', 'signed' => 'Firmada', 'delivered' => 'Entregada'];
$photos = $photos ?? [];
$sectionLabels = [
    'general' => 'EVIDENCIA FOTOGRÁFICA',
    'antes' => 'EVIDENCIA FOTOGRÁFICA - ANTES',
    'durante' => 'EVIDENCIA FOTOGRÁFICA - DURANTE',
    'despues' => 'EVIDENCIA FOTOGRÁFICA - DESPUÉS',
];
$groupedPhotos = [];
foreach ($photos as $photo) {
    $groupedPhotos[$photo['section'] ?? 'general'][] = $photo;
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <?= $this->include('pdf/_styles') ?>
</head>
<body>

<div class="brand-bar">
    <table>
        <tr>
            <td class="logo-cell">
                <?php if ($logo): ?><img src="<?= esc($logo) ?>" alt=""><?php endif; ?>
            </td>
            <td class="title-cell">
                <div class="title">ACTA DE ENTREGA</div>
                <div class="sub"><?= esc($note['title']) ?></div>
                <div class="folio">Folio: <?= esc($note['folio']) ?></div>
            </td>
        </tr>
    </table>
</div>

<table class="header-table">
    <tr>
        <td class="lbl">CLIENTE</td><td><?= esc($client['nombre'] ?? '') ?></td>
        <td class="lbl">ESTADO</td><td><?= esc($statusLabels[$note['status']] ?? $note['status']) ?></td>
    </tr>
    <tr>
        <td class="lbl">DIRECCIÓN</td><td colspan="3"><?= esc($client['direccion'] ?? '') ?></td>
    </tr>
    <tr>
        <td class="lbl">PLANTILLA</td><td><?= esc(($template['code'] ?? '') . ' · ' . ($template['name'] ?? '')) ?></td>
        <td class="lbl">FECHA</td><td><?= esc(date('Y-m-d', strtotime($note['created_at'] ?? 'now'))) ?></td>
    </tr>
</table>

<div class="section-title">TEXTO DEL ACTA</div>
<div class="observaciones acta-body">
    <?= $renderedBody ?>
</div>

<div class="section-title">INSPECCIONES RELACIONADAS</div>
<table class="items">
    <thead>
        <tr>
            <th>Folio</th><th>Sistema</th><th>Fecha</th><th>Estado</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($inspections as $inspection): ?>
        <tr>
            <td><?= esc($inspection['folio']) ?></td>
            <td><?= esc($inspection['sistema'] ?? '') ?></td>
            <td><?= esc($inspection['fecha'] ?? '') ?></td>
            <td><?= esc($inspection['status']) ?></td>
        </tr>
    <?php endforeach; ?>
    <?php if ($inspections === []): ?>
        <tr><td colspan="4" style="text-align:center">— sin inspecciones —</td></tr>
    <?php endif; ?>
    </tbody>
</table>

<?php if ($signatures !== []): ?>
    <div class="firma-block">
        <table>
            <?php foreach ($fields as $field): ?>
                <?php
                if (($field['type'] ?? '') !== 'signature') {
                    continue;
                }
                $key = (string) $field['key'];
                $sig = $signatures[$key] ?? null;
                ?>
                <tr>
                    <td style="width:60%">
                        <strong><?= esc($field['label']) ?></strong>
                        <?php if ($sig): ?>
                            <div><img class="sig-img" src="<?= esc(WRITEPATH . $sig['png_path']) ?>" alt=""></div>
                            <div class="sig-name"><?= esc($sig['name'] ?? '') ?> · <?= esc($sig['signed_at']) ?></div>
                        <?php else: ?>
                            <div style="height:50pt"></div>
                            <div class="sig-name">Pendiente de firma</div>
                        <?php endif; ?>
                    </td>
                    <td class="qr-box" style="width:40%">
                        <?php if ($key === array_key_first($signatures)): ?>
                            <barcode code="<?= esc($verifyUrl) ?>" type="QR" class="barcode" size="0.9" error="M" />
                            <small>Verifica este documento:<br><?= esc($verifyUrl) ?></small>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </table>
    </div>
<?php endif; ?>

<?php if ($photos !== []): ?>
    <?php foreach ($sectionLabels as $section => $label): ?>
        <?php if (empty($groupedPhotos[$section])): continue; endif; ?>
        <pagebreak />
        <div class="section-title"><?= esc($label) ?></div>
        <table class="photo-gallery">
            <tbody>
            <?php foreach (array_chunk($groupedPhotos[$section], 2) as $row): ?>
                <tr>
                    <?php foreach ($row as $photo): ?>
                        <td>
                            <div class="photo-box">
                                <img src="<?= esc(WRITEPATH . $photo['file_path']) ?>" alt="">
                                <?php if (! empty($photo['caption'])): ?>
                                    <div class="caption"><?= esc($photo['caption']) ?></div>
                                <?php endif; ?>
                            </div>
                        </td>
                    <?php endforeach; ?>
                    <?php if (count($row) === 1): ?><td></td><?php endif; ?>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endforeach; ?>
<?php endif; ?>

<div class="foot">
    <?= esc($company['name']) ?> · <?= esc($company['website']) ?> · <?= esc($company['tagline']) ?>
</div>

</body>
</html>
