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

{% block content %}
<div class="container py-5">
    <div class="row">
        <div class="col-md-8">
            <div class="card shadow-sm mb-4">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h2 class="mb-0">{{ shipment.origin }} <i class="fa-solid fa-arrow-right mx-2"></i> {{ shipment.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>{% trans "Shipment Details" %}</h5>
                    <p class="lead">{{ shipment.description }}</p>
                    <div class="row">
                        <div class="col-6 mb-3">
                            <small class="text-muted d-block">{% trans "Weight" %}</small>
                            <strong>{{ shipment.weight }}</strong>
                        </div>
                        <div class="col-6 mb-3">
                            <small class="text-muted d-block">{% trans "Requested Delivery Date" %}</small>
                            <strong>{{ shipment.delivery_date }}</strong>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Offers related to this shipment -->
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-white">
                    <h5 class="mb-0">{% trans "Offer Status" %}</h5>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                                <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>{{ bid.truck.display_truck_type }} ({{ bid.truck.plate_no }})</td>
                                    <td>${{ 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-4 text-muted">{% trans "No offers for this shipment." %}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            
            {% if shipment.status == 'IN_PROGRESS' %}
            <div class="alert alert-success d-flex align-items-center p-4">
                <i class="fa-solid fa-truck-moving fa-3x me-4 text-success opacity-50"></i>
                <div>
                    <h5 class="alert-heading">{% trans "Shipment is IN PROGRESS" %}</h5>
                    <p class="mb-0">{% trans "Assigned Truck:" %} <strong>{{ shipment.assigned_truck.display_truck_type }} ({{ shipment.assigned_truck.plate_no }})</strong></p>
                </div>
            </div>
            {% endif %}
        </div>
        
        <div class="col-md-4">
            <div class="card shadow-sm mb-4">
                <div class="card-body">
                    <h5>{% trans "Stakeholders" %}</h5>
                    <div class="mb-3">
                        <small class="text-muted d-block">{% trans "Shipper" %}</small>
                        <strong>{{ shipment.shipper.username }}</strong>
                    </div>
                    {% if shipment.assigned_truck %}
                    <div class="mb-3">
                        <small class="text-muted d-block">{% trans "Truck Owner" %}</small>
                        <strong>{{ shipment.assigned_truck.owner.username }}</strong>
                    </div>
                    {% endif %}
                </div>
            </div>

            {% if shipment.status == 'IN_PROGRESS' %}
            <div class="d-grid gap-2">
                {% 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">
                    <i class="fa-brands fa-whatsapp me-2"></i> {% trans "Message 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">
                    <i class="fa-brands fa-whatsapp me-2"></i> {% trans "Message Shipper" %}
                </a>
                {% endif %}
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
