<!DOCTYPE html>
<html lang="es">
<head>
  <script>/*pos_theme_early*/(function(){var t=localStorage.getItem("pos_theme")||"industrial";document.documentElement.setAttribute("data-theme",t);})()</script>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Flotillas — Nexus Autoparts POS</title>
  <link rel="stylesheet" href="/pos/static/css/chat.css" />
  <link rel="stylesheet" href="/pos/static/css/tokens.css" />
  <link rel="stylesheet" href="/pos/static/css/common.css" />
  <link rel="stylesheet" href="/pos/static/css/pos-ui.css?v=33" />
  <link rel="stylesheet" href="/pos/static/css/sidebar.css" />
  <link rel="stylesheet" href="/pos/static/css/pos-glass.css" />
  <link rel="manifest" href="/pos/static/pwa/manifest.json" />
  <meta name="theme-color" content="#F5A623" />
  <link rel="shortcut icon" type="image/png" href="/pos/static/pwa/icon-192.png" />

  <link rel="stylesheet" href="/pos/static/css/fleet.css"></head>
<body>

<div class="page-shell">
  <!-- Sidebar injected by sidebar.js -->
  <aside id="sidebar"></aside>

  <div class="main-area pos-main-offset">
    <!-- Offline banner injected by offline-banner.js -->
    <div id="offlineBanner"></div>

    <div class="page-header">
      <h1>Flotillas</h1>
      <div class="page-header__actions">
        <input type="text" class="search-input" id="searchInput"
               placeholder="Buscar placa, marca, modelo..." />
        <button class="btn btn--primary" id="btnNewVehicle">+ Nuevo Vehiculo</button>
      </div>
    </div>

    <div class="page-content">
      <!-- Stats -->
      <div class="stats-row" id="statsRow">
        <div class="stat-card">
          <span class="stat-card__label">Vehiculos Activos</span>
          <span class="stat-card__value" id="statTotal">--</span>
        </div>
        <div class="stat-card">
          <span class="stat-card__label">Mant. Vencidos</span>
          <span class="stat-card__value stat-card__value--danger" id="statOverdue">--</span>
        </div>
        <div class="stat-card">
          <span class="stat-card__label">Prox. Este Mes</span>
          <span class="stat-card__value stat-card__value--warn" id="statUpcoming">--</span>
        </div>
        <div class="stat-card">
          <span class="stat-card__label">Costo Mes</span>
          <span class="stat-card__value" id="statCost">--</span>
        </div>
      </div>

      <!-- Tabs -->
      <div class="tabs">
        <button class="tab-btn is-active" data-tab="vehicles">Vehiculos</button>
        <button class="tab-btn" data-tab="maintenance">Mantenimiento</button>
        <button class="tab-btn" data-tab="history">Historial</button>
        <button class="tab-btn" data-tab="alerts">Alertas</button>
      </div>

      <!-- Tab: Vehicles -->
      <div class="tab-panel is-active" id="tab-vehicles">
        <div class="vehicle-grid" id="vehicleGrid">
          <div class="empty-state">
            <div class="empty-state__icon">&#x1F69A;</div>
            <div class="empty-state__text">Cargando vehiculos...</div>
          </div>
        </div>
        <div class="pagination" id="vehiclePagination"></div>
      </div>

      <!-- Tab: Maintenance -->
      <div class="tab-panel" id="tab-maintenance">
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:var(--space-4);">
          <h2 style="font-family:var(--font-heading);font-size:var(--text-h4);font-weight:var(--font-weight-bold);letter-spacing:var(--tracking-wide);text-transform:uppercase;">Programas de Mantenimiento</h2>
        </div>
        <div class="vs-container" style="max-height:60vh;overflow-y:auto;">
          <table class="data-table" id="maintTable">
            <thead>
              <tr>
                <th>Vehiculo</th>
                <th>Tipo</th>
                <th>Intervalo</th>
                <th>Ultimo</th>
                <th>Proximo</th>
                <th>Estado</th>
                <th></th>
              </tr>
            </thead>
            <tbody id="maintBody">
              <tr><td colspan="7" style="text-align:center;padding:var(--space-6);color:var(--color-text-muted);">Cargando...</td></tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- Tab: History -->
      <div class="tab-panel" id="tab-history">
        <div class="vs-container" style="max-height:60vh;overflow-y:auto;">
          <table class="data-table">
            <thead>
              <tr>
                <th>Fecha</th>
                <th>Vehiculo</th>
                <th>Tipo</th>
                <th>Km</th>
                <th>Costo</th>
                <th>Empleado</th>
                <th>Notas</th>
              </tr>
            </thead>
            <tbody id="historyBody">
              <tr><td colspan="7" style="text-align:center;padding:var(--space-6);color:var(--color-text-muted);">Cargando...</td></tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- Tab: Alerts -->
      <div class="tab-panel" id="tab-alerts">
        <div id="alertsList">
          <div class="empty-state">
            <div class="empty-state__text">Cargando alertas...</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- Modal: New/Edit Vehicle -->
