{% extends "base.html" %}
{% block title %}Adjustment Requests{% endblock %}
{% block content %}
<div class="space-y-4">
  <div class="flex justify-between items-center">
    <h1 class="text-2xl font-bold">Stock Adjustment Requests</h1>
    <a href="{% url 'inventory:adjustment_request_create' %}" class="btn btn-primary btn-sm">New Request</a>
  </div>
  <form method="get" class="flex flex-wrap gap-2 items-end">
    <label class="form-control w-52">
      <span class="label-text">Status</span>
      <select name="status" class="select select-bordered">
        <option value="">All Statuses</option>
        <option value="pending" {% if current_status == 'pending' %}selected{% endif %}>Pending</option>
        <option value="approved" {% if current_status == 'approved' %}selected{% endif %}>Approved</option>
        <option value="rejected" {% if current_status == 'rejected' %}selected{% endif %}>Rejected</option>
      </select>
    </label>
    <label class="form-control w-64">
      <span class="label-text">Branch</span>
      <select name="branch" class="select select-bordered">
        <option value="">All Branches</option>
        {% for branch in branches %}
          <option value="{{ branch.id }}" {% if current_branch == branch.id|stringformat:"s" %}selected{% endif %}>{{ branch.name }}</option>
        {% endfor %}
      </select>
    </label>
    <button class="btn btn-primary btn-sm">Filter</button>
    {% if current_status or current_branch %}
      <a href="{% url 'inventory:adjustment_request_list' %}" class="btn btn-ghost btn-sm">Clear</a>
    {% endif %}
  </form>
  <div class="card bg-base-100 border border-base-200">
    <div class="overflow-x-auto">
      <table class="table table-zebra">
        <thead><tr><th>Product</th><th>Location</th><th>Qty</th><th>Reason</th><th>Status</th><th>Requested By</th><th></th></tr></thead>
        <tbody>
          {% for req in requests %}
          <tr>
            <td>{{ req.product }}</td>
            <td>
              {{ req.location|default:"-" }}
              {% if req.location %}<span class="opacity-60">({{ req.location.branch.name|default:"-" }})</span>{% endif %}
            </td>
            <td>{{ req.quantity }}</td>
            <td>{{ req.get_reason_display }}</td>
            <td>{{ req.get_status_display }}</td>
            <td>{{ req.requested_by|default:"-" }}</td>
            <td>
              {% if req.status == 'pending' %}
              <a href="{% url 'inventory:adjustment_request_approve' req.pk %}" class="btn btn-ghost btn-xs">Review</a>
              {% endif %}
            </td>
          </tr>
          {% empty %}
          <tr><td colspan="7" class="text-center opacity-70">No requests.</td></tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
  {% include "partials/pagination.html" with page_obj=requests %}
</div>
{% endblock %}
