<!-- partials/finalizados_table.php -->
<table class="table table-sm table-bordered">
    <thead>
        <tr>
            <th>idServ</th>
            <th>Fecha</th>
            <th>Cliente</th>
            <th>Producto</th>

            <th>Logo</th>
            <th>Cant</th>
            <th>Precio Venta</th>
            <th>Total</th>
            <th>Taller</th>
            <th>Estado</th>
            <th>Anexo</th>
            <th>Cant Taller</th>
            <th>Precio</th>
            <th>Total Servicio</th>
            <th>Diferencia</th>
            <th>Mes</th>
        </tr>
    </thead>
    <tbody>
        <?php
        global $estados, $meses;
        $mes = '';
        $st = $ss = $sd = 0;
        foreach (($DATASET ?? []) as $r):
            // Al cambiar de mes, imprimimos totales del mes anterior
            if ($mes !== $r['mes']):
                if ($mes !== ''):
        ?>
                    <tr class="table-secondary fw-bold">
                        <td colspan="6">Totales <?= htmlspecialchars($mes) ?></td>
                        <td class="text-right"><?= fmt($st) ?></td>
                        <td colspan="4"></td>
                        <td></td>
                        <td class="text-right"><?= fmt($ss) ?></td>
                        <td class="text-right"><?= fmt($sd) ?></td>
                        <td></td>
                    </tr>
            <?php
                endif;
                $mes = $r['mes'];
                $st = $ss = $sd = 0;
            endif;

            // Acumular valores
            $st += $r['total'];
            $ss += $r['totalServicio'];
            $sd += $r['diferencia'];
            ?>
            <tr data-id="<?= $r['idServ'] ?>">
                <td><?= $r['idServ'] ?></td>
                <td><?= $r['fechaOrden'] ? date('d-M-y', strtotime($r['fechaOrden'])) : '-' ?></td>
                <td><?= htmlspecialchars($r['cliente']) ?></td>
                <td><?= htmlspecialchars($r['producto']) ?></td>

                <td><?= htmlspecialchars($r['logo']) ?></td>
                <td class="text-right"><?= intval($r['cantVenta']) ?></td>
                <td class="text-right"><?= fmt($r['precioVenta']) ?></td>
                <td class="text-right"><?= fmt($r['total']) ?></td>
                <td><?= htmlspecialchars($r['taller']) ?></td>
                <td>
                    <?php if (!$r['tieneAnexo']): ?>
                        <select class="form-select form-select-sm field"
                            data-field="idEstadoTrabajo"
                            style="font-size:0.75rem;max-width:6rem;">
                            <?php foreach ($estados as $es): ?>
                                <option value="<?= $es['id'] ?>"
                                    <?= $es['id'] == $r['idEstadoTrabajo'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($es['nombre']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    <?php else: ?>
                        <?= htmlspecialchars($r['estado']) ?>
                    <?php endif; ?>
                </td>
                <!-- Anexo -->
                <td class="text-center">
                    <?= $r['tieneAnexo'] ? '✓' : '-' ?>
                </td>
                <td class="text-right"><?= $r['cantTaller'] ?></td>
                <td class="text-right"><?= fmt($r['precioUnitario']) ?></td>
                <td class="text-right"><?= fmt($r['totalServicio']) ?></td>
                <td class="text-right"><?= fmt($r['diferencia']) ?></td>
                <!-- Mes -->
                <td>
                    <?php if (!$r['tieneAnexo']): ?>
                        <select class="form-select form-select-sm field"
                            data-field="idMesContable"
                            style="font-size:0.75rem;max-width:6rem;">
                            <option value="" disabled <?= $r['idMesContable'] ? '' : 'selected' ?>>--</option>
                            <?php foreach ($meses as $m): ?>
                                <option value="<?= $m['id'] ?>"
                                    <?= $m['id'] == $r['idMesContable'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($m['nombre']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    <?php else: ?>
                        <?= htmlspecialchars($r['mes']) ?>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>

        <?php if ($mes !== ''): ?>
            <tr class="table-secondary fw-bold">
                <td colspan="6">Totales <?= htmlspecialchars($mes) ?></td>
                <td class="text-right"><?= fmt($st) ?></td>
                <td colspan="4"></td>
                <td></td>
                <td class="text-right"><?= fmt($ss) ?></td>
                <td class="text-right"> <?= fmt($sd) ?></td>
                <td></td>
            </tr>
        <?php endif; ?>
    </tbody>
</table>