<?php $__env->startSection('titulo','OT '.$orden->codigo); ?>
<?php $__env->startSection('contenido'); ?>
<div class="d-flex flex-wrap justify-content-between align-items-center mb-3">
  <h4><i class="fa fa-wrench"></i> <?php echo e($orden->codigo); ?>

    <?php if($orden->estado): ?><span class="estado-badge ms-2" style="background: <?php echo e($orden->estado->color); ?>;"><?php echo e($orden->estado->nombre); ?></span><?php endif; ?>
  </h4>
  <div>
    <a href="<?php echo e(route('ordenes.pdf',$orden)); ?>" target="_blank" class="btn btn-outline-danger"><i class="fa fa-file-pdf"></i> PDF</a>
    <div class="btn-group">
      <button class="btn btn-acento dropdown-toggle" data-bs-toggle="dropdown">
        <i class="fa fa-file-lines"></i> Informe Interno
      </button>
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="<?php echo e(route('ordenes.informe.pdf', $orden)); ?>" target="_blank">
          <i class="fa fa-file-pdf text-danger"></i> PDF</a></li>
        <li><a class="dropdown-item" href="<?php echo e(route('ordenes.informe.word', $orden)); ?>">
          <i class="fa fa-file-word text-primary"></i> Word (.docx)</a></li>
      </ul>
    </div>
    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('ordenes.editar')): ?><a href="<?php echo e(route('ordenes.edit',$orden)); ?>" class="btn btn-primario"><i class="fa fa-pen"></i> Editar</a><?php endif; ?>
  </div>
</div>

