{% extends "allianceauth/base-bs5.html" %}
{% load i18n %}
{% load static %}
{% block extra_css %}
<link rel="stylesheet" href="{% static 'indy_hub/css/page_headers.css' %}">
<link rel="stylesheet" href="{% static 'indy_hub/css/blueprints.css' %}">
{% endblock %}

{% block content %}
<div class="container-fluid my-4">
    <div class="page-header mb-4">
        <div class="d-flex flex-wrap align-items-center justify-content-between gap-3">
            <div>
                <h1 class="h2 mb-1">
                    <i class="fas fa-list me-2"></i>
                    {% trans "My Copy Requests" %}
                </h1>
                <p class="description">{% trans "Track the status of your blueprint copy requests" %}</p>
            </div>
            <div class="d-flex gap-2">
                <a href="{% url 'indy_hub:index' %}" class="btn btn-outline-light">
                    <i class="fas fa-arrow-left me-1"></i>{% trans "Back to Dashboard" %}
                </a>
            </div>
        </div>
        <i class="fas fa-list header-bg"></i>
    </div>
    <div class="blueprint-list-group">
        <div class="blueprint-list">
            {% for req in my_requests %}
            <div class="blueprint-list-item list-group-item d-flex align-items-center justify-content-between py-3 px-3">
                <div class="d-flex align-items-center">
                    <img src="{{ req.icon_url }}" alt="{{ req.type_name }}" class="rounded me-2"
                        style="width:48px;height:48px;object-fit:cover;" loading="lazy"
                        onerror="this.onerror=null;this.src='https://images.evetech.net/types/{{ req.type_id }}/icon?size=48'" />
                    <span class="fw-semibold text-truncate">{{ req.type_name }}</span>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-primary text-white me-2">ME {{ req.material_efficiency }}</span>
                    <span class="badge bg-success text-white me-2">TE {{ req.time_efficiency }}</span>
                    <span class="badge bg-info text-white me-2">{% trans 'Copy' %} {{ req.copies_requested }}</span>
                    <span class="badge bg-warning text-dark me-3">{% trans 'Run' %} {{ req.runs_requested }}</span>
                    <form action="{% url 'indy_hub:bp_cancel_copy_request' req.id %}" method="post"
                        style="display:inline;">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-sm btn-outline-danger">
                            <i class="fas fa-times me-1"></i>{% trans "Cancel" %}
                        </button>
                    </form>
                </div>
                <!-- Show accepted offer (free or conditional) -->
                {% if req.accepted_offer %}
                <div class="mt-2 alert alert-success">
                    {% trans 'Accepted by' %} {{ req.accepted_offer.owner.username }}
                </div>
                {% elif req.cond_accepted %}
                <div class="mt-2 alert alert-info">
                    {% trans 'Accepted with condition by' %} {{ req.cond_accepted.owner.username }}<br>
                    <span class="text-muted">{{ req.cond_accepted.message }}</span>
                </div>
                {% else %}
                <!-- Show conditional offers to accept -->
                {% for offer in req.cond_offers %}
                <div class="mt-2 alert alert-warning">
                    <span class="fw-bold">{% trans 'Offer from' %} </span>
                    {% if offer.accepted_by_buyer %}
                    {{ offer.owner.username }}
                    {% else %}
                    {% trans 'Pending validation' %}
                    {% endif %}
                    <br>
                    <span class="text-muted">{{ offer.message }}</span>
                    <form method="post" action="{% url 'indy_hub:bp_buyer_accept_offer' offer.id %}" class="mt-2">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-sm btn-primary">{% trans 'Accept this offer' %}</button>
                    </form>
                </div>
                {% endfor %}
                {% endif %}
                {% if req.delivered %}
                <span class="badge bg-success ms-2">{% trans 'Delivered' %}</span>
                {% endif %}
            </div>
            {% empty %}
            <div class="text-center text-muted py-4">{% trans "You have no copy requests." %}</div>
            {% endfor %}
        </div>
    </div>
</div>
{% endblock %}
