{% extends 'base.html' %}
{% block content %}
<div class="toolbar"><div><h1>Daten mehrerer Assets ändern</h1><p class="muted">Die Änderung wird auf {{ assets|length }} markierte Assets angewendet.</p></div><a class="button button-secondary" href="/assets{% if category_id %}?category_id={{ category_id }}{% endif %}">Abbrechen</a></div>
<section class="panel bulk-edit-panel">
  <form method="post" action="/assets/bulk-edit/apply" id="bulk-edit-apply-form">
    {% for id in asset_ids %}<input type="hidden" name="asset_ids" value="{{ id }}">{% endfor %}
    {% if category_id %}<input type="hidden" name="category_id" value="{{ category_id }}">{% endif %}
    <div class="form-grid">
      <label>Feld
        <select name="field_name" id="bulk-field" required>
          <option value="">Bitte auswählen</option>
          {% for field in fields %}<option value="{{ field.field_name }}">{{ field.label }}</option>{% endfor %}
        </select>
      </label>
      <label>Neuer Wert
        <input name="new_value" id="bulk-value" autocomplete="off">
        <select id="bulk-status-value" class="hidden-control">
          <option value="">Leer / nicht gesetzt</option>
          {% for status in status_options %}<option value="{{ status.name }}">{{ status.name }}</option>{% endfor %}
        </select>
        <select id="bulk-parent-value" class="hidden-control">
          <option value="">Kein übergeordnetes Gerät</option>
          {% for parent in parent_candidates %}<option value="{{ parent.id }}">{{ parent.name }}{% if parent.asset_tag %} – {{ parent.asset_tag }}{% endif %} ({{ parent.category.name }})</option>{% endfor %}
        </select>
        <select id="bulk-category-value" class="hidden-control">
          <option value="">Bitte Kategorie auswählen</option>
          {% for category in categories %}<option value="{{ category.id }}">{{ category.name }}</option>{% endfor %}
        </select>
      </label>
    </div>
    <p class="muted">Ein leerer Wert entfernt den bisherigen Wert. Inventarnummer, Seriennummer und MeshCentral Node-ID sind aus Sicherheitsgründen von der Massenänderung ausgeschlossen.</p>
    <details><summary>Ausgewählte Assets anzeigen</summary><ul class="compact-list">{% for asset in assets %}<li>{{ asset.name }} <span class="muted">({{ asset.category.name }})</span></li>{% endfor %}</ul></details>
    <div class="form-actions"><a class="button button-secondary" href="/assets{% if category_id %}?category_id={{ category_id }}{% endif %}">Abbrechen</a><button type="submit">💾 Für {{ assets|length }} Assets speichern</button></div>
  </form>
</section>
<script>
(() => {
  const field = document.getElementById('bulk-field');
  const input = document.getElementById('bulk-value');
  const status = document.getElementById('bulk-status-value');
  const parent = document.getElementById('bulk-parent-value');
  const category = document.getElementById('bulk-category-value');
  const controls = [input, status, parent, category];
  field.addEventListener('change', () => {
    const selected = field.value;
    const active = selected === 'status' ? status : selected === 'parent_asset_id' ? parent : selected === 'category_id' ? category : input;
    controls.forEach(control => {
      control.classList.toggle('hidden-control', control !== active);
      if (control === active) control.name = 'new_value'; else control.removeAttribute('name');
    });
    const dateFields = ['assigned_on','purchase_date','warranty_until'];
    input.type = dateFields.includes(selected) ? 'date' : 'text';
  });
})();
</script>
{% endblock %}