<div class="row g-3">
  <div class="col-lg-8">
    <div class="card mb-3"><div class="card-body">
      <div class="row">
        <div class="col-md-3"><strong>Clase:</strong> <?php echo e(ucfirst($orden->clase)); ?></div>
        <div class="col-md-3"><strong>Equipo:</strong> <?php echo e(optional($orden->equipo)->nombre); ?></div>
        <div class="col-md-3"><strong>Área:</strong> <?php echo e(optional($orden->equipo->area ?? null)->nombre); ?></div>
        <div class="col-md-3"><strong>Programada:</strong> <?php echo e($orden->fecha_programada?->format('d/m/Y')); ?></div>
        <div class="col-md-3"><strong>Inicio:</strong> <?php echo e($orden->fecha_inicio?->format('d/m/Y H:i')); ?></div>
        <div class="col-md-3"><strong>Cierre:</strong> <?php echo e($orden->fecha_cierre?->format('d/m/Y H:i')); ?></div>
        <div class="col-md-3"><strong>Responsable:</strong> <?php echo e(optional($orden->responsable)->nombre_completo); ?></div>
        <div class="col-md-3"><strong>Prioridad:</strong> <?php echo e(optional($orden->prioridad)->nombre); ?></div>
      </div>
      <?php if($orden->descripcion): ?><p class="mt-2"><strong>Descripción:</strong> <?php echo e($orden->descripcion); ?></p><?php endif; ?>
      <?php if($orden->observaciones): ?><p><strong>Observaciones:</strong> <?php echo e($orden->observaciones); ?></p><?php endif; ?>
    </div></div>

    <div class="card mb-3">
      <div class="card-header d-flex flex-wrap justify-content-between align-items-center">
        <strong><i class="fa fa-list-check"></i> Checklist
          <small class="text-muted ms-2">L=Limpieza · I=Inspección · Lu=Lubricación · A=Ajuste</small>
        </strong>
        <div class="d-flex gap-2">
          <?php if($orden->plan_id): ?>
            <form method="POST" action="<?php echo e(route('ordenes.poblar',$orden)); ?>" class="d-inline"
                  onsubmit="return confirm('¿Cargar todos los ítems desde el plan asociado?');">
              <?php echo csrf_field(); ?>
              <button class="btn btn-sm btn-outline-primary"><i class="fa fa-download"></i> Llenar desde plan</button>
            </form>
          <?php endif; ?>
          <button class="btn btn-sm btn-primario" data-bs-toggle="collapse" data-bs-target="#nuevoItemOt">
            <i class="fa fa-plus"></i> Agregar ítem
          </button>
        </div>
      </div>
      <div id="nuevoItemOt" class="collapse">
        <form method="POST" action="<?php echo e(route('ordenes.items.store',$orden)); ?>" class="card-body border-bottom">
          <?php echo csrf_field(); ?>
          <div class="row g-2">
            <div class="col-md-1"><label class="form-label small">N°</label>
              <input type="number" name="numero_item" class="form-control form-control-sm" placeholder="auto"></div>
            <div class="col-md-7"><label class="form-label small">Descripción *</label>
              <input name="descripcion" class="form-control form-control-sm" required maxlength="500"></div>
            <div class="col-md-4"><label class="form-label small">Lectura / condición</label>
              <input name="lectura_condicion" class="form-control form-control-sm" maxlength="100"></div>
            <div class="col-12">
              <div class="d-flex flex-wrap gap-3 mt-2">
                <?php $__currentLoopData = [['lila_l_limpieza','L (limpieza)'],['lila_i_inspeccion','I (inspección)'],
                          ['lila_l_lubricacion','Lu (lubricación)'],['lila_a_ajuste','A (ajuste)']]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$c,$lbl]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                  <div class="form-check">
                    <input type="hidden" name="<?php echo e($c); ?>" value="0">
                    <input type="checkbox" name="<?php echo e($c); ?>" value="1" id="ni_<?php echo e($c); ?>" class="form-check-input">
                    <label for="ni_<?php echo e($c); ?>" class="form-check-label small"><?php echo e($lbl); ?></label>
                  </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
              </div>
            </div>
          </div>
          <div class="text-end mt-2"><button class="btn btn-sm btn-primario"><i class="fa fa-save"></i> Agregar</button></div>
        </form>
      </div>
      <div class="table-responsive"><table class="table table-sm mb-0 align-middle">
        <thead><tr><th>#</th><th>Descripción</th><th>L</th><th>I</th><th>Lu</th><th>A</th><th>Estado</th><th>Lectura</th><th class="text-end"></th></tr></thead>
        <tbody>
        <?php $__empty_1 = true; $__currentLoopData = $orden->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $it): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
          <tr>
            <form method="POST" action="<?php echo e(route('ordenes.items.update',[$orden,$it])); ?>"><?php echo csrf_field(); ?> <?php echo method_field('PUT'); ?>
              <td><?php echo e($it->numero_item); ?></td>
              <td><?php echo e(\Illuminate\Support\Str::limit($it->descripcion,80)); ?></td>
              <?php $__currentLoopData = ['lila_l_limpieza','lila_i_inspeccion','lila_l_lubricacion','lila_a_ajuste']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $f): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <td class="text-center"><input type="hidden" name="<?php echo e($f); ?>" value="0"><input type="checkbox" name="<?php echo e($f); ?>" value="1" <?php echo e($it->$f?'checked':''); ?>></td>
              <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
              <td><select name="estado_item" class="form-select form-select-sm" data-no-search>
                <option value="pendiente" <?php if($it->estado_item==='pendiente'): echo 'selected'; endif; ?>>Pendiente</option>
                <option value="realizado" <?php if($it->estado_item==='realizado'): echo 'selected'; endif; ?>>Realizado</option>
                <option value="no_aplica" <?php if($it->estado_item==='no_aplica'): echo 'selected'; endif; ?>>No aplica</option>
                <option value="no_realizado" <?php if($it->estado_item==='no_realizado'): echo 'selected'; endif; ?>>No realizado</option>
              </select></td>
              <td><input name="lectura_condicion" value="<?php echo e($it->lectura_condicion); ?>" class="form-control form-control-sm"></td>
              <td class="text-end text-nowrap">
                <button class="btn btn-sm btn-outline-success" title="Guardar"><i class="fa fa-check"></i></button>
            </form>
            <form method="POST" action="<?php echo e(route('ordenes.items.destroy',[$orden,$it])); ?>" class="d-inline"
                  onsubmit="return confirm('¿Eliminar este ítem?');">
              <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
              <button class="btn btn-sm btn-outline-danger" title="Eliminar"><i class="fa fa-trash"></i></button>
            </form>
              </td>
          </tr>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
          <tr><td colspan="9" class="text-muted text-center py-3">
            Sin ítems.
            <?php if($orden->plan_id): ?>
              Usa <strong>Llenar desde plan</strong> arriba para cargarlos automáticamente.
            <?php else: ?>
              Usa <strong>Agregar ítem</strong> arriba para crearlos manualmente.
            <?php endif; ?>
          </td></tr>
        <?php endif; ?>
        </tbody>
      </table></div>
    </div>

    <div class="card mb-3">
      <div class="card-header d-flex justify-content-between"><strong><i class="fa fa-camera"></i> Evidencias</strong></div>
      <div class="card-body">
        <form method="POST" action="<?php echo e(route('ordenes.fotos.store',$orden)); ?>" enctype="multipart/form-data" class="row g-2 mb-3"><?php echo csrf_field(); ?>
          <div class="col-md-4"><input type="file" name="foto" accept="image/*" capture="environment" class="form-control" required></div>
          <div class="col-md-3"><select name="tipo" class="form-select">
            <option value="evidencia">Evidencia</option><option value="antes">Antes</option><option value="despues">Después</option>
            <option value="placa">Placa</option><option value="manual">Manual</option><option value="otro">Otro</option>
          </select></div>
          <div class="col-md-3"><input name="descripcion" class="form-control" placeholder="Descripción..."></div>
          <div class="col-md-2"><button class="btn btn-primario w-100"><i class="fa fa-upload"></i> Subir</button></div>
        </form>
        <div class="row g-2">
          <?php $__empty_1 = true; $__currentLoopData = $orden->fotos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $f): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
            <div class="col-6 col-md-3"><div class="card">
              <a href="<?php echo e(asset('storage/'.$f->ruta)); ?>" target="_blank">
                <img src="<?php echo e(asset('storage/'.$f->ruta)); ?>" class="card-img-top" style="height:120px; object-fit:cover;">
              </a>
              <div class="card-body p-2">
                <span class="badge text-bg-secondary"><?php echo e($f->tipo); ?></span>
                <small class="d-block"><?php echo e($f->descripcion); ?></small>
                <form method="POST" action="<?php echo e(route('ordenes.fotos.destroy',[$orden,$f])); ?>" onsubmit="return confirm('¿Eliminar?');">
                  <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?><button class="btn btn-sm btn-outline-danger mt-1"><i class="fa fa-trash"></i></button>
                </form>
              </div>
            </div></div>
          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?> <div class="col-12 text-muted">Sin fotos.</div>
          <?php endif; ?>
        </div>
      </div>
    </div>

    <div class="card mb-3">
      <div class="card-header"><strong><i class="fa fa-dollar-sign"></i> Costos</strong></div>
      <div class="card-body">
        <form method="POST" action="<?php echo e(route('ordenes.costos.store',$orden)); ?>" class="row g-2 mb-3"><?php echo csrf_field(); ?>
          <div class="col-md-2"><select name="tipo" class="form-select">
            <option value="repuesto">Repuesto</option><option value="mano_obra">Mano de obra</option><option value="otro">Otro</option>
          </select></div>
          <div class="col-md-3"><select name="repuesto_id" class="form-select"><option value="">— Repuesto —</option>
            <?php $__currentLoopData = $repuestos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $r): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($r->id); ?>"><?php echo e($r->codigo); ?> - <?php echo e($r->nombre); ?> (<?php echo e($r->stock); ?>)</option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
          </select></div>
          <div class="col-md-3"><input name="concepto" class="form-control" placeholder="Concepto" required></div>
          <div class="col-md-1"><input name="cantidad" type="number" step="0.001" class="form-control" placeholder="Cant" required></div>
          <div class="col-md-2"><input name="costo_unitario" type="number" step="0.01" class="form-control" placeholder="C. unit." required></div>
          <div class="col-md-1"><button class="btn btn-primario w-100"><i class="fa fa-plus"></i></button></div>
        </form>
        <table class="table table-sm">
          <thead><tr><th>Concepto</th><th>Tipo</th><th class="text-end">Cantidad</th><th class="text-end">C. unit.</th><th class="text-end">Total</th><th></th></tr></thead>
          <tbody>
            <?php $__currentLoopData = $orden->costos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <tr><td><?php echo e($c->concepto); ?></td><td><?php echo e($c->tipo); ?></td>
                <td class="text-end"><?php echo e($c->cantidad); ?></td>
                <td class="text-end"><?php echo e(number_format($c->costo_unitario,2)); ?></td>
                <td class="text-end"><?php echo e(number_format($c->costo_total,2)); ?></td>
                <td><form method="POST" action="<?php echo e(route('ordenes.costos.destroy',[$orden,$c])); ?>" onsubmit="return confirm('¿Eliminar?');">
                  <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?><button class="btn btn-sm btn-outline-danger"><i class="fa fa-trash"></i></button></form></td>
              </tr>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
          </tbody>
          <tfoot><tr class="table-light fw-bold"><td colspan="4" class="text-end">Total</td>
            <td class="text-end"><?php echo e(number_format($orden->costo_total,2)); ?></td><td></td></tr></tfoot>
        </table>
      </div>
    </div>
  </div>

  <div class="col-lg-4">
    <div class="card mb-3"><div class="card-header"><strong>Cambiar estado</strong></div>
      <form method="POST" action="<?php echo e(route('ordenes.estado',$orden)); ?>" class="card-body"><?php echo csrf_field(); ?>
        <div class="mb-2"><select name="estado_id" class="form-select">
          <?php $__currentLoopData = $estados; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $e): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($e->id); ?>" <?php if($orden->estado_id==$e->id): echo 'selected'; endif; ?>><?php echo e($e->nombre); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select></div>
        <div class="mb-2"><textarea name="comentario" class="form-control" rows="2" placeholder="Comentario..."></textarea></div>
        <button class="btn btn-primario w-100"><i class="fa fa-arrow-right"></i> Cambiar</button>
      </form>
    </div>

    <div class="card mb-3"><div class="card-header"><strong>Firma con clave personal</strong></div>
      <form method="POST" action="<?php echo e(route('ordenes.firmar',$orden)); ?>" class="card-body"><?php echo csrf_field(); ?>
        <div class="mb-2"><select name="tipo" class="form-select">
          <option value="responsable">Responsable</option><option value="supervisor">Supervisor</option>
        </select></div>
        <div class="mb-2"><input type="password" name="clave_firma" class="form-control" placeholder="Clave de firma" required></div>
        <button class="btn btn-acento w-100"><i class="fa fa-pen-nib"></i> Firmar</button>
      </form>
      <?php
        $renderFirmaWeb = function ($valor) {
            if (!$valor) return '';
            if (str_starts_with($valor, 'IMG:')) {
                [$ruta, $nombre, $fecha] = array_pad(explode('|', substr($valor, 4)), 3, '');
                return '<img src="' . asset('storage/'.$ruta) . '" style="max-height:50px; max-width:180px;"><br>'
                     . '<small class="text-muted">' . e($nombre) . ' — ' . e($fecha) . '</small>';
            }
            if (str_starts_with($valor, 'TXT:')) {
                [$nombre, $fecha] = array_pad(explode('|', substr($valor, 4)), 2, '');
                return '<small>' . e($nombre . ' — ' . $fecha) . '</small>';
            }
            return '<small>' . e($valor) . '</small>';
        };
      ?>
      <?php if($orden->firma_responsable): ?><div class="card-body small text-success"><strong>Responsable:</strong><br><?php echo $renderFirmaWeb($orden->firma_responsable); ?></div><?php endif; ?>
      <?php if($orden->firma_supervisor): ?><div class="card-body small text-success border-top"><strong>Supervisor:</strong><br><?php echo $renderFirmaWeb($orden->firma_supervisor); ?></div><?php endif; ?>
    </div>

    <div class="card"><div class="card-header"><strong>Bitácora</strong></div>
      <ul class="list-group list-group-flush" style="max-height:400px; overflow-y:auto;">
        <?php $__empty_1 = true; $__currentLoopData = $orden->logs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $l): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
          <li class="list-group-item small">
            <strong><?php echo e($l->accion); ?></strong>
            <?php if($l->estado_anterior || $l->estado_nuevo): ?><span class="text-muted">[<?php echo e($l->estado_anterior); ?> → <?php echo e($l->estado_nuevo); ?>]</span><?php endif; ?>
            <br><small class="text-muted"><?php echo e(optional($l->usuario)->usuario); ?> — <?php echo e($l->created_at->format('d/m H:i')); ?></small>
            <?php if($l->detalle): ?><br><em><?php echo e($l->detalle); ?></em><?php endif; ?>
          </li>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?> <li class="list-group-item text-muted">Sin actividad</li>
        <?php endif; ?>
      </ul>
    </div>
  </div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\mto\resources\views/ordenes/show.blade.php ENDPATH**/ ?>