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

{% 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 "Truck Owner Dashboard" %}</h2>
            <p class="text-muted">{% trans "Manage your fleet and incoming shipping offers." %}</p>
        </div>
        <div class="col-md-4 d-flex justify-content-md-end align-items-center">
            <a href="{% url 'truck_register' %}" class="btn btn-success">
                <i class="fa-solid fa-plus me-2"></i> {% trans "Add New Truck" %}
            </a>
        </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>

    <!-- Incoming Offers Section -->
    <div class="card shadow-sm border-0 mb-5">
        <div class="card-header bg-white py-3">
            <h5 class="mb-0 text-primary">{% trans "Received 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 "Route" %}</th>
                            <th>{% trans "Truck" %}</th>
                            <th>{% trans "Shipper" %}</th>
                            <th>{% trans "Offer Amount" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Action" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for bid in bids %}
                        <tr>
                            <td>
                                <div class="fw-bold">{{ bid.shipment.display_origin }} <i class="fa-solid fa-arrow-right mx-1 text-muted small"></i> {{ bid.shipment.display_destination }}</div>
                                <small class="text-muted">{{ bid.shipment.delivery_date }}</small>
                            </td>
                            <td>{{ bid.truck.plate_no }}</td>
                            <td>{{ bid.shipment.shipper.username }}</td>
                            <td class="fw-bold text-success">${{ bid.amount }}</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-secondary">{% trans "Rejected" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if bid.status == 'PENDING' %}
                                <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 %}
                                <a href="{% url 'shipment_detail' bid.shipment.id %}" class="btn btn-sm btn-outline-primary">{% trans "View Details" %}</a>
                                {% endif %}
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="fa-solid fa-inbox fa-2x mb-3 opacity-25"></i>
                                <p>{% trans "No offers received yet." %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- My Fleet -->
    <h4 class="mb-3">{% trans "My Fleet" %}</h4>
    <div class="row">
        {% for truck in trucks %}
        <div class="col-md-4 mb-4">
            <div class="card h-100 shadow-sm border-0">
                {% if truck.truck_picture %}
                    <img src="{{ truck.truck_picture.url }}" class="card-img-top" alt="{{ truck.display_truck_type }}" style="height: 200px; object-fit: cover;">
                {% else %}
                    <div class="bg-light d-flex align-items-center justify-content-center" style="height: 200px;">
                        <i class="fa-solid fa-truck fa-4x text-secondary opacity-25"></i>
                    </div>
                {% endif %}
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <h5 class="card-title mb-0">{{ truck.display_truck_type }}</h5>
                        <span class="badge bg-success">{% trans "Approved" %}</span>
                    </div>
                    <p class="card-text mb-1"><small class="text-muted">{% trans "Plate No:" %}</small> <strong>{{ truck.plate_no }}</strong></p>
                    <p class="card-text mb-1"><small class="text-muted">{% trans "Capacity:" %}</small> {{ truck.display_load_capacity }}</p>
                    <a href="{% url 'edit_truck' truck.id %}" class="btn btn-outline-primary btn-sm mt-3 w-100">
                        <i class="fa-solid fa-edit me-1"></i> {% trans "Edit Truck" %}
                    </a>
                </div>
            </div>
        </div>
        {% endfor %}

        {% for truck in pending_trucks %}
        <div class="col-md-4 mb-4">
            <div class="card h-100 shadow-sm border-0 bg-light">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <h5 class="card-title mb-0 text-muted">{{ truck.display_truck_type }}</h5>
                        <span class="badge bg-warning text-dark">{% trans "Pending" %}</span>
                    </div>
                    <p class="card-text mb-1"><small class="text-muted">{% trans "Plate No:" %}</small> <strong>{{ truck.plate_no }}</strong></p>
                    <p class="card-text mt-3"><small class="text-muted italic">{% trans "Waiting for admin approval..." %}</small></p>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>
{% endblock %}
