{% extends "layouts/base.html" %}
{% load i18n %}

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

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-2">
                    <li class="breadcrumb-item">
                        <a href="{% url 'px_sources:source_list' %}">{% trans "PX Sources" %}</a>
                    </li>
                    <li class="breadcrumb-item active" aria-current="page">
                        {{ source.name_en }}
                    </li>
                </ol>
            </nav>
            <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">
                {% if source.is_active %}
                    <span class="badge bg-success">{% trans "Active" %}</span>
                {% else %}
                    <span class="badge bg-secondary">{% trans "Inactive" %}</span>
                {% endif %}
            </p>
        </div>
        <div>
            <a href="{% url 'px_sources:source_list' %}" class="btn btn-outline-secondary me-2">
                <i class="bi bi-arrow-left me-1"></i> {% trans "Back to List" %}
            </a>
            {% if request.user.is_px_admin %}
            <a href="{% url 'px_sources:source_edit' source.pk %}" class="btn btn-primary me-2">
                <i class="bi bi-pencil me-1"></i> {% trans "Edit" %}
            </a>
            <a href="{% url 'px_sources:source_delete' source.pk %}" class="btn btn-danger">
                <i class="bi bi-trash me-1"></i> {% trans "Delete" %}
            </a>
            {% endif %}
        </div>
    </div>

    <!-- Detail Cards -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-info-circle me-2"></i>{% trans "Source Details" %}
                    </h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-8">
                            <h5>{% trans "Source Details" %}</h5>
                            <table class="table table-borderless">
                                <tr>
                                    <th width="30%">{% trans "Name (English)" %}</th>
                                    <td><strong>{{ source.name_en }}</strong></td>
                                </tr>
                                <tr>
                                    <th>{% trans "Name (Arabic)" %}</th>
                                    <td dir="rtl">{{ source.name_ar|default:"-" }}</td>
                                </tr>
                                <tr>
                                    <th>{% trans "Description" %}</th>
                                    <td>{{ source.description|default:"-"|linebreaks }}</td>
                                </tr>
                                <tr>
                                    <th>{% trans "Status" %}</th>
                                    <td>
                                        {% if source.is_active %}
                                            <span class="badge bg-success">{% trans "Active" %}</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{% trans "Inactive" %}</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <th>{% trans "Created" %}</th>
                                    <td>{{ source.created_at|date:"Y-m-d H:i" }}</td>
                                </tr>
                                <tr>
                                    <th>{% trans "Last Updated" %}</th>
                                    <td>{{ source.updated_at|date:"Y-m-d H:i" }}</td>
                                </tr>
                            </table>
                        </div>

                        <div class="col-md-4">
                            <h5>{% trans "Quick Actions" %}</h5>
                            <div class="list-group">
                                {% if request.user.is_px_admin %}
                                <a href="{% url 'px_sources:source_edit' source.pk %}" class="list-group-item list-group-item-action">
                                    <i class="fas fa-edit"></i> {% trans "Edit Source" %}
                                </a>
                                <a href="{% url 'px_sources:source_delete' source.pk %}" class="list-group-item list-group-item-action list-group-item-danger">
                                    <i class="fas fa-trash"></i> {% trans "Delete Source" %}
                                </a>
                                {% endif %}
                            </div>
                        </div>
                    </div>

                    <hr>

                    <h5>{% trans "Recent Usage" %} ({{ usage_records|length }})</h5>
                    {% if usage_records %}
                    <div class="table-responsive">
                        <table class="table table-striped table-sm">
                            <thead>
                                <tr>
                                    <th>{% trans "Date" %}</th>
                                    <th>{% trans "Content Type" %}</th>
                                    <th>{% trans "Object ID" %}</th>
                                    <th>{% trans "Hospital" %}</th>
                                    <th>{% trans "User" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for record in usage_records %}
                                <tr>
                                    <td>{{ record.created_at|date:"Y-m-d H:i" }}</td>
                                    <td><code>{{ record.content_type.model }}</code></td>
                                    <td>{{ record.object_id|truncatechars:20 }}</td>
                                    <td>{{ record.hospital.name_en|default:"-" }}</td>
                                    <td>{{ record.user.get_full_name|default:"-" }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <p class="text-muted">{% trans "No usage records found for this source." %}</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Source Users Section (PX Admin only) -->
    {% comment %} {% if request.user.is_px_admin %} {% endcomment %}
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-people-fill me-2"></i>
                        {% trans "Source Users" %} ({{ source_users|length }})
                    </h5>
                    <a href="{% url 'px_sources:source_user_create' source.pk %}" class="btn btn-sm btn-primary">
                        <i class="bi bi-plus-lg me-1"></i>{% trans "Add Source User" %}
                    </a>
                </div>
                <div class="card-body">
                    {% if source_users %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>{% trans "User" %}</th>
                                    <th>{% trans "Email" %}</th>
                                    <th>{% trans "Status" %}</th>
                                    <th>{% trans "Permissions" %}</th>
                                    <th>{% trans "Created" %}</th>
                                    <th>{% trans "Actions" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for su in source_users %}
                                <tr>
                                    <td>
                                        <strong>{{ su.user.get_full_name|default:"-" }}</strong>
                                    </td>
                                    <td>{{ su.user.email }}</td>
                                    <td>
                                        {% if su.is_active %}
                                            <span class="badge bg-success">{% trans "Active" %}</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{% trans "Inactive" %}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if su.can_create_complaints %}
                                            <span class="badge bg-primary me-1">{% trans "Complaints" %}</span>
                                        {% endif %}
                                        {% if su.can_create_inquiries %}
                                            <span class="badge bg-info">{% trans "Inquiries" %}</span>
                                        {% endif %}
                                        {% if not su.can_create_complaints and not su.can_create_inquiries %}
                                            <span class="text-muted">{% trans "None" %}</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ su.created_at|date:"Y-m-d" }}</td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'px_sources:source_user_edit' source.pk su.pk %}" 
                                               class="btn btn-outline-primary" 
                                               title="{% trans 'Edit' %}">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                            <a href="{% url 'px_sources:source_user_delete' source.pk su.pk %}" 
                                               class="btn btn-outline-danger" 
                                               title="{% trans 'Delete' %}">
                                                <i class="bi bi-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="bi bi-people fs-1 text-muted mb-3"></i>
                        <p class="text-muted mb-0">
                            {% trans "No source users assigned yet." %}
                            <a href="{% url 'px_sources:source_user_create' source.pk %}" class="text-primary">
                                {% trans "Add a source user" %}
                            </a>
                            {% trans "to get started." %}
                        </p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    {% comment %} {% endif %} {% endcomment %}
</div>
{% endblock %}
