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

{% block title %}{{ title }} - PX 360{% endblock %}

{% block content %}
<div class="p-6">
    <!-- Header -->
    <div class="mb-6">
        <div class="flex items-center justify-between">
            <div class="flex items-center gap-4">
                <a href="{% url 'complaints:oncall_schedule_list' %}" class="text-[#64748b] hover:text-[#005696]">
                    <i data-lucide="arrow-left" class="w-6 h-6"></i>
                </a>
                <div>
                    <h1 class="text-2xl font-bold text-[#005696]">{{ title }}</h1>
                    <p class="text-[#64748b] mt-1">
                        {% if schedule.hospital %}{{ schedule.hospital.name }}{% else %}{% trans "System-wide Configuration" %}{% endif %}
                    </p>
                </div>
            </div>
            <div class="flex gap-3">
                <a href="{% url 'complaints:oncall_schedule_edit' schedule.id %}" 
                   class="px-4 py-2 border border-gray-200 text-gray-600 rounded-lg hover:bg-gray-50 transition-colors flex items-center gap-2">
                    <i data-lucide="edit" class="w-4 h-4"></i>
                    {% trans "Edit Schedule" %}
                </a>
                <a href="{% url 'complaints:oncall_admin_add' schedule.id %}" 
                   class="px-4 py-2 bg-[#005696] text-white rounded-lg hover:bg-[#007bbd] transition-colors flex items-center gap-2">
                    <i data-lucide="user-plus" class="w-4 h-4"></i>
                    {% trans "Add On-Call Admin" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Schedule Info Card -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 mb-6">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
            <div>
                <p class="text-sm text-[#64748b] mb-1">{% trans "Scope" %}</p>
                <p class="font-medium text-gray-900">
                    {% if schedule.hospital %}{{ schedule.hospital.name }}{% else %}<span class="text-[#005696]">{% trans "System-wide" %}</span>{% endif %}
                </p>
            </div>
            <div>
                <p class="text-sm text-[#64748b] mb-1">{% trans "Working Hours" %}</p>
                <p class="font-medium text-gray-900">
                    {{ schedule.work_start_time|time:"H:i" }} - {{ schedule.work_end_time|time:"H:i" }}
                </p>
            </div>
            <div>
                <p class="text-sm text-[#64748b] mb-1">{% trans "Working Days" %}</p>
                <p class="font-medium text-gray-900 text-sm">
                    {% for day in schedule.get_working_days_list %}
                        {% if day == 0 %}{% trans "Mon" %}{% elif day == 1 %}{% trans "Tue" %}{% elif day == 2 %}{% trans "Wed" %}{% elif day == 3 %}{% trans "Thu" %}{% elif day == 4 %}{% trans "Fri" %}{% elif day == 5 %}{% trans "Sat" %}{% elif day == 6 %}{% trans "Sun" %}{% endif %}{% if not forloop.last %}, {% endif %}
                    {% endfor %}
                </p>
            </div>
            <div>
                <p class="text-sm text-[#64748b] mb-1">{% trans "Current Status" %}</p>
                {% if is_working_hours %}
                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                        <span class="w-2 h-2 bg-green-500 rounded-full mr-1.5"></span>
                        {% trans "Working Hours" %}
                    </span>
                {% else %}
                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-amber-100 text-amber-800">
                        <span class="w-2 h-2 bg-amber-500 rounded-full mr-1.5"></span>
                        {% trans "After Hours" %}
                    </span>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- On-Call Admins Table -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="px-6 py-4 border-b border-gray-100 flex items-center justify-between">
            <h2 class="text-lg font-semibold text-[#005696] flex items-center gap-2">
                <i data-lucide="users" class="w-5 h-5"></i>
                {% trans "On-Call Admins" %}
            </h2>
            <span class="text-sm text-[#64748b]">{{ on_call_admins.count }} {% trans "assigned" %}</span>
        </div>
        
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-[#64748b] uppercase tracking-wider">{% trans "Admin" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-[#64748b] uppercase tracking-wider">{% trans "Active Period" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-[#64748b] uppercase tracking-wider">{% trans "Priority" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-[#64748b] uppercase tracking-wider">{% trans "Contact" %}</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-[#64748b] uppercase tracking-wider">{% trans "Status" %}</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-[#64748b] uppercase tracking-wider">{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    {% for admin in on_call_admins %}
                    <tr class="hover:bg-gray-50">
                        <td class="px-6 py-4">
                            <div class="flex items-center gap-3">
                                <div class="w-10 h-10 bg-[#eef6fb] rounded-full flex items-center justify-center">
                                    <span class="text-[#005696] font-medium">
                                        {{ admin.admin_user.first_name|first|default:admin.admin_user.email|first|upper }}
                                    </span>
                                </div>
                                <div>
                                    <p class="font-medium text-gray-900">
                                        {{ admin.admin_user.get_full_name|default:admin.admin_user.email }}
                                    </p>
                                    <p class="text-sm text-[#64748b]">{{ admin.admin_user.email }}</p>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            {% if admin.start_date and admin.end_date %}
                                <span class="text-sm text-gray-900">{{ admin.start_date }} {% trans "to" %} {{ admin.end_date }}</span>
                            {% elif admin.start_date %}
                                <span class="text-sm text-gray-900">{% trans "From" %} {{ admin.start_date }}</span>
                            {% elif admin.end_date %}
                                <span class="text-sm text-gray-900">{% trans "Until" %} {{ admin.end_date }}</span>
                            {% else %}
                                <span class="text-sm text-green-600">{% trans "Permanent" %}</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
                                {{ admin.notification_priority }}
                            </span>
                        </td>
                        <td class="px-6 py-4">
                            <div class="flex gap-2">
                                {% if admin.notify_email %}
                                    <span class="text-xs bg-blue-50 text-blue-700 px-2 py-1 rounded">{% trans "Email" %}</span>
                                {% endif %}
                                {% if admin.notify_sms %}
                                    <span class="text-xs bg-green-50 text-green-700 px-2 py-1 rounded">{% trans "SMS" %}</span>
                                {% endif %}
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            {% if admin.is_active %}
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                    {% trans "Active" %}
                                </span>
                            {% else %}
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
                                    {% trans "Inactive" %}
                                </span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 text-right">
                            <div class="flex items-center justify-end gap-2">
                                <a href="{% url 'complaints:oncall_admin_edit' admin.id %}" 
                                   class="text-[#005696] hover:text-[#007bbd]">
                                    <i data-lucide="edit" class="w-4 h-4"></i>
                                </a>
                                <form method="post" action="{% url 'complaints:oncall_admin_delete' admin.id %}" 
                                      class="inline" onsubmit="return confirm('{% trans "Are you sure you want to remove this admin from on-call?" %}')">
                                    {% csrf_token %}
                                    <button type="submit" class="text-red-600 hover:text-red-800">
                                        <i data-lucide="trash-2" class="w-4 h-4"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="px-6 py-8 text-center text-[#64748b]">
                            <i data-lucide="users" class="w-12 h-12 mx-auto mb-3 text-gray-300"></i>
                            <p>{% trans "No on-call admins assigned yet." %}</p>
                            <a href="{% url 'complaints:oncall_admin_add' schedule.id %}" 
                               class="text-[#005696] hover:text-[#007bbd] font-medium mt-2 inline-block">
                                {% trans "Add your first on-call admin" %}
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- Delete Schedule -->
    <div class="mt-6 flex justify-end">
        <form method="post" action="{% url 'complaints:oncall_schedule_delete' schedule.id %}" 
              onsubmit="return confirm('{% trans "Are you sure you want to delete this schedule? All on-call admin assignments will be removed." %}')">
            {% csrf_token %}
            <button type="submit" class="px-4 py-2 text-red-600 border border-red-200 rounded-lg hover:bg-red-50 transition-colors flex items-center gap-2">
                <i data-lucide="trash-2" class="w-4 h-4"></i>
                {% trans "Delete Schedule" %}
            </button>
        </form>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        lucide.createIcons();
    });
</script>
{% endblock %}
