{#
  _preview_rand.html — un singur rand de preview import.
  US-006 (PRD 5.12): editarea inline (tr.preview-edit + mutual-exclusion script)
  a fost eliminata. Butonul Editeaza deschide MODALUL global (#detaliu-modal-body).

  Parametri:
    editing      — ELIMINAT (ignorat, pastrat pentru compatibilitate apeluri vechi)
    include_oob  — True: randeaza OOB rezumat + contor + script recalc (swap dupa save)
    oob_tr       — True: adauga hx-swap-oob pe <tr> insusi (pentru raspunsul POST succes)
    summary      — dict cu contoarele per status

  Campuri pre-computate de _web_compute_preview (NOT din template raw):
    row.prez           — prezentare_din_payload(resolved): vehicul_nr, vin_scurt,
                          operatie, cod_rar, data_prestatie, odometru
    row.stare_eticheta — text uman (ex. "Gata de trimis"), din STARI_PREVIEW
    row.stare_css      — clasa CSS (ex. "s-ok"), din STARI_PREVIEW
    row.nota_umana     — mesaj uman formatat pentru coloana Note (fara repr Python)
#}
{%- set res = row.resolved -%}
{%- set status = row.resolved_status -%}
{%- set _sent_dup = status in ('already_sent', 'duplicate_in_file') -%}
<tr id="preview-row-{{ row.row_index }}" data-status="{{ status }}"
    {% if _sent_dup %}class="preview-sent-row"{% endif %}
    {% if oob_tr %}hx-swap-oob="outerHTML:#preview-row-{{ row.row_index }}"{% endif %}
    style="{% if status == 'needs_review' %}background:rgba(230,179,74,.04);{% elif _sent_dup %}opacity:.6;{% endif %}">
  {#- Motivul (validare / deja-trimis / duplicat) — fost coloana Note, acum tooltip pe pill.
      KM final iese din tabel (se editeaza in modal). -#}
  {%- if status == 'already_sent' and row.get('already_sent_info') -%}
    {%- set ai = row.already_sent_info -%}
    {%- set _nota = 'deja trimis ' ~ ((ai.get('created_at') or '')[:10]) ~ ((' (#' ~ ai.id_prezentare ~ ')') if ai.get('id_prezentare') else '') -%}
  {%- elif status == 'duplicate_in_file' and row.get('duplicate_with') -%}
    {%- set _dwith = [] -%}
    {%- for idx in row.duplicate_with -%}{{ _dwith.append(idx + 1) or '' }}{%- endfor -%}
    {%- set _nota = 'dubla cu randul ' ~ (_dwith | join(', ')) -%}
  {%- else -%}
    {%- set _nota = row.nota_umana or '' -%}
  {%- endif -%}
  <td class="col-stare" data-eticheta="Stare">
    <span class="pill {{ row.stare_css }}" style="display:inline-flex; align-items:center; gap:5px;"
          {% if _nota %}title="{{ _nota }}"{% endif %}>
      <span aria-hidden="true" style="display:inline-block; width:7px; height:7px; border-radius:99px; background:currentColor; flex-shrink:0;"></span>{{ row.stare_eticheta }}</span>
    {# Badge mediu RAR (US-010 PRD 5.20) — rar_env disponibil din contextul batch-ului. #}
    {% if rar_env | default('') %}
    {% set _eb = eticheta_env(rar_env) %}
    <div style="margin-top:3px;"><span class="{{ _eb[1] }}">{{ _eb[0] }}</span></div>
    {% endif %}
  </td>
  <td class="col-vehicul" data-eticheta="Vehicul">
    {{ row.prez.vehicul_nr }}
    {% if row.prez.vin_scurt and row.prez.vin_scurt != '—' %}
    <div class="muted" style="font-size:12px; white-space:nowrap;">{{ row.prez.vin_scurt }}</div>
    {% endif %}
  </td>
  <td class="col-operatie" data-eticheta="Operatie">
    <div>{{ row.prez.operatie }}</div>
    {% if row.prez.cod_rar and row.prez.cod_rar != '—' %}
    <div class="cod-rar-sub"><span class="cod-rar-cod">{{ row.prez.cod_rar }}</span></div>
    {% else %}
    <div class="muted cod-rar-sub">nemapat</div>
    {% endif %}
  </td>
  <td class="col-data" data-eticheta="Data prestatie">{{ row.prez.data_prestatie }}</td>
  <td class="col-actiuni" data-eticheta="Actiuni" style="text-align:center;">
    {% if status not in ('already_sent', 'duplicate_in_file') %}
    <div style="display:inline-flex; gap:6px; flex-wrap:wrap; justify-content:center;">
      {# B1: confirm rapid per-rand direct din tabel (fara a deschide modalul).
         Refoloseste ruta /confirma-review (reviewed=1 pe un singur rand). hx-swap="none":
         raspunsul (empty div) NU se insereaza nicaieri; HX-Trigger reincarcaPreview
         reincarca sectiunea (contoare + banner corecte). Editarea reseteaza reviewed=0. #}
      {% if status == 'needs_review' %}
      <button type="button" class="btn-confirma-rapid"
              style="min-height:36px; padding:6px 14px; font-size:13px;
                     background:var(--ok, #2a7); color:#fff; border-color:transparent;"
              hx-post="/_import/{{ import_id }}/rand/{{ row.row_index }}/confirma-review"
              hx-vals='{"csrf_token": "{{ csrf_token or '' }}"}'
              hx-swap="none" hx-disabled-elt="this"
              aria-label="Confirma valorile randului {{ row.row_index + 1 }} (VIN: {{ res.get('vin', '') }})">
        Confirma valorile
      </button>
      {% endif %}
      <button type="button" class="btn-editeaza"
              style="min-height:36px; padding:6px 14px; font-size:13px;
                     background:transparent; border-color:var(--line); color:var(--ink);"
              hx-get="/_import/{{ import_id }}/rand/{{ row.row_index }}/editare-modal"
              hx-target="#detaliu-modal-body" hx-swap="innerHTML"
              aria-label="Editeaza randul {{ row.row_index + 1 }} (VIN: {{ res.get('vin', '') }})">
        Editeaza
      </button>
    </div>
    {% endif %}
  </td>
</tr>
{% if include_oob %}
{# OOB: actualizeaza rezumatul, contorul, bannerul needs_review dupa save/confirma-review. #}
{% set status_labels = [
  ('ok','Gata de trimis'), ('needs_review','Verifica valori'), ('needs_mapping','Cod RAR lipsa'),
  ('needs_data','Date incomplete'), ('already_sent','Deja trimis'), ('duplicate_in_file','Duplicat in fisier')] %}
<div id="preview-rezumat" hx-swap-oob="true"
     style="display:flex; gap:8px; flex-wrap:wrap; margin-bottom:12px;">
  {% for status_key, label in status_labels %}
  {%- set cnt = summary.get(status_key, 0) -%}
  {% if cnt > 0 %}<span class="pill s-{{ status_key }}" style="display:inline-flex; align-items:center; gap:5px; font-size:var(--fs-xs);"><span aria-hidden="true" style="display:inline-block; width:7px; height:7px; border-radius:99px; background:currentColor; flex-shrink:0;"></span>{{ cnt }} {{ label | lower }}</span>{% endif %}
  {% endfor %}
</div>
<span id="preview-ok-count" hx-swap-oob="true" data-ok="{{ summary.get('ok', 0) }}" hidden></span>
{# Banner discoverability: OOB swap dupa confirmare/editare → dispare cand needs_review==0. #}
<div id="preview-needs-review-banner" hx-swap-oob="true">
{% if summary.get('needs_review', 0) %}
<div class="banner warn" role="note" aria-live="polite"
     style="margin-bottom:12px; padding:8px 14px; border-radius:6px;
            background:color-mix(in srgb, var(--warn, #e6b34a) 12%, var(--card));
            border:1px solid var(--warn, #e6b34a); font-size:13px;">
  <div>
    Randurile cu <span class="pill s-needs_review" style="font-size:11px;">Verifica valori</span>
    nu pleaca la RAR pana confirmi valorile. Verifica-le (butonul <strong>Confirma valorile</strong>
    de pe rand sau in modal) sau, daca lotul e in regula, confirma-le pe toate deodata.
  </div>
  <button type="button"
          hx-post="/_import/{{ import_id }}/confirma-toate-review"
          hx-vals='{"csrf_token": "{{ csrf_token or '' }}"}'
          hx-target="#import-section"
          hx-swap="outerHTML"
          hx-confirm="Confirmi valorile pentru toate cele {{ summary.get('needs_review', 0) }} randuri de verificat? Devin gata de trimis la RAR."
          hx-disabled-elt="this"
          style="margin-top:10px; min-height:40px; padding:8px 18px;
                 background:var(--ok, #2a7); color:#fff; border-color:transparent; font-size:13px;">
    Confirma toate valorile ({{ summary.get('needs_review', 0) }})
  </button>
</div>
{% endif %}
</div>
<script>
(function() {
  /* Editare incheiata: re-activeaza confirm + butoanele Editeaza, recalculeaza N.
     Defer pe tick-ul urmator: la momentul rularii scriptului, swap-ul randului poate
     sa nu se fi asezat inca, deci tr[data-editing] ar fi inca prezent si updateN ar
     lasa confirm dezactivat (editing=true). Dupa setTimeout(0) randul e in mod display. */
  setTimeout(function() {
    document.querySelectorAll('.btn-editeaza').forEach(function(b) { b.disabled = false; });
    var btn = document.getElementById('confirm-btn');
    if (btn) btn.title = '';
    if (window.updateN) window.updateN();
  }, 0);
})();
</script>
{% endif %}