<div class="modal-overlay" id="vehicleModal">
  <div class="modal">
    <div class="modal__header">
      <span class="modal__title" id="vehicleModalTitle">Nuevo Vehiculo</span>
      <button class="modal__close" onclick="Fleet.closeVehicleModal()">&times;</button>
    </div>
    <div class="modal__body">
      <input type="hidden" id="vehEditId" />
      <div class="form-row">
        <div class="form-group">
          <label>Placa</label>
          <input type="text" id="vehPlate" placeholder="ABC-123" maxlength="20" />
        </div>
        <div class="form-group">
          <label>VIN</label>
          <input type="text" id="vehVin" placeholder="17 caracteres" maxlength="17" />
        </div>
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>Marca</label>
          <input type="text" id="vehMake" placeholder="Toyota" />
        </div>
        <div class="form-group">
          <label>Modelo</label>
          <input type="text" id="vehModel" placeholder="Hilux" />
        </div>
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>Anio</label>
          <input type="number" id="vehYear" placeholder="2024" min="1900" max="2100" />
        </div>
        <div class="form-group">
          <label>Kilometraje</label>
          <input type="number" id="vehMileage" placeholder="0" min="0" />
        </div>
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>Combustible</label>
          <select id="vehFuel">
            <option value="gasolina">Gasolina</option>
            <option value="diesel">Diesel</option>
            <option value="gas">Gas LP</option>
            <option value="electrico">Electrico</option>
            <option value="hibrido">Hibrido</option>
          </select>
        </div>
        <div class="form-group">
          <label>Color</label>
          <input type="text" id="vehColor" placeholder="Blanco" />
        </div>
      </div>
      <div class="form-group">
        <label>Propietario</label>
        <input type="text" id="vehOwner" placeholder="Nombre del propietario" />
      </div>
      <div class="form-group">
        <label>Notas</label>
        <textarea id="vehNotes" rows="2" placeholder="Notas adicionales..."></textarea>
      </div>
    </div>
    <div class="modal__footer">
      <button class="btn btn--ghost" onclick="Fleet.closeVehicleModal()">Cancelar</button>
      <button class="btn btn--primary" id="btnSaveVehicle" onclick="Fleet.saveVehicle()">Guardar</button>
    </div>
  </div>
</div>

<!-- Modal: New Schedule -->
<div class="modal-overlay" id="scheduleModal">
  <div class="modal">
    <div class="modal__header">
      <span class="modal__title">Nuevo Programa de Mantenimiento</span>
      <button class="modal__close" onclick="Fleet.closeScheduleModal()">&times;</button>
    </div>
    <div class="modal__body">
      <input type="hidden" id="schedVehicleId" />
      <div class="form-group">
        <label>Vehiculo</label>
        <select id="schedVehicleSelect"></select>
      </div>
      <div class="form-group">
        <label>Tipo de Mantenimiento</label>
        <input type="text" id="schedType" placeholder="Cambio de aceite, Frenos, etc." />
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>Intervalo (km)</label>
          <input type="number" id="schedIntervalKm" placeholder="10000" min="0" />
        </div>
        <div class="form-group">
          <label>Intervalo (meses)</label>
          <input type="number" id="schedIntervalMonths" placeholder="6" min="0" />
        </div>
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>Proximo Vencimiento (fecha)</label>
          <input type="date" id="schedNextDate" />
        </div>
        <div class="form-group">
          <label>Proximo Vencimiento (km)</label>
          <input type="number" id="schedNextKm" placeholder="50000" min="0" />
        </div>
      </div>
      <div class="form-group">
        <label>Notas</label>
        <textarea id="schedNotes" rows="2" placeholder="Notas..."></textarea>
      </div>
    </div>
    <div class="modal__footer">
      <button class="btn btn--ghost" onclick="Fleet.closeScheduleModal()">Cancelar</button>
      <button class="btn btn--primary" onclick="Fleet.saveSchedule()">Guardar</button>
    </div>
  </div>
</div>

<!-- Modal: Record Maintenance -->
<div class="modal-overlay" id="logModal">
  <div class="modal">
    <div class="modal__header">
      <span class="modal__title">Registrar Mantenimiento</span>
      <button class="modal__close" onclick="Fleet.closeLogModal()">&times;</button>
    </div>
    <div class="modal__body">
      <div class="form-group">
        <label>Vehiculo</label>
        <select id="logVehicleSelect"></select>
      </div>
      <div class="form-group">
        <label>Programa (opcional)</label>
        <select id="logScheduleSelect">
          <option value="">-- Sin programa --</option>
        </select>
      </div>
      <div class="form-group">
        <label>Tipo de Mantenimiento</label>
        <input type="text" id="logType" placeholder="Cambio de aceite" />
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>Kilometraje Actual</label>
          <input type="number" id="logMileage" placeholder="50000" min="0" />
        </div>
        <div class="form-group">
          <label>Costo</label>
          <input type="number" id="logCost" placeholder="0.00" min="0" step="0.01" />
        </div>
      </div>
      <div class="form-group">
        <label>Refacciones Usadas</label>
        <textarea id="logParts" rows="2" placeholder="Filtro de aceite, aceite 5W-30..."></textarea>
      </div>
      <div class="form-group">
        <label>Notas</label>
        <textarea id="logNotes" rows="2" placeholder="Notas..."></textarea>
      </div>
    </div>
    <div class="modal__footer">
      <button class="btn btn--ghost" onclick="Fleet.closeLogModal()">Cancelar</button>
      <button class="btn btn--primary" onclick="Fleet.saveLog()">Registrar</button>
    </div>
  </div>
</div>

<script src="/pos/static/js/i18n.js" defer></script>
<script src="/pos/static/js/app-init.js" defer></script>
<script src="/pos/static/js/splash-loader.js?v=33" defer></script>
<script src="/pos/static/js/pos-utils.js?v=33" defer></script>
  <script src="/pos/static/js/sidebar.js" defer></script>
<script src="/pos/static/js/fleet.js" defer></script>
<script src="/pos/static/js/offline-banner.js" defer></script>
<script>if('serviceWorker' in navigator){navigator.serviceWorker.register('/pos/sw.js',{scope:'/pos/'});}</script>
  <script src="/pos/static/js/pwa-install.js" defer></script>

  <script src="/pos/static/js/chat.js" defer></script>
</body>
</html>
