{# Macro-uri partajate intre template-urile de import si mapari. #}

{# US-002 (PRD 5.11): autosend_toggle neutralizat — auto_send nu mai tine randuri (US-001).
   Simbolul pastrat (apelat in _mapari.html, _preview_import.html, _trimitere_detaliu.html)
   dar intoarce string gol. Coloanele DB raman (default 1, ne-citite pentru hold). #}
{% macro autosend_toggle(form_id='', checked=True, label='') -%}{%- endmacro %}

{# US-005 (PRD 5.12): macro `camp` partajat — extras din _trimitere_detaliu.html si
   _preview_rand.html. Suporta tip='date' (calendar nativ, D#10/R3) si tip='text' (default).

   Parametri:
     nome        — name="" al input-ului (si cheie in err_map/fix_map)
     eticheta    — text pentru label
     valoare     — valoarea curenta (pre-fill)
     tip         — type="" al input-ului: 'text' (default) sau 'date' (calendar nativ)
     err_map     — dict {field_name: mesaj_eroare}; default {}
     fix_map     — dict {field_name: hint_fix}; default {}
     vin_context — string VIN pentru aria-label cu context (default '')
     id_prefix   — prefix pentru id="" al input-ului (default 'c'; preview poate folosi 'e-N')
#}
{% macro camp(nome, eticheta, valoare, tip='text', err_map={}, fix_map={}, vin_context='', id_prefix='c', slim=False, mono=False) %}
{# slim=False: randare clasica (neschimbata). slim=True: varianta compacta (.camp-slim) din US-002 PRD 5.15:
   label 11px muted deasupra, input ~30px, fundal --card2.
   mono=True (valid numai cu slim=True): adauga clasa 'camp-mono' pe input pentru campuri
   VIN/odometru/nr (IBM Plex Mono, prin .camp-slim .camp-mono din base.html). #}
<div {% if slim %}class="camp-slim"{% else %}style="margin-bottom:10px;"{% endif %}>
  <label for="{{ id_prefix }}-{{ nome }}"{% if not slim %} class="muted" style="font-size:12px; display:block;"{% endif %}>{{ eticheta }}</label>
  {% if tip == 'date' %}
    {# D#10/R3: degradare grijulie pentru valori ne-YYYY-MM-DD.
       Daca valoarea nu e in formatul corect, inputul ramane gol + hint + hidden cu valoarea bruta
       (ca sa nu se piarda tacut la submit). #}
    {%- set _dp_ok = (valoare and valoare|length == 10 and valoare[4:5] == '-' and valoare[7:8] == '-') -%}
    <input id="{{ id_prefix }}-{{ nome }}" type="date" name="{{ nome }}"
           value="{{ valoare if _dp_ok else '' }}"
           {% if slim and mono %}class="camp-mono"{% endif %}
           style="{% if not slim %}width:100%; {% endif %}{% if err_map.get(nome) %}border-color:var(--err);{% endif %}"
           aria-label="{{ eticheta }}{% if vin_context %} (VIN: {{ vin_context }}){% endif %}"
           {% if err_map.get(nome) %}aria-invalid="true"{% endif %}>
    {% if not _dp_ok and valoare %}
    <input type="hidden" name="data_prestatie_raw" value="{{ valoare }}">
    <span class="camp-fix" style="font-size:12px;">Valoarea originala: {{ valoare }}</span>
    {% endif %}
  {% else %}
    <input id="{{ id_prefix }}-{{ nome }}" type="{{ tip }}" name="{{ nome }}"
           value="{{ valoare or '' }}"
           {% if slim and mono %}class="camp-mono"{% endif %}
           style="{% if not slim %}width:100%; {% endif %}{% if err_map.get(nome) %}border-color:var(--err);{% endif %}"
           {% if vin_context %}aria-label="{{ eticheta }} (VIN: {{ vin_context }})"{% endif %}
           {% if err_map.get(nome) %}aria-invalid="true"{% endif %}>
  {% endif %}
  {% if err_map.get(nome) %}
  <div class="s-error" style="font-size:12px; margin-top:2px;">{{ err_map.get(nome) }}</div>
  {% endif %}
  {% if fix_map.get(nome) %}
  <span class="camp-fix" style="font-size:12px;">{{ fix_map.get(nome) }}</span>
  {% endif %}
</div>
{% endmacro %}

{# PRD 5.13 — sistem butoane de actiune responsive.
   CSS-ul aferent (.act, .act-tx, .act-ic, .act-save, .act-del, .act-group)
   este definit in base.html.
   Desktop: se afiseaza textul (.act-tx); mobil: se afiseaza iconita (.act-ic). #}

{% macro icon(name) -%}
<svg class="act-ic" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor"
     stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
{%- if name == 'save' -%}<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><polyline points="17 21 17 13 7 13 7 21"/><polyline points="7 3 7 8 15 8"/>
{%- elif name == 'trash' -%}<polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/>
{%- elif name == 'edit' -%}<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
{%- elif name == 'plus' -%}<line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/>
{%- elif name == 'x' -%}<line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>
{%- endif -%}
</svg>
{%- endmacro %}

{% macro act_btn(label, ic, kind='', attrs='') -%}
<button class="act{% if kind %} act-{{ kind }}{% endif %}" aria-label="{{ label }}" {{ attrs | safe }}>
  <span class="act-tx">{{ label }}</span>{{ icon(ic) }}</button>
{%- endmacro %}
