<?php
$company = $company ?? company_profile();
$logo = $logoPath ?? null;
$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">REPORTE DE SERVICIO</div>
                <div class="sub"><?= esc($report['title']) ?></div>
                <div class="folio">Folio: <?= esc($report['folio']) ?></div>
            </td>
        </tr>
    </table>
</div>

<table class="header-table">
    <tr>
        <td class="lbl">CLIENTE</td><td><?= esc($client['nombre'] ?? '') ?></td>
        <td class="lbl">CONTRATO</td><td><?= esc($client['contrato'] ?? '') ?></td>
    </tr>
    <tr>
        <td class="lbl">DIRECCIÓN</td><td colspan="3"><?= esc($client['direccion'] ?? '') ?></td>
    </tr>
</table>

<div class="section-title">INSPECCIONES INCLUIDAS</div>
<table class="items">
    <thead>
        <tr>
            <th>Folio</th><th>Sistema</th><th>Fecha</th><th>Status</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($inspections as $i): ?>
        <tr>
            <td><?= esc($i['folio']) ?></td>
            <td><?= esc($i['sistema'] ?? '') ?></td>
            <td><?= esc($i['fecha'] ?? '') ?></td>
            <td><?= esc($i['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 (! empty($report['notas'])): ?>
    <div class="observaciones">
        <h4>DESCRIPCIÓN DEL TRABAJO / OBSERVACIONES</h4>
        <div><?= nl2br(esc($report['notas'])) ?></div>
    </div>
<?php endif; ?>

<div class="firma-block">
    <table>
        <tr>
            <td style="width:60%">
                <strong>ENTREGADO POR</strong>
                <div style="height:50pt"></div>
                <div class="sig-name"><?= esc($company['name']) ?></div>
            </td>
            <td class="qr-box" style="width:40%">
                <barcode code="<?= esc($verifyUrl) ?>" type="QR" class="barcode" size="0.9" error="M" />
                <small>Verifica este reporte:<br><?= esc($verifyUrl) ?></small>
            </td>
        </tr>
    </table>
    <?php if (! empty($clientSignature)): ?>
    <table style="margin-top:10pt">
        <tr>
            <td>
                <strong>RECIBIDO POR EL CLIENTE</strong>
                <div><img class="sig-img" src="<?= esc(WRITEPATH . $clientSignature['png_path']) ?>" alt=""></div>
                <div class="sig-name"><?= esc($clientSignature['name'] ?? '') ?> · <?= esc($clientSignature['signed_at']) ?></div>
            </td>
        </tr>
    </table>
    <?php endif; ?>
	</div>

<?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>
