<!-- File: app/Views/catalogo_configuracion_libro.php -->
<div class="accordion accordion-bordered mt-3" id="accordionConfiguracionLibro">
    <div class="card accordion-item active">
        <h2 class="accordion-header" id="headingConfiguracionLibro">
            <button class="accordion-button" type="button" data-bs-toggle="collapse"
                data-bs-target="#collapseConfiguracionLibro" aria-expanded="true"
                aria-controls="collapseConfiguracionLibro">
                <h5 class="mb-0"><?= lang('Catalogo.configuracionLibro') ?: 'Configuración del libro' ?></h5>
            </button>
        </h2>

        <div id="collapseConfiguracionLibro" class="accordion-collapse collapse show"
            data-bs-parent="#accordionConfiguracionLibro">
            <div class="accordion-body">

                <?php
                // 1) TIPO DE IMPRESIÓN + ENCUADERNACIÓN
                $tipos = ['negro', 'negrohq', 'color', 'colorhq'];
                ?>
                <div class="divider divider-dark text-start mb-1">
                    <div class="divider-text">
                        <h5><?= lang('Presupuestos.tipoImpresion') ?></h5>
                    </div>
                </div>
                <div class="row px-4 mt-1">
                    <div class="col-lg-3 col-md-12">
                        <select id="tipo_impresion" name="tipo_impresion" class="form-control select2bs2 warning-change"
                            style="width:100%" data-placeholder="<?= lang('Catalogo.seleccionarTipoImpresion') ?>">
                            <option></option>
                            <?php foreach ($tipos as $t): ?>
                                <option value="<?= $t ?>" <?= $catalogoLibrosEntity->tipo_impresion === $t ? 'selected' : '' ?>>
                                    <?= lang("MaquinasTarifasImpresions.$t") ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-lg-3 col-md-12">
                        <select id="encuadernacion_id" name="encuadernacion_id"
                            class="form-control select2bs2 warning-change" style="width:100%">
                            <option value="<?= $catalogoLibrosEntity->encuadernacion_id ?>" selected>
                                <?= esc($catalogoLibrosEntity->encuadernacionName) ?>
                            </option>
                        </select>
                    </div>
                </div>

                <?php
                // 2) COLUMNAS Y ETIQUETAS COMPARTIDAS
                $cols = [2, 2, 2, 2, 2, 2];
                $labels = ['', 'numeroPaginas', 'papel', 'gramaje', 'solapas', 'acabados'];

                // Función única para renderizar cualquier fila, con clase opcional
                $renderRow = function (array $cellsHtml, string $rowClass = '') use ($cols) {
                    $classAttr = $rowClass ? " {$rowClass}-line" : '';
                    echo "<div class=\"row{$classAttr} mb-0\">";
                    foreach ($cellsHtml as $i => $cell) {
                        $w = $cols[$i] ?? 2;
                        echo "<div class=\"col-lg-{$w} col-md-12 px-4 mb-0\">{$cell}</div>";
                    }
                    echo '<div><hr class="my-1"></div></div>';
                };
                ?>

                <div class="divider divider-dark text-start mb-1">
                    <div class="divider-text">
                        <h5><?= lang('Presupuestos.papelesComparadorPresupuestoAdmin') ?></h5>
                    </div>
                </div>

                <!-- 2a) Encabezado -->
                <?php
                $hdr = [];
                foreach ($labels as $lbl) {
                    $hdr[] = '<p class="mb-0">' . ($lbl ? lang("Presupuestos.$lbl") : '') . '</p>';
                }
                echo '<div class="row mb-1">';
                foreach ($hdr as $i => $html) {
                    echo '<div class="col-lg-' . $cols[$i] . ' col-md-12 px-4 mb-0">' . $html . '</div>';
                }
                echo '<div><hr class="my-1"><hr class="my-1"></div></div>';
                ?>

                <!-- 3) Filas dinámicas: negro y color -->
                <?php foreach (['negro', 'negro_pod', 'color', 'color_pod'] as $tipo):
                    $pag = old("{$tipo}_paginas", $catalogoLibrosEntity->{"{$tipo}_paginas"});
                    $papId = $catalogoLibrosEntity->{"{$tipo}_papel_id"};
                    $papNm = $catalogoLibrosEntity->{"{$tipo}PapelName"};
                    $gram = $catalogoLibrosEntity->{"{$tipo}_gramaje"};

                    $cells = [
                        '<p>' . lang("MaquinasTarifasImpresions.$tipo") . '</p>',
                        "<input type=\"text\" id=\"{$tipo}_paginas\" name=\"{$tipo}_paginas\" placeholder=\"0\" maxlength=\"5\" class=\"form-control {$tipo}_items\" value=\"" . esc($pag) . "\">",
                        "<select id=\"{$tipo}_papel_id\" name=\"{$tipo}_papel_id\" class=\"form-control select2bs2 {$tipo}_items\" style=\"width:100%\">"
                        . ($papNm ? "<option value=\"" . esc($papId) . "\" selected>" . esc($papNm) . "</option>" : "")
                        . "</select>",
                        "<select id=\"{$tipo}_gramaje\" name=\"{$tipo}_gramaje\" class=\"form-control select2bs2 {$tipo}_items\" style=\"width:100%\">"
                        . "<option value=\"" . esc($gram) . "\" selected>" . esc($gram) . "</option></select>",
                        '', // solapas
                        ''  // acabados
                    ];
                    $renderRow($cells, $tipo);
                endforeach; ?>

                <!-- 4) Cubierta y Sobrecubierta -->
                <?php
                $especiales = [
                    'cubierta' => ['faces' => [2 => 'unaCara', 4 => 'dosCaras'], 'disable' => false],
                    'cubierta_pod' => ['faces' => [2 => 'unaCara', 4 => 'dosCaras'], 'disable' => false],
                    'sobrecubierta' => ['faces' => [0 => 'no', 1 => 'si'], 'disable' => in_array($catalogoLibrosEntity->tipo_impresion_id, [5, 6, 7, 8, 21])],
                    'sobrecubierta_pod' => ['faces' => [0 => 'no', 1 => 'si'], 'disable' => in_array($catalogoLibrosEntity->tipo_impresion_id, [5, 6, 7, 8, 21])]
                ];
                foreach ($especiales as $tipo => $cfg):
                    $pag = $catalogoLibrosEntity->{"{$tipo}_paginas"};
                    $papId = $catalogoLibrosEntity->{"{$tipo}_papel_id"};
                    $papNm = $catalogoLibrosEntity->{"{$tipo}PapelName"};
                    $gram = $catalogoLibrosEntity->{"{$tipo}_gramaje"};
                    $sol = old("{$tipo}_solapas_ancho", $catalogoLibrosEntity->{"{$tipo}_ancho_solapas"});
                    $acId = $catalogoLibrosEntity->{"{$tipo}_acabado_id"};
                    $acNm = $catalogoLibrosEntity->{"{$tipo}AcabadoName"};

                    $optPag = '';
                    foreach ($cfg['faces'] as $v => $lbl) {
                        $sel = $pag == $v ? ' selected' : '';
                        $optPag .= "<option value=\"$v\"$sel>" . lang("Presupuestos.$lbl") . "</option>";
                    }

                    $cells = [
                        '<p>' . lang("PapelImpresion.$tipo") . '</p>',
                        "<select id=\"{$tipo}_paginas\" name=\"{$tipo}_paginas\" class=\"form-control select2bs2\" style=\"width:100%\"" . ($cfg['disable'] ? ' disabled' : '') . ">{$optPag}</select>",
                        "<select id=\"{$tipo}_papel_id\" name=\"{$tipo}_papel_id\" class=\"form-control select2bs2 {$tipo}_items\" style=\"width:100%\"" . ($cfg['disable'] ? ' disabled' : '') . ">"
                        . ($papNm ? "<option value=\"" . esc($papId) . "\" selected>" . esc($papNm) . "</option>" : "")
                        . "</select>",
                        "<select id=\"{$tipo}_gramaje\" name=\"{$tipo}_gramaje\" class=\"form-control select2bs2 {$tipo}_items\" style=\"width:100%\"" . ($cfg['disable'] ? ' disabled' : '') . ">"
                        . "<option value=\"" . esc($gram) . "\" selected>" . esc($gram) . "</option></select>",
                        "<input type=\"text\" id=\"{$tipo}_ancho_solapas\" name=\"{$tipo}_ancho_solapas\" placeholder=\"0\" maxlength=\"5\" class=\"form-control {$tipo}_items\" value=\"" . esc($sol) . "\"" . ($cfg['disable'] ? ' disabled' : '') . ">",
                        "<select id=\"{$tipo}_acabado_id\" name=\"{$tipo}_acabado_id\" class=\"form-control select2bs2 {$tipo}_items\" style=\"width:100%\"" . ($cfg['disable'] ? ' disabled' : '') . ">"
                        . "<option value=\"" . esc($acId) . "\" selected>" . esc($acNm) . "</option></select>",
                    ];
                    $renderRow($cells, $tipo);
                endforeach;
                ?>

            </div>
        </div>
    </div>
</div>