{% extends "layouts/source_user_base.html" %}
{% load i18n action_icons %}

{% block title %}{% trans "Source User Dashboard" %} - {{ source.name_en }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-lightning-fill text-warning me-2"></i>
                {{ source.name_en }}
            </h2>
            <p class="text-muted mb-0">
                {% trans "Welcome" %}, {{ request.user.get_full_name }}!
                {% trans "You're managing feedback from this source." %}
            </p>
        </div>
       
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <h6 class="card-title">{% trans "Total Complaints" %}</h6>
                    <h2 class="mb-0">{{ total_complaints }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-dark">
                <div class="card-body">
                    <h6 class="card-title">{% trans "Open Complaints" %}</h6>
                    <h2 class="mb-0">{{ open_complaints }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <h6 class="card-title">{% trans "Total Inquiries" %}</h6>
                    <h2 class="mb-0">{{ total_inquiries }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-secondary text-white">
                <div class="card-body">
                    <h6 class="card-title">{% trans "Open Inquiries" %}</h6>
                    <h2 class="mb-0">{{ open_inquiries }}</h2>
                </div>
            </div>
        </div>
    </div>

    
    <!-- Complaints Table -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        {% action_icon 'filter' %} {% trans "Recent Complaints" %} ({{ complaints|length }})
                    </h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>{% trans "ID" %}</th>
                                    <th>{% trans "Title" %}</th>
                                    <th>{% trans "Patient" %}</th>
                                    <th>{% trans "Category" %}</th>
                                    <th>{% trans "Status" %}</th>
                                    <th>{% trans "Priority" %}</th>
                                    <th>{% trans "Created" %}</th>
                                    <th>{% trans "Actions" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for complaint in complaints %}
                                <tr>
                                    <td><code>{{ complaint.id|slice:":8" }}</code></td>
                                    <td>{{ complaint.title|truncatewords:8 }}</td>
                                    <td>{{ complaint.patient.get_full_name }}</td>
                                    <td>{{ complaint.get_category_display }}</td>
                                    <td>
                                        {% if complaint.status == 'open' %}
                                            <span class="badge bg-danger">{% trans "Open" %}</span>
                                        {% elif complaint.status == 'in_progress' %}
                                            <span class="badge bg-warning text-dark">{% trans "In Progress" %}</span>
                                        {% elif complaint.status == 'resolved' %}
                                            <span class="badge bg-success">{% trans "Resolved" %}</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{% trans "Closed" %}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if complaint.priority == 'high' %}
                                            <span class="badge bg-danger">{% trans "High" %}</span>
                                        {% elif complaint.priority == 'medium' %}
                                            <span class="badge bg-warning text-dark">{% trans "Medium" %}</span>
                                        {% else %}
                                            <span class="badge bg-success">{% trans "Low" %}</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ complaint.created_at|date:"Y-m-d" }}</td>
                                    <td>
                                        <a href="{% url 'complaints:complaint_detail' complaint.pk %}" 
                                           class="btn btn-sm btn-info" 
                                           title="{% trans 'View' %}">
                                            {% action_icon 'view' %}
                                        </a>
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="8" class="text-center py-4">
                                        <p class="text-muted mb-2">
                                            <i class="bi bi-inbox fs-1"></i>
                                        </p>
                                        <p>{% trans "No complaints found for this source." %}</p>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Inquiries Table -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        {% action_icon 'filter' %} {% trans "Recent Inquiries" %} ({{ inquiries|length }})
                    </h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>{% trans "ID" %}</th>
                                    <th>{% trans "Subject" %}</th>
                                    <th>{% trans "Patient" %}</th>
                                    <th>{% trans "Category" %}</th>
                                    <th>{% trans "Status" %}</th>
                                    <th>{% trans "Created" %}</th>
                                    <th>{% trans "Actions" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for inquiry in inquiries %}
                                <tr>
                                    <td><code>{{ inquiry.id|slice:":8" }}</code></td>
                                    <td>{{ inquiry.subject|truncatewords:8 }}</td>
                                    <td>
                                        {% if inquiry.patient %}
                                            {{ inquiry.patient.get_full_name }}
                                        {% else %}
                                            {{ inquiry.contact_name|default:"-" }}
                                        {% endif %}
                                    </td>
                                    <td>{{ inquiry.get_category_display }}</td>
                                    <td>
                                        {% if inquiry.status == 'open' %}
                                            <span class="badge bg-danger">{% trans "Open" %}</span>
                                        {% elif inquiry.status == 'in_progress' %}
                                            <span class="badge bg-warning text-dark">{% trans "In Progress" %}</span>
                                        {% elif inquiry.status == 'resolved' %}
                                            <span class="badge bg-success">{% trans "Resolved" %}</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{% trans "Closed" %}</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ inquiry.created_at|date:"Y-m-d" }}</td>
                                    <td>
                                        <a href="{% url 'complaints:inquiry_detail' inquiry.pk %}" 
                                           class="btn btn-sm btn-info" 
                                           title="{% trans 'View' %}">
                                            {% action_icon 'view' %}
                                        </a>
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="7" class="text-center py-4">
                                        <p class="text-muted mb-2">
                                            <i class="bi bi-inbox fs-1"></i>
                                        </p>
                                        <p>{% trans "No inquiries found for this source." %}</p>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}