{% extends "admin_frontend/base.html" %}
{% load crispy_forms_tags breadcrumbs %}
{% block title %}Emission Factors{% endblock %}
{% block content %}
{% render_breadcrumbs breadcrumbs %}
<div class="flex items-center justify-between mb-4">
  <h1 class="text-2xl font-semibold">Emission Factors</h1>
  <details class="relative">
    <summary class="cursor-pointer px-3 py-2 bg-blue-600 text-white rounded">New Factor</summary>
    <div class="absolute right-0 mt-2 bg-white shadow rounded p-4 w-96">
      {% include 'admin_frontend/_form.html' with form=form submit_label='Create' %}
    </div>
  </details>
  
</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:emission_factors_list' %}" class="btn-outline">Reset</a>
    </div>
  </form>
</div>

<div class="bg-white rounded shadow">
  <div class="p-4 border-b">
    <h2 class="font-medium">Factors</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">Material</th>
          <th class="px-4 py-2">Unit</th>
          <th class="px-4 py-2">kgCO2e/unit</th>
          <th class="px-4 py-2">Source</th>
          <th class="px-4 py-2">Effective</th>
          <th class="px-4 py-2">Actions</th>
        </tr>
      </thead>
      <tbody class="divide-y">
        {% for f in factors %}
        <tr>
          <td class="px-4 py-2">{{ f.organization.name }}</td>
          <td class="px-4 py-2">{{ f.material.name }}</td>
          <td class="px-4 py-2">{{ f.unit }}</td>
          <td class="px-4 py-2">{{ f.kgco2e_per_unit }}</td>
          <td class="px-4 py-2">{{ f.source|default:'-' }}</td>
          <td class="px-4 py-2">{{ f.effective_from|date:'Y-m-d' }}{% if f.effective_to %} → {{ f.effective_to|date:'Y-m-d' }}{% endif %}</td>
          <td class="px-4 py-2 space-x-2">
            <a class="text-blue-600 hover:underline" href="{% url 'recycle_core:emission_factor_edit' f.id %}">Edit</a>
            <form method="post" action="{% url 'recycle_core:emission_factor_delete' f.id %}" class="inline">
              {% csrf_token %}
              <button class="text-red-600 hover:underline" onclick="return confirm('Delete factor?')">Delete</button>
            </form>
          </td>
        </tr>
        {% empty %}
        <tr>
          <td class="px-4 py-4 text-gray-500" colspan="7">No factors.</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% include 'admin_frontend/_pagination.html' %}
</div>
{% endblock %}

