{% 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 active" aria-current="page">{% trans "Dashboard" %}</li>
            </ol>
        </nav>
    </div>
</div>
{% endblock %}

{% block content %}
<div class="container py-5">
    <div class="row mb-4">
        <div class="col-md-8">
            <h2 class="mb-1">{% trans "Shipper Dashboard" %}</h2>
            <p class="text-muted">{% trans "Manage your shipping offers and active shipments." %}</p>
        </div>
        <div class="col-md-4 d-flex justify-content-md-end align-items-center">
            <div class="d-flex gap-2">
                <a href="{% url 'post_shipment' %}" class="btn btn-success">
                    <i class="fa-solid fa-plus me-2"></i> {% trans "Add A Bid" %}
                </a>
                <a href="{% url 'marketplace' %}" class="btn btn-primary">
                    <i class="fa-solid fa-search me-2"></i> {% trans "Browse Trucks" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Stats and Quick Actions -->
    <div class="row mb-5">
        <div class="col-md-3 mb-3">
            <div class="card shadow-sm border-0 bg-primary text-white h-100">
                <div class="card-body">
                    <h6 class="text-uppercase small opacity-75">{% trans "Financials" %}</h6>
                    <h4 class="mb-3">{% trans "Payments" %}</h4>
                    <a href="{% url 'financial_history' %}" class="btn btn-light btn-sm">{% trans "View History" %}</a>
                </div>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-body">
                    <h6 class="text-uppercase small text-muted">{% trans "Subscription" %}</h6>
                    {% if request.user.profile.is_subscription_active %}
                        <h4 class="mb-1 text-success">{% trans "Active" %}</h4>
                        <small class="text-muted d-block mb-3">{% trans "Expires:" %} {{ request.user.profile.subscription_expiry }}</small>
                    {% else %}
                        <h4 class="mb-1 text-danger">{% trans "Inactive" %}</h4>
                        <small class="text-muted d-block mb-3">{% trans "Payment required" %}</small>
                    {% endif %}
                    <a href="{% url 'renew_subscription' %}" class="btn btn-outline-primary btn-sm">{% trans "Manage" %}</a>
                </div>
            </div>
        </div>
    </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.display_origin }}</span>
                                <i class="fa-solid fa-arrow-right mx-1 text-muted small"></i>
                                <span>{{ bid.shipment.display_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 %}
                        <tr>
                            <td>{{ shipment.description|truncatechars:30 }}</td>
                            <td>{{ shipment.display_origin }} <i class="fa-solid fa-arrow-right mx-1 text-muted small"></i> {{ shipment.display_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>
                        {% 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 %}
