<section x-show="step === 2" x-transition x-ref="step2" x-data="{
    rows: <?php echo \Illuminate\Support\Js::from($dispoPlantilla)->toHtml() ?>,

    _num(val) { const n = parseFloat(val); return isNaN(n) ? 0 : n; },
    totalPonderado() {
      return this.rows.reduce((acc, r) => acc + this._num(r.ponderado ?? 3.3), 0);
    },
    cumplidoPonderado() {
      return this.rows.reduce((acc, r) => acc + (r.cumple === 'SI' ? this._num(r.ponderado ?? 3.3) : 0), 0);
    },
    porcentaje() {
      const total = this.totalPonderado();
      if (!total) return 0;
      return (this.cumplidoPonderado() * 100) / total;
    },
    fmt(n) { n = Number(n); return Number.isInteger(n) ? n : n.toFixed(1); },

    // Actualiza hidden y store global para el resumen final
    recalcular() {
      const total = this.totalPonderado();
      const obt = this.cumplidoPonderado();
      const pct = this.porcentaje();

      if (this.$refs.total) this.$refs.total.value = total.toFixed(2);
      if (this.$refs.obt) this.$refs.obt.value   = obt.toFixed(2);
      if (this.$refs.pct) this.$refs.pct.value   = pct.toFixed(1);

      if (window.Alpine?.store('resumen')) {
  Alpine.store('resumen').disposiciones         = pct;
  Alpine.store('resumen').disposicionesTotal    = total;
  Alpine.store('resumen').disposicionesObtenido = obt;
}
window.dispatchEvent(new CustomEvent('resumen-updated')); // 👈 nuevo

    },
    siguiente() {
      // fuerza recálculo y luego validación del stepper
      this.recalcular();
      const parent = this.$root?.__x?.$data;
      if (parent && typeof parent.next === 'function') parent.next();
    },
    back() {
      const parent = this.$root?.__x?.$data;
      if (parent && typeof parent.prev === 'function') parent.prev();
    }
  }" x-init="recalcular()" @sync-resumen.window="recalcular()" class="sec sec-azul">

  <h2 class="sec-title flex items-center gap-2">
    DISPOSICIONES GENERALES DEL CONTRATO
    <span class="badge badge-required">* Obligatorio</span>
  </h2>

  <div class="overflow-x-auto border rounded-lg">
    <h3 class="sec-title flex items-center gap-2">
      AUDITORIA NACIONAL - SUB PROCESO AUDITORIA INTERNA
    </h3>
    <table class="min-w-full text-sm table-dispos">
      <thead>
        <tr>
          <th class="p-2 w-12 border-b text-center">No.</th>
          <th class="p-2 border-b">ITEM EVALUADO</th>
          <th class="p-2 border-b text-center">PONDERADO</th>
          <th class="p-2 w-40 border-b text-center">CUMPLE</th>
          <th class="p-2 w-[40%] border-b">OBSERVACIONES</th>
        </tr>
      </thead>

      <tbody>
        <template x-for="(row, i) in rows" :key="i">
          <tr>
            <td class="p-2 text-center whitespace-pre-line font-medium text-blue-900" x-text="i + 1"></td>

            <td class="p-2 border-b">
              <div class="whitespace-pre-line font-medium text-gray-800" x-text="row.item"></div>
              <input type="hidden" :name="`disposiciones[items][${i}][item]`" :value="row.item">
            </td>

            <td class="p-2 border-b text-center">
              <span class="whitespace-pre-line font-medium text-gray-800" x-text="(row.ponderado ?? 3.3)"></span>
              <input type="hidden" :name="`disposiciones[items][${i}][ponderado]`" :value="row.ponderado ?? 3.3">
            </td>

            <td class="p-2 border-b">
              <div
                class="flex items-center justify-center gap-4 radio-sino whitespace-pre-line font-medium text-blue-900">
                <label class="inline-flex items-center gap-1">
                  <input type="radio" :name="`disposiciones[items][${i}][cumple]`" value="SI" x-model="row.cumple"
                    @change="recalcular()" required>
                  <span>SI</span>
                </label>
                <label class="inline-flex items-center gap-1">
                  <input type="radio" :name="`disposiciones[items][${i}][cumple]`" value="NO" x-model="row.cumple"
                    @change="recalcular()" required>
                  <span>NO</span>
                </label>
              </div>
            </td>

            <td class="p-2 border-b">
              <textarea class="w-full border p-2" rows="3" :name="`disposiciones[items][${i}][observaciones]`"
                x-model="row.observaciones" :required="row.cumple === 'NO'"
                @input.debounce.300="recalcular()"></textarea>
            </td>
          </tr>
        </template>
      </tbody>
      <tfoot>
        <tr>
          <td class="p-2 border-t font-semibold text-right" colspan="2">Totales</td>
          <td class="p-2 border-t font-semibold text-center" x-text="fmt(totalPonderado())"></td>
          <td class="p-2 border-t font-semibold text-center" colspan="2">
            Obtenido: <span x-text="fmt(cumplidoPonderado())"></span> /
            <span x-text="fmt(totalPonderado())"></span>
            — <strong x-text="porcentaje().toFixed(1) + '%'"></strong>
          </td>
        </tr>
      </tfoot>
    </table>
  </div>


  
  <input type="hidden" x-ref="total" name="disposiciones[total_ponderado]" :value="totalPonderado().toFixed(2)">
  <input type="hidden" x-ref="obt" name="disposiciones[obtenido_ponderado]" :value="cumplidoPonderado().toFixed(2)">
  <input type="hidden" x-ref="pct" name="disposiciones[porcentaje_cumplimiento]" :value="porcentaje().toFixed(1)">

  
  <?php if (isset($component)) { $__componentOriginal6d5aacf0ef396ae7f3d0d05573c46c0d = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal6d5aacf0ef396ae7f3d0d05573c46c0d = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.upload-descripcion','data' => ['title' => 'Servicios contratados','name' => 'servicios','class' => 'mt-6']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('upload-descripcion'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['title' => 'Servicios contratados','name' => 'servicios','class' => 'mt-6']); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal6d5aacf0ef396ae7f3d0d05573c46c0d)): ?>
<?php $attributes = $__attributesOriginal6d5aacf0ef396ae7f3d0d05573c46c0d; ?>
<?php unset($__attributesOriginal6d5aacf0ef396ae7f3d0d05573c46c0d); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal6d5aacf0ef396ae7f3d0d05573c46c0d)): ?>
<?php $component = $__componentOriginal6d5aacf0ef396ae7f3d0d05573c46c0d; ?>
<?php unset($__componentOriginal6d5aacf0ef396ae7f3d0d05573c46c0d); ?>
<?php endif; ?>
  <?php if (isset($component)) { $__componentOriginal6d5aacf0ef396ae7f3d0d05573c46c0d = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal6d5aacf0ef396ae7f3d0d05573c46c0d = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.upload-descripcion','data' => ['title' => 'REGISTRO ESPECIAL DE PRESTADORES DE SALUD','name' => 'registro_especial','class' => 'mt-6']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('upload-descripcion'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['title' => 'REGISTRO ESPECIAL DE PRESTADORES DE SALUD','name' => 'registro_especial','class' => 'mt-6']); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal6d5aacf0ef396ae7f3d0d05573c46c0d)): ?>
<?php $attributes = $__attributesOriginal6d5aacf0ef396ae7f3d0d05573c46c0d; ?>
<?php unset($__attributesOriginal6d5aacf0ef396ae7f3d0d05573c46c0d); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal6d5aacf0ef396ae7f3d0d05573c46c0d)): ?>
<?php $component = $__componentOriginal6d5aacf0ef396ae7f3d0d05573c46c0d; ?>
<?php unset($__componentOriginal6d5aacf0ef396ae7f3d0d05573c46c0d); ?>
<?php endif; ?>

</section><?php /**PATH C:\xampp\htdocs\Auditoria-app\resources\views/formularios/sections/_disposiciones.blade.php ENDPATH**/ ?>