<?php include __DIR__ . '/../partials/sidebar_importador.php'; ?>

<!DOCTYPE html>
<html lang="es"> 
<head>
  <meta charset="UTF-8">
  <title>➕ Editar Transporte # <?= $t['id_transporte'] ?></title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <link href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
  <style>
    table.dataTable thead th { background:#343a40; color:#fff; }
    body { font-family: 'Segoe UI', sans-serif; background-color: #f4f6f9; }
    .sidebar { width: 220px; height: 100vh; background-color: #343a40; position: fixed; top: 0; left: 0; padding-top: 56px; z-index: 1040; }
    .sidebar .nav-link { font-weight: bold; color: white; transition: all 0.3s ease; }
    .sidebar .nav-link:hover,
    .sidebar .nav-link.active { background-color: #495057; color: #fff; }
    .content { margin-top: 56px; padding: 40px 20px; position: relative; z-index: 1; background-color: #f4f6f9; transition: margin-left 0.3s ease; }
    .navbar { position: fixed; top: 0; width: 100%; z-index: 1050; }
    /* A partir de dispositivos medianos (>=768px), deja espacio lateral */
    @media (min-width: 768px) { .content { margin-left: 250px; /* Ancho del sidebar */ } }
    /* En móviles, sin margen lateral */
    @media (max-width: 767.98px) {
      .content { margin-left: 0; }
      .sidebar .nav-link { font-weight: normal; color: #343a40; background-color: transparent; }
      .sidebar .nav-link:hover,
      .sidebar .nav-link.active { background-color: #e9ecef; color: #212529; }
    }
    .card { border-radius: 12px; }
    .preview{ width: 40%; }
  </style>
</head>
<body>
  
  <div class="content">
    <h4>✏️ Editar Transporte #<?= $t['id_transporte'] ?></h4>
    <form id="formTransEdit" action="/IMPORTADORES/transportes/actualizar" method="POST" enctype="multipart/form-data" class="card p-4 shadow-sm">
      <input type="hidden" name="id_transporte" value="<?= $t['id_transporte'] ?>">
      <div class="row g-3">
        <div class="col-md-6">
          <label class="form-label">Vehículo *</label>
          <input name="vehiculo" id="vehEdit" class="form-control"
                 value="<?= htmlspecialchars($t['vehiculo']) ?>" required>
        </div>
        <div class="col-md-6">
          <label class="form-label">Identificador fiscal *</label>
          <input name="identificador_fiscal" id="fiscEdit" class="form-control"
                 value="<?= htmlspecialchars($t['identificador_fiscal']) ?>" required>
        </div>
        <div class="col-md-6">
          <label class="form-label">Foto actual</label><br>
          <?php if($t['foto_url']): ?>
            <img src="<?= $t['foto_url'] ?>" class="preview">
          <?php else: ?>
            <span class="text-muted">Sin foto</span>
          <?php endif; ?>
        </div>
        <div class="col-md-6">
          <label class="form-label">Reemplazar foto (opcional)</label>
          <input type="file" name="foto" id="fotoEdit" class="form-control" accept="image/*">
        </div>
        <div class="col-md-6">
          <label class="form-label">Transportista *</label>
          <select name="id_transportista" id="trEdit" class="form-select" required>
            <?php foreach($transportistas as $tr): ?>
              <option value="<?= $tr['id_transportista'] ?>"
                <?= $tr['id_transportista']==$t['id_transportista']?'selected':'' ?>>
                <?= htmlspecialchars($tr['nombre']) ?>
              </option>
            <?php endforeach; ?>
          </select>
        </div>
      </div>
      <div class="text-end mt-4">
        <button type="submit" class="btn btn-primary">Actualizar</button>
      </div>
    </form>
  </div>

  <script>
    document.getElementById('formTransEdit').addEventListener('submit', function(e) {
      const veh    = document.getElementById('vehEdit').value.trim();
      const fisc   = document.getElementById('fiscEdit').value.trim();
      const trans  = document.getElementById('trEdit').value;
      const fotoF  = document.getElementById('fotoEdit').files[0];

      if (!veh) {
        e.preventDefault();
        return Swal.fire({ icon:'error', title:'Vehículo requerido', text:'Por favor ingresa el nombre del vehículo.' });
      }
      if (!fisc) {
        e.preventDefault();
        return Swal.fire({ icon:'error', title:'Identificador fiscal requerido', text:'Por favor ingresa el identificador fiscal.' });
      }
      if (!trans) {
        e.preventDefault();
        return Swal.fire({ icon:'error', title:'Transportista no seleccionado', text:'Debes elegir un transportista.' });
      }
      if (fotoF && fotoF.size > 2 * 1024 * 1024) {
        e.preventDefault();
        return Swal.fire({ icon:'error', title:'Foto demasiado grande', text:'La imagen no debe exceder 2 MB.' });
      }
      // Si todo OK, se envía
    });
  </script>

</body>
</html>