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

{% block title %}{% trans "Quotations" %} | {{ site_settings.business_name }}{% endblock %}

{% block content %}
<div class="container-fluid px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold mb-0">{% trans "Quotations" %}</h2>
            <p class="text-muted small mb-0">{% trans "Manage and track your price proposals" %}</p>
        </div>
        <a href="{% url 'quotation_create' %}" class="btn btn-primary rounded-3 px-4 shadow-sm">
            <i class="bi bi-plus-circle me-2"></i>{% trans "New Quotation" %}
        </a>
    </div>

    {% if messages %}
    <div class="mb-4">
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible fade show rounded-3 shadow-sm border-0" role="alert">
            <i class="bi bi-info-circle me-2"></i>{{ message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">{% trans "Quotation #" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Customer" %}</th>
                            <th>{% trans "Total" %}</th>
                            <th>{% trans "User" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Valid Until" %}</th>
                            <th class="text-end pe-4">{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for q in quotations %}
                        <tr>
                            <td class="ps-4 fw-bold">
                                {{ q.quotation_number|default:q.id }}
                            </td>
                            <td>{{ q.created_at|date:"Y-m-d" }}</td>
                            <td>{{ q.customer.name|default:_("Guest") }}</td>
                            <td class="fw-bold text-dark">{{ site_settings.currency_symbol }}{{ q.total_amount|floatformat:3 }}</td>
                            <td>
                                <span class="text-muted small">
                                    <i class="bi bi-person me-1"></i>{{ q.created_by.username|default:"System" }}
                                </span>
                            </td>
                            <td>
                                {% if q.status == 'draft' %}
                                    <span class="badge bg-secondary-subtle text-secondary rounded-pill px-3">{% trans "Draft" %}</span>
                                {% elif q.status == 'sent' %}
                                    <span class="badge bg-info-subtle text-info rounded-pill px-3">{% trans "Sent" %}</span>
                                {% elif q.status == 'accepted' %}
                                    <span class="badge bg-success-subtle text-success rounded-pill px-3">{% trans "Accepted" %}</span>
                                {% elif q.status == 'converted' %}
                                    <span class="badge bg-primary-subtle text-primary rounded-pill px-3">{% trans "Converted" %}</span>
                                {% elif q.status == 'rejected' %}
                                    <span class="badge bg-danger-subtle text-danger rounded-pill px-3">{% trans "Rejected" %}</span>
                                {% endif %}
                            </td>
                            <td>{{ q.valid_until|date:"Y-m-d"|default:"-" }}</td>
                            <td class="text-end pe-4">
                                <div class="btn-group shadow-sm rounded-3">
                                    <a href="{% url 'quotation_detail' q.id %}" class="btn btn-sm btn-white border" title="{% trans 'View & Print' %}">
                                        <i class="bi bi-printer"></i>
                                    </a>
                                    {% if q.status != 'converted' %}
                                    <button type="button" class="btn btn-sm btn-white border text-success" data-bs-toggle="modal" data-bs-target="#convertModal{{ q.id }}" title="{% trans 'Convert to Invoice' %}">
                                        <i class="bi bi-arrow-right-circle"></i>
                                    </button>
                                    {% endif %}
                                    <button type="button" class="btn btn-sm btn-white border text-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ q.id }}">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </div>

                                <!-- Convert Modal -->
                                <div class="modal fade text-start" id="convertModal{{ q.id }}" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content border-0 shadow rounded-4">
                                            <div class="modal-body p-4 text-center">
                                                <div class="text-primary mb-3">
                                                    <i class="bi bi-question-circle" style="font-size: 3rem;"></i>
                                                </div>
                                                <h4 class="fw-bold">{% trans "Convert to Invoice?" %}</h4>
                                                <p class="text-muted">{% trans "This will create a sales invoice and deduct items from stock. You won't be able to undo this easily." %}</p>
                                                <div class="d-grid gap-2">
                                                    <a href="{% url 'convert_quotation_to_invoice' q.id %}" class="btn btn-primary rounded-3 py-2">{% trans "Yes, Convert to Invoice" %}</a>
                                                    <button type="button" class="btn btn-light rounded-3 py-2" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Delete Modal -->
                                <div class="modal fade text-start" id="deleteModal{{ q.id }}" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content border-0 shadow rounded-4">
                                            <div class="modal-body p-4 text-center">
                                                <div class="text-danger mb-3">
                                                    <i class="bi bi-exclamation-octagon" style="font-size: 3rem;"></i>
                                                </div>
                                                <h4 class="fw-bold">{% trans "Delete Quotation?" %}</h4>
                                                <p class="text-muted">{% trans "Are you sure you want to delete this quotation? This action cannot be undone." %}</p>
                                                <div class="d-grid gap-2">
                                                    <a href="{% url 'delete_quotation' q.id %}" class="btn btn-danger rounded-3 py-2">{% trans "Yes, Delete" %}</a>
                                                    <button type="button" class="btn btn-light rounded-3 py-2" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <img src="https://illustrations.popsy.co/gray/work-from-home.svg" alt="Empty" style="width: 200px;" class="mb-3">
                                <p class="text-muted">{% trans "No quotations found." %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
                {% include "core/pagination.html" with page_obj=quotations %}
        </div>
    </div>
</div>
{% endblock %}