{% extends "admin_frontend/base.html" %}
{% load static crispy_forms_tags breadcrumbs %}
{% block title %}Materials{% endblock %}
{% block content %}
{% render_breadcrumbs breadcrumbs %}
<div class="flex items-center justify-between mb-4">
  <h1 class="text-2xl font-semibold">Materials</h1>
  <div class="flex items-center gap-2">
    <details class="relative">
      <summary class="cursor-pointer px-3 py-2 bg-blue-600 text-white rounded">New Material</summary>
      <div class="absolute right-0 mt-2 bg-white shadow rounded p-4 w-96">
        {% include 'admin_frontend/_form.html' with form=mat_form submit_label='Create' %}
      </div>
    </details>
    <details class="relative">
      <summary class="cursor-pointer px-3 py-2 bg-gray-700 text-white rounded">New Category</summary>
      <div class="absolute right-0 mt-2 bg-white shadow rounded p-4 w-96">
        {% include 'admin_frontend/_form.html' with form=cat_form submit_label='Create' %}
      </div>
    </details>
  </div>
</div>

<div class="bg-white rounded shadow p-4 mb-4">
  <form method="get" class="space-y-3 flex gap-2">
    {{ filter.form|crispy }}
    <div class="flex items-center gap-2">
      <button class="px-3 py-2 bg-blue-600 text-white rounded">Filter</button>
      <a href="{% url 'recycle_core:materials_list' %}" class="btn-outline">Reset</a>
    </div>
  </form>
  {% if filter.qs.count != materials|length %}
    <p class="text-sm text-gray-500 mt-2">Filtered results: {{ materials|length }} items</p>
  {% endif %}
</div>

<div class="bg-white rounded shadow">
  <div class="p-4 border-b">
    <h2 class="font-medium">Material List</h2>
  </div>
  <div class="overflow-x-auto">
    <table class="min-w-full">
      <thead>
        <tr class="text-left text-sm text-gray-600">
          <th class="px-4 py-2">Organization</th>
          <th class="px-4 py-2">Category</th>
          <th class="px-4 py-2">Name</th>
          <th class="px-4 py-2">Code</th>
          <th class="px-4 py-2">Unit</th>
          <th class="px-4 py-2">Actions</th>
        </tr>
      </thead>
      <tbody class="divide-y">
        {% for m in materials %}
          <tr>
            <td class="px-4 py-2">{{ m.organization.name }}</td>
            <td class="px-4 py-2">{{ m.category.name }}</td>
            <td class="px-4 py-2">{{ m.name }}</td>
            <td class="px-4 py-2">{{ m.code }}</td>
            <td class="px-4 py-2">{{ m.get_default_unit_display }}</td>
            <td class="px-4 py-2 space-x-2">
              <a class="text-blue-600 hover:underline" href="{% url 'recycle_core:material_edit' m.id %}">Edit</a>
              <form method="post" action="{% url 'recycle_core:material_delete' m.id %}" class="inline">
                {% csrf_token %}
                <button class="text-red-600 hover:underline" onclick="return confirm('Delete material?')">Delete</button>
              </form>
            </td>
          </tr>
        {% empty %}
          <tr>
            <td class="px-4 py-4 text-gray-500" colspan="6">No materials.</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% endblock %}
