{% extends 'base.html' %}
{% block content %}
<div class="toolbar">
  <div>
    <h1>{{ t('jobdefs.title') }}</h1>
    <p class="muted">{{ t('jobdefs.description') }}</p>
  </div>
  <a class="button button-save" href="/job-definitions/new">{{ t('jobdefs.new') }}</a>
</div>

<section class="panel">
  <form method="get" action="/job-definitions" class="jobdefs-filter-form">
    <label class="jobdefs-filter-search">
      <span>{{ t('jobdefs.filter.search') }}</span>
      <input type="search" name="q" value="{{ search_text }}" placeholder="{{ t('jobdefs.filter.search_placeholder') }}">
    </label>
    <input type="hidden" id="jobdefs-active-only-value" name="active_only" value="{{ '1' if active_only else '0' }}">
    <label class="jobdefs-active-filter">
      <input id="jobdefs-active-only" type="checkbox" {% if active_only %}checked{% endif %} onchange="document.getElementById('jobdefs-active-only-value').value=this.checked?'1':'0'">
      <span>{{ t('jobdefs.filter.active_only') }}</span>
    </label>
    <button class="button button-secondary" type="submit">{{ t('jobdefs.filter.apply') }}</button>
    {% if search_text or not active_only %}<a class="button button-secondary" href="/job-definitions">{{ t('jobdefs.filter.reset') }}</a>{% endif %}
  </form>
</section>

<div class="panel table-panel">
  <table class="data-table">
    <thead>
      <tr>
        <th>{{ t('jobdefs.status') }}</th>
        <th>{{ t('common.name') }}</th>
        <th>{{ t('jobdefs.platform') }}</th>
        <th>{{ t('jobdefs.interpreter') }}</th>
        <th>{{ t('jobdefs.source') }}</th>
        <th>{{ t('jobdefs.revision') }}</th>
        <th>{{ t('common.actions') }}</th>
      </tr>
    </thead>
    <tbody>
      {% for row in definition_rows %}
      {% set item = row.item %}
      <tr>
        <td>
          <div class="jobdefs-status-list">
            <span class="software-settings-badge">{{ t('jobdefs.status.active') if item.enabled else t('jobdefs.status.inactive') }}</span>
            {% if item.is_system %}<span class="software-settings-badge">{{ t('jobdefs.system') }}</span>{% endif %}
          </div>
        </td>
        <td><strong>{{ item.name }}</strong><div class="muted">{{ item.description or '' }}</div></td>
        <td>{{ t('jobdefs.platform.' ~ item.platform, item.platform) }}</td>
        <td>{{ t('jobdefs.interpreter.' ~ item.interpreter, item.interpreter) }}</td>
        <td>{{ t('jobdefs.source.' ~ item.source_type, item.source_type) }}</td>
        <td>{{ item.revision }}</td>
        <td>
          <div class="table-actions">
            <a class="button button-secondary" href="/job-definitions/{{ item.id }}/edit">{{ t('common.edit') }}</a>
            {% if row.can_delete %}
              <form method="post" action="/job-definitions/{{ item.id }}/delete" class="inline-form" onsubmit="return confirm({{ t('jobdefs.delete.confirm')|tojson }});">
                <button class="button button-danger" type="submit">{{ t('common.delete') }}</button>
              </form>
            {% else %}
              <button class="button button-danger" type="button" disabled title="{{ t('jobdefs.delete.system_forbidden') if row.delete_reason == 'system' else t('jobdefs.delete.referenced') }}">{{ t('common.delete') }}</button>
            {% endif %}
          </div>
        </td>
      </tr>
      {% else %}
      <tr><td colspan="7">{{ t('jobdefs.none_filtered') if search_text or active_only else t('jobdefs.none') }}</td></tr>
      {% endfor %}
    </tbody>
  </table>
</div>
<section class="panel"><h2>{{ t('jobdefs.delete.behavior_title') }}</h2><p>{{ t('jobdefs.delete.behavior') }}</p></section>

<style>
.jobdefs-filter-form{display:flex;align-items:end;gap:1rem;flex-wrap:wrap}
.jobdefs-filter-search{display:grid;gap:.35rem;min-width:min(26rem,100%);flex:1}
.jobdefs-active-filter{display:flex;align-items:center;gap:.5rem;min-height:2.5rem;white-space:nowrap}
.jobdefs-status-list{display:flex;gap:.35rem;flex-wrap:wrap}
</style>
{% endblock %}
