<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Reporte de Trámites</title>


    <style>

        @page {
            margin-top: 0;
            }
        body {
            margin: 0;
            padding-top: 70px;
            font-family: DejaVu Sans, sans-serif;
            font-size: 11px;
            color: #333;
        }

        .pdf-header {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 56px;
            background-color: #fff;
            border-bottom: 1px solid #ccc;
            z-index: 100;
        }

        .pdf-header-line {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 1px;
            background: #ccc;
        }

        .logo-img {
            position: fixed;
            top: 0;         
            left: 0;         
            height: 56px;
            width: auto;
            z-index: 101;
        }

        .pdf-title {
            position: fixed;
            top: 22px;      
            left: 50%;     
            transform: translateX(-50%);
            font-size: 12px;
            font-family: DejaVu Sans, sans-serif;
            text-transform: uppercase;
            white-space: nowrap;
            color: #000;
            z-index: 101;
            margin: 0;
        }

        .title {
            text-align: center;
            margin-bottom: 5px;
        }
        .title h1 {
            margin: 0;
            font-size: 20px;
            color: #2c3e50;
        }
        .generated {
            text-align: center;
            font-size: 10px;
            color: #777;
            margin-bottom: 15px;
        }
        .filters {
            margin-bottom: 15px;
            font-size: 11px;
            color: #444;
        }
        .filters strong { color: #2c3e50; }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        th, td {
            padding: 7px 6px;
            border: 1px solid #ccc;
            vertical-align: middle;
            text-align: center;
        }
        th {
            background-color: #f5f5f5;
            font-weight: bold;
            color: #2c3e50;
        }
        tr:nth-child(even) {
            background-color: #fafafa;
        }
        .no-results {
            text-align: center;
            margin-top: 30px;
            font-style: italic;
            color: #777;
        }
    </style>

</head>

<body>

    <div class="pdf-header">
        <?php
        $logoPath = realpath(__DIR__ . '/../../../public/assets/favicon.png');
        if ($logoPath && file_exists($logoPath)) {
            $logoData = base64_encode(file_get_contents($logoPath));
            $logoSrc = 'data:image/png;base64,' . $logoData;
        } else {
            $logoSrc = '';
        }
        ?>

        <?php if ($logoSrc): ?>
            <img src="<?= $logoSrc ?>" alt="Logo" class="logo-img">
        <?php else: ?>
            <p>Logo no disponible</p>
        <?php endif; ?>

        <div class="pdf-title"><?= htmlspecialchars($nombreAnio) ?></div>
    </div>
    
    <div class="title">
        <h1><?= htmlspecialchars($report_title) ?></h1>
    </div>

    <div class="generated">
        Generado el <?= date('d/m/Y H:i') ?>
    </div>

<?php if (!empty($processes)): ?>
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Expediente</th>
                <th>Administrado</th>
                <th>DNI</th>
                <th>Procedimiento</th>
                <th>Asunto</th>
                <th>Folios</th>
                <th>Estado</th>
                <th>Fecha</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($processes as $index => $p): ?>
            <tr>
                <td><?= $index + 1 ?></td>
                <td><?= htmlspecialchars($p->num_exped) ?></td>
                <td><?= htmlspecialchars($p->user()->name . ' ' . $p->user()->paternal_surname . ' ' . $p->user()->maternal_surname) ?></td>
                <td><?= htmlspecialchars($p->user()->dni) ?></td>
                <td><?= htmlspecialchars($p->procedure()->description ?? 'Sin procedimiento') ?></td>
                <td><?= htmlspecialchars($p->subject) ?></td>
                <td><?= htmlspecialchars($p->folios) ?></td>
                <td><?= htmlspecialchars($p->general_state) ?></td>
                <td><?= date('d/m/Y', strtotime($p->entry_date)) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

<?php elseif (!empty($derivation_results)): ?>
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Expediente</th>
                <th>Administrado</th>
                <th>DNI</th>
                <th>Procedimiento</th>
                <th>Asunto</th>
                <th>Fecha de derivación</th>
                <th>Derivado a</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($derivation_results as $index => $d): ?>
            <tr>
                <td><?= $index + 1 ?></td>
                <td><?= htmlspecialchars($d['num_exped']) ?></td>
                <td><?= htmlspecialchars($d['user_name']) ?></td>
                <td><?= htmlspecialchars($d['user_dni']) ?></td>
                <td><?= htmlspecialchars($d['procedure_description'] ?? 'Sin procedimiento') ?></td>
                <td><?= htmlspecialchars($d['subject']) ?></td>
                <td><?= date('d/m/Y', strtotime($d['derived_date'])) ?></td>
                <td><?= htmlspecialchars($d['subgroup_name'] ?? $d['group_name'] ?? 'N/A') ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

<?php elseif (!empty($state_results)): ?>
    <?php foreach ($state_results as $area => $grupos): ?>
        <h2 style="margin-top: 20px;"><?= htmlspecialchars($area) ?></h2>

        <?php foreach ($grupos as $grupo => $subgrupos): ?>
            <h3 style="margin-top: 10px; margin-left: 10px;"><?= htmlspecialchars("Grupo: $grupo") ?></h3>

            <?php foreach ($subgrupos as $subgrupo => $items): ?>
                <h4 style="margin-left: 20px;">
                    <?= $subgrupo !== 'default' ? "Subgrupo: " . htmlspecialchars($subgrupo) : 'Sin subgrupo' ?>
                </h4>

                <table border="1" cellspacing="0" cellpadding="5"
                       style="margin: 10px 0; width: 100%; border-collapse: collapse; box-sizing: border-box;">
                    <thead>
                        <tr>
                            <th style="text-align: left;">N°</th>
                            <th style="text-align: left;">Expediente</th>
                            <th style="text-align: left;">Asunto</th>
                            <th style="text-align: left;">Fecha de entrada</th>
                            <th style="text-align: left;">Días transcurridos</th>
                            <th style="text-align: left;">Estado</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($items as $index => $item): ?>
                            <tr>
                                <td><?= $index + 1 ?></td>
                                <td><?= htmlspecialchars($item['expediente']) ?></td>
                                <td><?= htmlspecialchars($item['asunto']) ?></td>
                                <td><?= date('d/m/Y', strtotime($item['fecha_ingreso'])) ?></td>
                                <td><?= htmlspecialchars($item['dias_transcurridos']) ?></td>
                                <td>
                                    <span style="color: <?= htmlspecialchars($item['color'] ?? '#000') ?>">
                                        <?= htmlspecialchars($item['estado']) ?>
                                    </span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endforeach; ?>
        <?php endforeach; ?>
    <?php endforeach; ?>


<?php else: ?>
    <p class="no-results">No se encontraron resultados para esta búsqueda.</p>
<?php endif; ?>

</body>
</html>
