{% extends "base.html" %}
{% from "components/ui.html" import page_header %}

{% block content %}
{% set breadcrumbs = [
    {'text': 'Inventory', 'url': url_for('inventory.list_stock_items')},
    {'text': 'Adjustments'}
] %}

{{ page_header(
    icon_class='fas fa-edit',
    title_text='Stock Adjustments',
    subtitle_text='View stock adjustments and corrections',
    breadcrumbs=breadcrumbs,
    actions_html='<a href="' + url_for("inventory.new_adjustment") + '" class="bg-primary text-white px-4 py-2 rounded-lg hover:bg-primary/90 transition-colors"><i class="fas fa-plus mr-2"></i>New Adjustment</a>' if (current_user.is_admin or has_permission('manage_stock_movements')) else None
) }}

<div class="bg-card-light dark:bg-card-dark p-6 rounded-xl border border-border-light dark:border-border-dark shadow-sm mb-6">
    <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
        <div>
            <label for="warehouse_id" class="form-label">{{ _('Warehouse') }}</label>
            <select name="warehouse_id" id="warehouse_id" class="form-input">
                <option value="">{{ _('All Warehouses') }}</option>
                {% for warehouse in warehouses %}
                <option value="{{ warehouse.id }}" {% if selected_warehouse_id == warehouse.id %}selected{% endif %}>{{ warehouse.code }} - {{ warehouse.name }}</option>
                {% endfor %}
            </select>
        </div>
        <div>
            <label for="stock_item_id" class="form-label">{{ _('Stock Item') }}</label>
            <select name="stock_item_id" id="stock_item_id" class="form-input">
                <option value="">{{ _('All Items') }}</option>
                {% for item in stock_items %}
                <option value="{{ item.id }}" {% if selected_stock_item_id == item.id %}selected{% endif %}>{{ item.sku }} - {{ item.name }}</option>
                {% endfor %}
            </select>
        </div>
        <div>
            <label for="date_from" class="form-label">{{ _('Date From') }}</label>
            <input type="date" name="date_from" id="date_from" value="{{ date_from or '' }}" class="form-input">
        </div>
        <div class="flex items-end">
            <button type="submit" class="bg-primary text-white px-4 py-2 rounded-lg hover:bg-primary/90 transition-colors w-full">{{ _('Filter') }}</button>
        </div>
        <div>
            <label for="date_to" class="form-label">{{ _('Date To') }}</label>
            <input type="date" name="date_to" id="date_to" value="{{ date_to or '' }}" class="form-input">
        </div>
    </form>
</div>

<div class="bg-card-light dark:bg-card-dark p-6 rounded-xl border border-border-light dark:border-border-dark shadow-sm overflow-x-auto">
    <table class="table table-zebra w-full text-left responsive-cards">
        <thead>
            <tr>
                <th class="p-4">{{ _('Date') }}</th>
                <th class="p-4">{{ _('Item') }}</th>
                <th class="p-4">{{ _('Warehouse') }}</th>
                <th class="p-4">{{ _('Quantity') }}</th>
                <th class="p-4">{{ _('Reason') }}</th>
                <th class="p-4">{{ _('User') }}</th>
            </tr>
        </thead>
        <tbody>
            {% for adjustment in adjustments %}
            <tr>
                <td class="p-4 mobile-card-header" data-label="{{ _('Date') }}">{{ adjustment.moved_at|user_datetime if adjustment.moved_at else '—' }}</td>
                <td class="p-4" data-label="{{ _('Item') }}">
                    <a href="{{ url_for('inventory.view_stock_item', item_id=adjustment.stock_item_id) }}" class="text-primary hover:underline">
                        {{ adjustment.stock_item.name }} ({{ adjustment.stock_item.sku }})
                    </a>
                </td>
                <td class="p-4" data-label="{{ _('Warehouse') }}">
                    <a href="{{ url_for('inventory.view_warehouse', warehouse_id=adjustment.warehouse_id) }}" class="text-primary hover:underline">
                        {{ adjustment.warehouse.code }}
                    </a>
                </td>
                <td class="p-4 {% if adjustment.quantity > 0 %}text-green-600{% else %}text-red-600{% endif %} font-semibold" data-label="{{ _('Quantity') }}">
                    {{ '+' if adjustment.quantity > 0 else '' }}{{ adjustment.quantity }}
                </td>
                <td class="p-4" data-label="{{ _('Reason') }}">{{ adjustment.reason or '—' }}</td>
                <td class="p-4" data-label="{{ _('User') }}">{{ adjustment.moved_by_user.username if adjustment.moved_by_user else '—' }}</td>
            </tr>
            {% else %}
            <tr>
                <td colspan="6" class="p-8 text-center text-gray-500">
                    {{ _('No adjustments found.') }}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %}

