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

<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <title>✏️ Editar Transportista</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>
  <style>
    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; }
  </style>
</head>
<body>

  <div class="content">
    <h4 class="mb-4">✏️ Editar Transportista #<?= $t['id_transportista'] ?></h4>
    <form action="/IMPORTADORES/transportistas/actualizar" method="POST" class="card p-4 shadow-sm">
      <input type="hidden" name="id_transportista" value="<?= $t['id_transportista'] ?>">
      <div class="row g-3">
        <div class="col-md-4">
          <label class="form-label">Clave</label>
          <input name="clave" value="<?= htmlspecialchars($t['clave_identificador']) ?>"
                 class="form-control" required>
        </div>
        <div class="col-md-4">
          <label class="form-label">Nombre</label>
          <input name="nombre" value="<?= htmlspecialchars($t['nombre']) ?>"
                 class="form-control" required>
        </div>
        <div class="col-md-4">
          <label class="form-label">RFC</label>
          <input name="rfc" value="<?= htmlspecialchars($t['rfc']) ?>"
                 class="form-control" required>
        </div>
        <div class="col-md-4">
          <label class="form-label">CURP</label>
          <input name="curp" value="<?= htmlspecialchars($t['curp']) ?>"
                 class="form-control">
        </div>
        <div class="col-md-4">
          <label class="form-label">Teléfono</label>
          <input name="telefono" value="<?= htmlspecialchars($t['telefono']) ?>"
                 class="form-control" required>
        </div>
        <div class="col-md-4">
          <label class="form-label">Código CAAT</label>
          <input name="caat" value="<?= htmlspecialchars($t['caat']) ?>"
                 class="form-control" required>
        </div>

        <div class="col-md-4">
          <label class="form-label">País</label>
          <select id="pais" name="pais" class="form-select" required>
            <option value="">Selecciona país</option>
            <?php foreach($paises as $p): ?>
            <option value="<?= $p['id_pais'] ?>"
              <?= $p['id_pais']==$t['pais']?'selected':'' ?>>
              <?= htmlspecialchars($p['nombre']) ?>
            </option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-4">
          <label class="form-label">Estado</label>
          <select id="estado" name="entidad" class="form-select" required>
            <option value="">Selecciona estado</option>
            <?php foreach($estados as $e): ?>
            <option value="<?= $e['id_estado'] ?>"
              <?= $e['id_estado']==$t['entidad_federativa']?'selected':'' ?>>
              <?= htmlspecialchars($e['nombre']) ?>
            </option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-4">
          <label class="form-label">Ciudad</label>
          <select id="ciudad" name="ciudad" class="form-select" required>
            <option value="">Selecciona ciudad</option>
            <?php foreach($ciudades as $c): ?>
            <option value="<?= $c['id_ciudad'] ?>"
              <?= $c['id_ciudad']==$t['ciudad']?'selected':'' ?>>
              <?= htmlspecialchars($c['nombre']) ?>
            </option>
            <?php endforeach; ?>
          </select>
        </div>

        <div class="col-md-12">
          <label class="form-label">Domicilio Completo</label>
          <textarea name="domicilio" class="form-control" rows="3" required><?= htmlspecialchars($t['domicilio']) ?></textarea>
        </div>
      </div>
      <div class="text-end mt-4">
        <button class="btn btn-success px-4">💾 Guardar Cambios</button>
      </div>
    </form>
  </div>

  <script>
    // Al cambiar país → recargar estados
    document.getElementById('pais').addEventListener('change', function(){
      const pid = this.value, selE = document.getElementById('estado');
      selE.innerHTML = '<option>Cargando…</option>'; selE.disabled = true;
      fetch(`/IMPORTADORES/transportistas/estados?pais=${pid}`)
        .then(r=>r.json())
        .then(list=>{
          selE.innerHTML = '<option value="">Selecciona estado</option>';
          list.forEach(e=> selE.add(new Option(e.nombre,e.id_estado)) );
          selE.disabled = false;
          // Dispara cambio para recargar ciudades
          selE.dispatchEvent(new Event('change'));
        });
    });

    // Al cambiar estado → recargar ciudades
    document.getElementById('estado').addEventListener('change', function(){
      const eid = this.value, selC = document.getElementById('ciudad');
      selC.innerHTML = '<option>Cargando…</option>'; selC.disabled = true;
      fetch(`/IMPORTADORES/transportistas/ciudades?estado=${eid}`)
        .then(r=>r.json())
        .then(list=>{
          selC.innerHTML = '<option value="">Selecciona ciudad</option>';
          list.forEach(c=> selC.add(new Option(c.nombre,c.id_ciudad)) );
          selC.disabled = false;
        });
    });
  </script>

</body>
</html>