{% extends "base.html" %}
{% load i18n %}

{% block breadcrumbs %}
<div class="breadcrumb-container">
    <div class="container">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item"><a href="/"><i class="fa-solid fa-house me-1"></i> {% trans "Home" %}</a></li>
                <li class="breadcrumb-item"><a href="{% url 'dashboard' %}">{% trans "Dashboard" %}</a></li>
                <li class="breadcrumb-item active" aria-current="page">{% trans "Shipment Details" %}</li>
            </ol>
        </nav>
    </div>
</div>
{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="mb-4">
        <a href="{% url 'dashboard' %}" class="btn btn-outline-secondary btn-sm">
            <i class="fa-solid fa-arrow-left me-1"></i> {% trans "Back to Dashboard" %}
        </a>
    </div>

    <div class="row">
        <div class="col-md-8">
            <div class="card shadow-sm mb-4 border-0">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h2 class="mb-0">{{ shipment.display_origin }} <i class="fa-solid fa-arrow-right mx-2 text-muted"></i> {{ shipment.display_destination }}</h2>
                        <span class="badge {% if shipment.status == 'OPEN' %}bg-primary{% elif shipment.status == 'IN_PROGRESS' %}bg-warning{% else %}bg-success{% endif %} fs-6">
                            {{ shipment.get_status_display }}
                        </span>
                    </div>
                    <hr>
                    <h5 class="text-muted small text-uppercase fw-bold">{% trans "Shipment Details" %}</h5>
                    <p class="lead fw-normal">{{ shipment.description }}</p>
                    <div class="row mt-4">
                        <div class="col-md-4 mb-3">
                            <small class="text-muted d-block text-uppercase small">{% trans "Weight / Volume" %}</small>
                            <strong class="fs-5">{{ shipment.weight }}</strong>
                        </div>
                        <div class="col-md-4 mb-3">
                            <small class="text-muted d-block text-uppercase small">{% trans "Required Truck Type" %}</small>
                            <strong class="fs-5">
                                {% if shipment.required_truck_type_link %}
                                    {{ shipment.required_truck_type_link }}
                                {% else %}
                                    <span class="text-muted opacity-50">{% trans "Any" %}</span>
                                {% endif %}
                            </strong>
                        </div>
                        <div class="col-md-4 mb-3">
                            <small class="text-muted d-block text-uppercase small">{% trans "Requested Delivery Date" %}</small>
                            <strong class="fs-5">{{ shipment.delivery_date }}</strong>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Offers related to this shipment -->
            <div class="card shadow-sm mb-4 border-0">
                <div class="card-header bg-white py-3">
                    <h5 class="mb-0">{% trans "Offer Details" %}</h5>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>{% trans "Truck" %}</th>
                                    <th>{% trans "Amount" %}</th>
                                    <th>{% trans "Status" %}</th>
                                    <th>{% trans "Action" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for bid in bids %}
                                <tr>
                                    <td>
                                        <div class="fw-bold">{{ bid.truck.display_truck_type }}</div>
                                        <small class="text-muted">{{ bid.truck.plate_no }}</small>
                                    </td>
                                    <td class="fw-bold text-success">${{ bid.amount }}</td>
                                    <td>
                                        <span class="badge {% if bid.status == 'PENDING' %}bg-info{% elif bid.status == 'ACCEPTED' %}bg-success{% else %}bg-secondary{% endif %}">
                                            {{ bid.get_status_display }}
                                        </span>
                                    </td>
                                    <td>
                                        {% if bid.status == 'PENDING' and user == bid.truck_owner %}
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'accept_bid' bid.id %}" class="btn btn-success">{% trans "Accept" %}</a>
                                            <a href="{% url 'reject_bid' bid.id %}" class="btn btn-outline-danger">{% trans "Reject" %}</a>
                                        </div>
                                        {% else %}
                                        <span class="text-muted small">{% trans "No action available" %}</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="4" class="text-center py-5 text-muted">
                                        <i class="fa-solid fa-receipt fa-2x mb-3 opacity-25"></i>
                                        <p>{% trans "No offers for this shipment yet." %}</p>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            
            {% if shipment.status == 'IN_PROGRESS' %}
            <div class="card border-0 bg-success bg-opacity-10 mb-4">
                <div class="card-body d-flex align-items-center p-4">
                    <i class="fa-solid fa-truck-moving fa-3x me-4 text-success opacity-75"></i>
                    <div>
                        <h5 class="text-success mb-1">{% trans "Shipment is IN PROGRESS" %}</h5>
                        <p class="mb-0 text-dark">
                            {% trans "Assigned Truck:" %} <strong>{{ shipment.assigned_truck.display_truck_type }} ({{ shipment.assigned_truck.plate_no }})</strong>
                        </p>
                    </div>
                </div>
            </div>
            {% endif %}
        </div>
        
        <div class="col-md-4">
            <div class="card shadow-sm mb-4 border-0">
                <div class="card-body">
                    <h5 class="mb-4">{% trans "Contact Information" %}</h5>
                    <div class="mb-4">
                        <small class="text-muted d-block text-uppercase small">{% trans "Shipper" %}</small>
                        <div class="d-flex align-items-center mt-1">
                            <div class="bg-primary bg-opacity-10 rounded-circle p-2 me-2">
                                <i class="fa-solid fa-user text-primary"></i>
                            </div>
                            <strong class="fs-5">{{ shipment.shipper.username }}</strong>
                        </div>
                    </div>
                    {% if shipment.assigned_truck %}
                    <div class="mb-4">
                        <small class="text-muted d-block text-uppercase small">{% trans "Truck Owner" %}</small>
                        <div class="d-flex align-items-center mt-1">
                            <div class="bg-success bg-opacity-10 rounded-circle p-2 me-2">
                                <i class="fa-solid fa-user text-success"></i>
                            </div>
                            <strong class="fs-5">{{ shipment.assigned_truck.owner.username }}</strong>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>

            {% if shipment.status == 'IN_PROGRESS' %}
            <div class="d-grid gap-3">
                {% if user == shipment.shipper %}
                <a href="https://wa.me/{{ shipment.assigned_truck.owner.profile.full_phone_number }}" target="_blank" class="btn btn-success btn-lg py-3">
                    <i class="fa-brands fa-whatsapp me-2"></i> {% trans "WhatsApp Driver" %}
                </a>
                {% elif user == shipment.assigned_truck.owner %}
                <a href="https://wa.me/{{ shipment.shipper.profile.full_phone_number }}" target="_blank" class="btn btn-success btn-lg py-3">
                    <i class="fa-brands fa-whatsapp me-2"></i> {% trans "WhatsApp Shipper" %}
                </a>
                {% endif %}
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
