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

{% block content %}
<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">{% trans "Shipper Dashboard" %}</h2>
            <p class="text-muted">{% trans "Manage your shipping offers and active shipments." %}</p>
        </div>
        <a href="{% url 'marketplace' %}" class="btn btn-primary btn-lg">
            <i class="fa-solid fa-search me-2"></i> {% trans "Browse Trucks" %}
        </a>
    </div>

    <!-- Active Offers -->
    <div class="card shadow-sm border-0 mb-5">
        <div class="card-header bg-white py-3">
            <h5 class="mb-0 text-primary">{% trans "My Shipping Offers" %}</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 "Route" %}</th>
                            <th>{% trans "Amount" %}</th>
                            <th>{% trans "Date Sent" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Action" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for bid in bids %}
                        <tr>
                            <td>
                                <div><strong>{{ bid.truck.display_truck_type }}</strong></div>
                                <small class="text-muted">{{ bid.truck.plate_no }}</small>
                            </td>
                            <td>
                                <span>{{ bid.shipment.origin }}</span>
                                <i class="fa-solid fa-arrow-right mx-1 text-muted small"></i>
                                <span>{{ bid.shipment.destination }}</span>
                            </td>
                            <td class="fw-bold">${{ bid.amount }}</td>
                            <td>{{ bid.created_at|date:"d M Y" }}</td>
                            <td>
                                {% if bid.status == 'PENDING' %}
                                <span class="badge bg-info">{% trans "Pending" %}</span>
                                {% elif bid.status == 'ACCEPTED' %}
                                <span class="badge bg-success">{% trans "Accepted" %}</span>
                                {% else %}
                                <span class="badge bg-danger">{% trans "Rejected" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% url 'shipment_detail' bid.shipment.id %}" class="btn btn-sm btn-outline-secondary">{% trans "Details" %}</a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="fa-solid fa-paper-plane fa-2x mb-3 opacity-25"></i>
                                <p>{% trans "You haven't sent any offers yet." %}</p>
                                <a href="{% url 'marketplace' %}" class="btn btn-sm btn-primary">{% trans "Find a Truck" %}</a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Active Shipments -->
    <div class="card shadow-sm border-0">
        <div class="card-header bg-white py-3">
            <h5 class="mb-0">{% trans "Active Shipments" %}</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 "Description" %}</th>
                            <th>{% trans "Route" %}</th>
                            <th>{% trans "Delivery Date" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Assigned Truck" %}</th>
                            <th>{% trans "Action" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for shipment in shipments %}
                        {% if shipment.status != 'OPEN' or not shipment.bids.exists %}
                        <tr>
                            <td>{{ shipment.description|truncatechars:30 }}</td>
                            <td>{{ shipment.origin }} <i class="fa-solid fa-arrow-right mx-1 text-muted small"></i> {{ shipment.destination }}</td>
                            <td>{{ shipment.delivery_date }}</td>
                            <td>
                                <span class="badge {% if shipment.status == 'IN_PROGRESS' %}bg-warning{% elif shipment.status == 'COMPLETED' %}bg-success{% else %}bg-secondary{% endif %}">
                                    {{ shipment.get_status_display }}
                                </span>
                            </td>
                            <td>
                                {% if shipment.assigned_truck %}
                                    {{ shipment.assigned_truck.plate_no }}
                                {% else %}
                                    <span class="text-muted italic">{% trans "None" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% url 'shipment_detail' shipment.id %}" class="btn btn-sm btn-outline-primary">{% trans "Track" %}</a>
                            </td>
                        </tr>
                        {% endif %}
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-4 text-muted">{% trans "No active shipments." %}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}