{% extends "industry_reforged/base.html" %}
{% load i18n humanize %}

{% block details %}
<div class="container-fluid">
    <div class="row mb-3">
        <div class="col-12 d-flex justify-content-between align-items-center">
            <h3><i class="fas fa-boxes"></i> {% trans "Inventory & Analytics" %}</h3>
            <div>
                <a href="{% url 'industry_reforged:trigger_inventory_sync' %}" class="btn btn-warning me-2 show-loader"><i class="fas fa-sync"></i> Sync Inventory</a>
                <a href="{% url 'industry_reforged:director_dashboard' %}" class="btn btn-primary"><i class="fas fa-arrow-left"></i>
                    Back</a>
            </div>
        </div>
    </div>


    <div class="card border-0 mb-4 h-100">
        <div class="card-header p-0 border-bottom-0">
            <ul class="nav nav-tabs" id="inventory-tabs" role="tablist">
                <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="overview-tab" data-bs-toggle="tab" data-bs-target="#overview-pane"
                        type="button" role="tab" aria-controls="overview-pane" aria-selected="true">
                        <i class="fas fa-clipboard-list"></i> Corporate Inventory
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="alerts-tab" data-bs-toggle="tab" data-bs-target="#alerts-pane"
                        type="button" role="tab" aria-controls="alerts-pane" aria-selected="false">
                        <i class="fas fa-exclamation-triangle"></i> Low Stock Alerts
                        {% if low_stock %}<span class="badge bg-danger ms-1">{{ low_stock|length }}</span>{% endif %}
                    </button>
                </li>
            </ul>
        </div>
        <div class="card-body">
            <div class="tab-content" id="inventory-tabContent">
                <!-- Overview Tab -->
                <div class="tab-pane fade show active" id="overview-pane" role="tabpanel" aria-labelledby="overview-tab" tabindex="0">
                    <div class="table-responsive" style="max-height: 600px; overflow-y: auto;">
                        <table class="table table-striped table-hover m-0">
                            <thead>
                                <tr>
                                    <th>Item</th>
                                    <th class="text-end">Total Quantity</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for inv in inventory %}
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <img onerror="this.onerror=null; this.src='https://images.evetech.net/types/3514/icon?size=32';" src="https://images.evetech.net/types/{{ inv.item_type__id }}/icon?size=32"
                                                alt="{{ inv.item_type__name }}" width="24" height="24"
                                                class="me-2 rounded">
                                            <span>{{ inv.item_type__name }}</span>
                                        </div>
                                    </td>
                                    <td class="text-end numeric">{{ inv.total_qty|intcomma }}</td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="2" class="text-center py-4">
                                        <div class="text-muted mb-3"><i class="fas fa-info-circle fa-2x"></i></div>
                                        <h5 class="text-white">No inventory records found</h5>
                                        <p class="text-muted">To populate your inventory, you must first configure which Corporate Hangars to track.</p>
                                        <a href="{% url 'industry_reforged:director_config' %}" class="btn btn-sm btn-success mt-2"><i class="fas fa-cog"></i> Go to Configurations</a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>

                <!-- Alerts Tab -->
                <div class="tab-pane fade" id="alerts-pane" role="tabpanel" aria-labelledby="alerts-tab" tabindex="0">
                    {% if low_stock %}
                    <div class="alert alert-danger shadow-sm mb-4" style="border-left: 4px solid #dc3545;">
                        <h5 class="alert-heading text-danger mb-0"><i class="fas fa-exclamation-triangle"></i> Action Required: Low Stock Detected</h5>
                    </div>
                    <div class="table-responsive" style="max-height: 600px; overflow-y: auto;">
                        <table class="table table-sm table-borderless m-0">
                        <thead>
                            <tr class="text-danger">
                                <th>Item</th>
                                <th class="text-end">Current Stock</th>
                                <th class="text-end">Target Threshold</th>
                                <th class="text-end">Deficit</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for item in low_stock %}
                            <tr>
                                <td><strong class="text-white">{{ item.item_type.name }}</strong></td>
                                <td class="text-end numeric text-muted">{{ item.current_qty|intcomma }}</td>
                                <td class="text-end numeric text-muted">{{ item.target|intcomma }}</td>
                                <td class="text-end numeric"><strong class="text-danger">{{ item.deficit|intcomma
                                        }}</strong></td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                    </div>
                    {% else %}
                    <div class="alert alert-success shadow-sm">
                        <i class="fas fa-check-circle"></i> No low stock alerts! All tracked items are above their target thresholds.
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
