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

<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <title>📂 Importación Masiva de Transportes</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>
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></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>📂 Importación Masiva de Transportes</h4>
    <?php if (isset($_SESSION['import_result'])):
      $res = $_SESSION['import_result'];
      unset($_SESSION['import_result']);
    ?>
      <script>
        Swal.fire({
          title: 'Importación completada',
          html:
            '✅ Registros importados: <?= $res['imported'] ?>.<br>' +
            '<?php foreach ($res['errors'] as $err) {
              echo htmlspecialchars($err) . '<br>';
            } ?>',
          icon: 'info',
          width: 600
        });
      </script>
    <?php endif; ?>

    <div class="card p-4 shadow-sm">
      <form action="/IMPORTADORES/transportes/importarGuardar" method="POST" enctype="multipart/form-data">
        <div class="mb-3">
          <label class="form-label">Archivo CSV *</label>
          <input type="file" name="csv" accept=".csv" class="form-control" required>
        </div>
        <p>Descarga la plantilla y llena las columnas: <code>contenedor, identificador_fiscal, id_transportista</code>.</p>
        <a href="/IMPORTADORES/public/downloads/transportes_masivo_template.csv" class="btn btn-outline-secondary mb-3">
          📥 Descargar plantilla
        </a><br>
        <button type="submit" class="btn btn-primary">Importar</button>
      </form>
    </div>
  </div>

  <script>
    document.addEventListener('DOMContentLoaded', function() {
      const Toast = Swal.mixin({
        toast: true,
        position: 'top-end',
        showConfirmButton: false,
        timer: 3000,
        timerProgressBar: true
      });

      let importados = <?= json_encode($imported ?? 0) ?>;
      let errores = <?= json_encode($errors ?? []) ?>;

      if (importados > 0) {
        Toast.fire({
          icon: 'success',
          title: `✅ Importados: ${importados}`
        });
      }

      errores.forEach(err => {
        Toast.fire({
          icon: 'error',
          title: err
        });
      });
    });
  </script>
  
</body>
</html>