<!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" />
  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
  <meta http-equiv="Pragma" content="no-cache" />
  <meta http-equiv="Expires" content="0" />
  <title>WhatsApp — 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/whatsapp.css"></head>
<body>

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

  <div class="main-content pos-main-offset">
    <!-- Top bar -->
    <div class="top-bar">
      <div class="top-bar__title">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/>
        </svg>
        WhatsApp Business
      </div>
      <div class="top-bar__actions">
        <div class="top-bar__status">
          <span class="status-dot" id="statusDot"></span>
          <span id="statusText">Verificando...</span>
        </div>
        <button class="btn btn--sm btn--whatsapp" id="connectBtn" style="display:none">Conectar WhatsApp</button>
        <button class="btn btn--sm" id="disconnectBtn" style="display:none">Desconectar</button>
      </div>
    </div>

    <!-- Connection / QR section (shown when not connected) -->
    <div class="connect-section" id="connectSection" style="display:none">
      <svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="#25D366" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
        <path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/>
      </svg>
      <div class="connect-section__title">Conectar WhatsApp</div>
      <div class="connect-section__desc">
        Escanea el codigo QR con tu telefono para vincular WhatsApp.<br>
        Abre WhatsApp &gt; Dispositivos vinculados &gt; Vincular un dispositivo.
      </div>
      <div class="connect-section__qr">
        <img id="qrImg" style="display:none" alt="QR Code" />
        <span class="connect-section__qr-placeholder" id="qrPlaceholder">
          Haz click en "Conectar WhatsApp" para generar el QR
        </span>
      </div>
      <div class="connect-section__actions">
        <button class="btn btn--sm" id="refreshQrBtn" style="display:none">Actualizar QR</button>
      </div>
    </div>

    <!-- Messenger (shown when connected) -->
    <div class="messenger" id="messengerArea" style="display:none">

      <!-- Left: Conversation list -->
      <div class="conv-panel">
        <div class="conv-panel__header">
          <span class="conv-panel__title">Conversaciones</span>
          <button class="btn btn--sm btn--whatsapp" id="newChatBtn" title="Nueva conversacion">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/>
            </svg>
            Nuevo
          </button>
        </div>
        <div class="conv-panel__list" id="convList">
          <div class="conv-empty">Cargando...</div>
        </div>
      </div>

      <!-- Right: Chat view (hidden until a conversation is selected) -->
      <div class="empty-state" id="emptyState">
        <svg width="80" height="80" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round">
          <path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/>
        </svg>
        <div class="empty-state__text">Selecciona una conversacion<br>o inicia una nueva</div>
        <div class="empty-state__hint">Los mensajes de WhatsApp aparecen aqui en tiempo real</div>
      </div>

      <div class="wa-chat-panel" id="waChatPanel" style="display:none">
        <div class="wa-chat-panel__header">
          <button class="btn btn--sm" id="backToListBtn" style="display:none;margin-right:8px;">&larr; Volver</button>
          <span class="wa-chat-panel__phone" id="chatHeaderPhone"></span>
          <div class="wa-chat-panel__actions">
            <button class="btn btn--sm" id="sendQuoteBtn" title="Enviar cotizacion por WhatsApp">
              <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/>
              </svg>
              Enviar Cotizacion
            </button>
          </div>
        </div>

        <div class="wa-chat-panel__messages" id="waChatMessages"></div>

        <div class="chat-input-bar">
          <textarea id="waChatInput" placeholder="Escribe un mensaje..." rows="1"></textarea>
          <button class="btn btn--primary" id="waSendBtn">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/>
            </svg>
            Enviar
          </button>
        </div>
      </div>

    </div>
  </div>
</div>

<!-- Theme engine -->
<script>
function posSetTheme(t){document.documentElement.setAttribute('data-theme',t);localStorage.setItem('pos_theme',t);}
function posLogout(){localStorage.removeItem('pos_token');window.location.href='/pos/login';}
</script>

<!-- Sidebar -->
<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/whatsapp2.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/chat.js" defer></script>
</body>
</html>
