{% extends "base.html" %}

{% block title %}SO {{ so.so_number }} Details{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Sales Order: {{ so.so_number }}</h2>
        <div>
            <!-- <a href="{% url 'sales_order_edit' so.id %}" class="btn btn-outline-primary">
                <i class="bi bi-pencil"></i> Edit
            </a> -->
            <a href="{% url 'sales_order_list' %}" class="btn btn-outline-secondary">
                Back to List
            </a>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <h5>Supplier Information</h5>
                    <p><strong>Name:</strong> {{ so.customer.name }}</p>
                    <p><strong>Company:</strong> {{ so.company_id }}</p>
                    <p><strong>Phone:</strong> {{ so.customer.phone|default:"-" }}</p>
                </div>
                <div class="col-md-4">
                    <h5>Order Details</h5>
                    <p><strong>Date:</strong> {{ so.order_date|date:"M d, Y" }}</p>
                    <p><strong>Status:</strong> 
                        <span class="badge bg-{% if so.status == 'draft' %}secondary
                                            {% elif so.status == 'approved' %}success
                                            {% elif so.status == 'cancelled' %}danger
                                            {% else %}primary{% endif %}">
                            {{ so.get_status_display }}
                        </span>
                    </p>
                    <p><strong>Job:</strong> {{ so.job.job_title|default:"-" }}</p>
                </div>
                <div class="col-md-4">
                    <h5>Financials</h5>
                    <p><strong>Total Amount:</strong> ${{ total_amount|floatformat:2 }}</p>
                    
                    
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">Order Items</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th>Description</th>
                            <th>Price</th>
                            <th>Quantity</th>
                            <th>Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in items %}
                        <tr>
                            <td>{{ item.product.name }}</td>
                            <td>{{ item.description|default:"-" }}</td>
                            <td>${{ item.price|floatformat:2 }}</td>
                            <td>{{ item.quantity }}</td>
                            <td>${{ item.total|floatformat:2 }}</td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="5" class="text-center">No items found</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    {% if sales_returns %}
    <div class="card mt-4">
        <div class="card-header bg-warning">
            <h5 class="mb-0">Sales Returns</h5>
        </div>
        <div class="card-body">
            {% for return_entry in sales_returns %}
                <div class="mb-4">
                    <h6>Return #: {{ return_entry.return_number }} | Date: {{ return_entry.return_date|date:"M d, Y" }}</h6>
                    {% if return_entry.notes %}
                        <p><strong>Notes:</strong> {{ return_entry.notes }}</p>
                    {% endif %}
                    <div class="table-responsive">
                        <table class="table table-sm table-bordered">
                            <thead class="table-light">
                                <tr>
                                    <th>Product</th>
                                    <th>Returned Quantity</th>
                                    <th>Unit Price</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for item in return_entry.items.all %}
                                <tr>
                                    <td>{{ item.product.name }}</td>
                                    <td>{{ item.quantity }}</td>
                                    <td>${{ item.price|floatformat:2 }}</td>
                                    <td>${{ item.total_amount|floatformat:2 }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            {% endfor %}

            <!-- Here show the already-calculated total -->
            <div class="d-flex justify-content-end">
                <h5><strong>Total Returned: ${{ grand_return_total|floatformat:2 }}</strong></h5>
            </div>
            <div class="d-flex justify-content-end mt-3">
                <h4>
                    <span class="badge bg-success">
                        Net Receivable: ${{ net_receivable|floatformat:2 }}
                    </span>
                </h4>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}