{% extends 'layouts/base.html' %}
{% load i18n static %}

{% block title %}{% trans "Government Tickets" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    .page-header-gradient {
        background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
        color: white;
        padding: 1.5rem 2rem;
        border-radius: 1rem;
        margin-bottom: 1.5rem;
        box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
    }
    .section-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        overflow: hidden;
    }
    .section-header {
        padding: 1rem 1.5rem;
        border-bottom: 2px solid #e2e8f0;
        background: linear-gradient(to right, #f8fafc, #f1f5f9);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }
    .btn-transition {
        transition: all 0.2s ease-in-out;
    }
    .status-badge {
        display: inline-flex;
        align-items: center;
        padding: 0.25rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 500;
    }
    .status-pending { background: #fef3c7; color: #92400e; }
    .status-in_progress { background: #dbeafe; color: #1e40af; }
    .status-resolved { background: #d1fae5; color: #065f46; }
    .status-closed { background: #f3f4f6; color: #374151; }
</style>
{% endblock %}

{% block content %}
<div class="px-6 py-4">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="flex justify-between items-center">
            <div>
                <h1 class="text-2xl font-bold mb-1">{% trans "Government Source Tickets" %}</h1>
                <p class="text-blue-100 text-sm">{% trans "Manage tickets from MOH, CCHI, and other government sources" %}</p>
            </div>
            <div class="flex gap-2">
                <a href="{% url 'complaints:government_ticket_import' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-blue-500 text-white font-medium rounded-xl hover:bg-blue-600 transition">
                    <i data-lucide="upload" class="w-4 h-4 me-2"></i>{% trans "Import" %}
                </a>
                <a href="{% url 'complaints:government_ticket_export' %}?{% if source_filter %}source={{ source_filter }}&{% endif %}{% if status_filter %}status={{ status_filter }}&{% endif %}{% if department_filter %}department={{ department_filter }}&{% endif %}{% if search_query %}q={{ search_query|urlencode }}{% endif %}" 
                   class="btn-transition inline-flex items-center px-4 py-2.5 bg-purple-500 text-white font-medium rounded-xl hover:bg-purple-600 transition">
                    <i data-lucide="download" class="w-4 h-4 me-2"></i>{% trans "Export" %}
                </a>
                <a href="{% url 'complaints:government_ticket_create' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-green-500 text-white font-medium rounded-xl hover:bg-green-600 transition">
                    <i data-lucide="plus" class="w-4 h-4 me-2"></i>{% trans "Create Ticket" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="section-card mb-6">
        <div class="p-4">
            <form method="get" class="flex flex-wrap items-end gap-3">
                <div class="flex-1 min-w-[200px]">
                    <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Search" %}</label>
                    <input type="text" name="q" value="{{ search_query }}" placeholder="{% trans 'Ticket #, name, ID...' %}"
                           class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm focus:ring-2 focus:ring-navy focus:border-transparent">
                </div>
                <div class="min-w-[150px]">
                    <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Source" %}</label>
                    <select name="source" class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm focus:ring-2 focus:ring-navy focus:border-transparent">
                        <option value="">{% trans "All Sources" %}</option>
                        {% for source in sources %}
                        <option value="{{ source.id }}" {% if source_filter == source.id|stringformat:"s" %}selected{% endif %}>{{ source.name_en }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="min-w-[150px]">
                    <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Status" %}</label>
                    <select name="status" class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm focus:ring-2 focus:ring-navy focus:border-transparent">
                        <option value=""{% if not status_filter %}selected{% endif %}>{% trans "All Statuses" %}</option>
                        {% for value, label in status_choices %}
                        <option value="{{ value }}" {% if status_filter == value %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="min-w-[150px]">
                    <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Department" %}</label>
                    <select name="department" class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm focus:ring-2 focus:ring-navy focus:border-transparent">
                        <option value=""{% if not department_filter %}selected{% endif %}>{% trans "All Departments" %}</option>
                        {% for dept in departments %}
                        <option value="{{ dept.id }}" {% if department_filter == dept.id|stringformat:"s" %}selected{% endif %}>{{ dept.name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="min-w-[150px]">
                    <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Converted" %}</label>
                    <select name="converted" class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm focus:ring-2 focus:ring-navy focus:border-transparent">
                        <option value=""{% if not converted_filter %}selected{% endif %}>{% trans "All" %}</option>
                        <option value="yes" {% if converted_filter == "yes" %}selected{% endif %}>{% trans "Converted" %}</option>
                        <option value="no" {% if converted_filter == "no" %}selected{% endif %}>{% trans "Not Converted" %}</option>
                    </select>
                </div>
                <div class="flex gap-2">
                    <button type="submit" class="btn-transition px-4 py-2 bg-navy text-white rounded-lg hover:bg-blue transition">
                        <i data-lucide="filter" class="w-4 h-4"></i>
                    </button>
                    <a href="{% url 'complaints:government_ticket_list' %}" class="btn-transition px-4 py-2 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200 transition">
                        <i data-lucide="x" class="w-4 h-4"></i>
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Tickets Table -->
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-navy/10">
                <i data-lucide="building-2" class="w-5 h-5 text-navy"></i>
            </div>
            <h5 class="text-lg font-semibold text-gray-800">{% trans "Tickets" %} <span class="text-sm text-gray-500">({{ total_count }})</span></h5>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{% trans "Ticket #" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{% trans "Source" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{% trans "Complainant" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{% trans "Department" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{% trans "Received" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{% trans "Status" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{% trans "Converted" %}</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    {% for ticket in page_obj %}
                    <tr class="hover:bg-gray-50 transition">
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="inline-flex items-center px-3 py-1 rounded-lg text-xs font-semibold bg-navy/10 text-navy">
                                {{ ticket.ticket_number }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ ticket.source.name_en }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ ticket.complainant_name }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ ticket.department.name_en|default:"-" }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ ticket.received_date|date:"Y-m-d H:i" }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="status-badge status-{{ ticket.status }}">
                                {{ ticket.get_status_display }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            {% if ticket.converted_to_complaint %}
                            <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                <i data-lucide="check-circle" class="w-3 h-3 me-1"></i>{% trans "Yes" %}
                            </span>
                            {% else %}
                            <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-600">
                                <i data-lucide="minus-circle" class="w-3 h-3 me-1"></i>{% trans "No" %}
                            </span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right">
                            <div class="flex gap-2 justify-end">
                                <a href="{% url 'complaints:government_ticket_detail' pk=ticket.pk %}" 
                                   class="btn-transition inline-flex items-center px-3 py-1.5 bg-navy text-white text-xs font-medium rounded-lg hover:bg-blue transition">
                                    <i data-lucide="eye" class="w-3 h-3 me-1"></i>{% trans "View" %}
                                </a>
                                <a href="{% url 'complaints:government_ticket_update' pk=ticket.pk %}" 
                                   class="btn-transition inline-flex items-center px-3 py-1.5 border-2 border-navy text-navy text-xs font-medium rounded-lg hover:bg-navy hover:text-white transition">
                                    <i data-lucide="edit-2" class="w-3 h-3 me-1"></i>{% trans "Edit" %}
                                </a>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="8" class="px-6 py-12 text-center text-gray-500">
                            <i data-lucide="inbox" class="w-12 h-12 mx-auto mb-3 text-gray-300"></i>
                            <p>{% trans "No government tickets found" %}</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        {% if page_obj.has_other_pages %}
        <div class="px-6 py-4 border-t border-gray-200 flex justify-between items-center">
            <div class="text-sm text-gray-500">
                {% trans "Showing" %} {{ page_obj.start_index }} - {{ page_obj.end_index }} {% trans "of" %} {{ page_obj.paginator.count }}
            </div>
            <div class="flex gap-2">
                {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}{% if search_query %}&q={{ search_query|urlencode }}{% endif %}{% if source_filter %}&source={{ source_filter }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}" 
                   class="px-3 py-1.5 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200 transition text-sm">{% trans "Previous" %}</a>
                {% endif %}
                {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}{% if search_query %}&q={{ search_query|urlencode }}{% endif %}{% if source_filter %}&source={{ source_filter }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}" 
                   class="px-3 py-1.5 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200 transition text-sm">{% trans "Next" %}</a>
                {% endif %}
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}
