{% extends 'base.html' %}
{% block title %}Expenses{% endblock %}

{% block content %}
<div class="flex justify-between items-center mb-6">
  <h1 class="text-2xl font-bold">Expenses</h1>
  <a href="{% url 'finance:export_expenses' %}{% if request.GET %}?{{ request.GET.urlencode }}{% endif %}"
     class="btn btn-outline btn-sm">
    ⬇ Export CSV
  </a>
  <a href="{% url 'finance:expense_create' %}" class="btn btn-primary btn-sm">➕ Add Expense</a>
</div>

<!-- Search & Filter -->
<form method="get" class="flex flex-wrap gap-2 items-end mb-4">
  <div class="form-control">
    <input type="text" name="q" value="{{ query }}"
           placeholder="Search description..."
           class="input input-bordered input-sm w-64"/>
  </div>
  <div class="form-control">
    <select name="category" class="select select-bordered select-sm">
      <option value="">All Categories</option>
      {% for cat in categories %}
        <option value="{{ cat.pk }}" {% if current_category == cat.pk|stringformat:"s" %}selected{% endif %}>
          {{ cat.name }}
        </option>
      {% endfor %}
    </select>
  </div>
  <div class="form-control">
    <input type="date" name="start" value="{{ request.GET.start }}" class="input input-bordered input-sm"/>
  </div>
  <div class="form-control">
    <input type="date" name="end" value="{{ request.GET.end }}" class="input input-bordered input-sm"/>
  </div>
  <button class="btn btn-primary btn-sm">🔍 Search</button>
  <a href="{% url 'finance:expense_list' %}" class="btn btn-ghost btn-sm">Reset</a>
</form>
<div class="card bg-base-100 shadow">
  <div class="card-body p-0">
    <table class="table table-zebra">
      <thead>
        <tr><th>Description</th><th>Category</th><th>Account</th><th>Amount</th><th>Date</th><th>By</th></tr>
      </thead>
      <tbody>
        {% for expense in expenses %}
        <tr>
          <td>{{ expense.description }}</td>
          <td><span class="badge badge-ghost badge-sm">{{ expense.category.name }}</span></td>
          <td>{{ expense.account.name }}</td>
          <td class="text-error font-medium">{{ business.currency }} {{ expense.amount|floatformat:0 }}</td>
          <td>{{ expense.date|date:"d M Y" }}</td>
          <td>{{ expense.created_by.username|default:"—" }}</td>
        </tr>
        {% empty %}
        <tr>
          <td colspan="6" class="text-center text-base-content/40 py-10">
            No expenses yet. <a href="{% url 'finance:expense_create' %}" class="link link-primary">Add first expense</a>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% include 'partials/pagination.html' with page_obj=expenses %}
{% endblock %}