{% load static i18n %}
{% get_current_language as LANGUAGE_CODE %}

<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}" dir="{% if LANGUAGE_CODE == 'ar' %}rtl{% else %}ltr{% endif %}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="{% trans 'King Abdullah Academic University Hospital - Agency Portal' %}">
    <title>{% block title %}{% trans 'KAAUH Agency Portal' %}{% endblock %}</title>

    {# Load correct Bootstrap CSS file for RTL/LTR #}
    {% if LANGUAGE_CODE == 'ar' %}
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    {% else %}
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    {% endif %}

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" integrity="sha512-Kc323vGBEqzTmouAECnVceyQqyqdsSiqLQISBL29aUW4U/M7pSPA/gEUZQqv1cwx4OnYxTxve5UMg5GT6L4JJg==" crossorigin="anonymous" referrerpolicy="no-referrer" />

    <link rel="stylesheet" href="{% static 'css/main.css' %}">

    {% block customCSS %}{% endblock %}
</head>
<body class="d-flex flex-column min-vh-100" hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>

    <div class="top-bar d-none d-md-block" style="background-color: #f8f9fa;">
        <div class="container-fluid">
            <div class="d-flex justify-content-between align-items-center gap-2" style="max-width: 1600px; margin: 0 auto; padding: 0.5rem 0;">
                <div class="logo-group d-flex gap-3 align-items-center">
                    <img src="{% static 'image/vision.svg' %}" alt="{% trans 'Saudi Vision 2030' %}" loading="lazy" style="height: 35px; object-fit: contain;">
                </div>
                <div class="hospital-info d-flex gap-2 align-items-center">
                    <div class="hospital-text text-center text-md-end">
                        <div class="small fw-semibold" style="color: #004a53;">
                            {% if LANGUAGE_CODE == 'ar' %}
                                جامعة الأميرة نورة بنت عبدالرحمن الأكاديمية
                                <br>
                                ومستشفى الملك عبدالله بن عبدالعزيز التخصصي
                            {% else %}
                                Princess Nourah bint Abdulrahman University
                                <br>
                                King Abdullah bin Abdulaziz University Hospital
                            {% endif %}
                        </div>
                    </div>
                    <img src="{% static 'image/kaauh.png' %}" alt="KAAUH Logo" style="max-height: 40px; max-width: 40px;">
                </div>
            </div>
        </div>
    </div>

    {# Using inline style for nav background color - replace with a dedicated CSS class (e.g., .bg-kaauh-nav) if defined in main.css #}
    <div style="background-color: #00636e;">
        <nav class="navbar navbar-expand-lg navbar-dark sticky-top">
            <div class="container-fluid" style="max-width: 1600px;">
            {% if request.user.user_type == 'candidate' %}
                <a class="navbar-brand text-white" href="{% url 'candidate_portal_dashboard' %}" aria-label="Applicant Dashboard">
                    <img src="{% static 'image/kaauh_green1.png' %}" alt="{% trans 'kaauh logo green bg' %}" style="width: 40px; height: 40px;">
                    <span class="ms-3 d-none d-md-inline fw-semibold">{% trans "Applicant Portal" %}</span>
                </a>
            {% elif request.user.user_type == 'agency' %}
                <a class="navbar-brand text-white" href="{% url 'agency_portal_dashboard' %}" aria-label="Agency Dashboard">
                    <img src="{% static 'image/kaauh_green1.png' %}" alt="{% trans 'kaauh logo green bg' %}" style="width: 40px; height: 40px;">
                    <span class="ms-3 d-none d-md-inline fw-semibold">{% trans "Agency Portal" %}</span>
                </a>
            {% endif %}


                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#agencyNavbar"
                    aria-controls="agencyNavbar" aria-expanded="false" aria-label="{% trans 'Toggle navigation' %}">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse" id="agencyNavbar">
                    <div class="navbar-nav ms-auto">

                        {# NAVIGATION LINKS (Add your portal links here if needed) #}
                        {% if request.user.user_type == 'agency' %}
                            <li class="nav-item">
                                <a class="nav-link text-white" href="{% url 'agency_portal_dashboard' %}">
                                    <i class="fas fa-tachometer-alt me-1"></i> {% trans "Dashboard" %}
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" href="{% url 'agency_portal_persons_list' %}">
                                    <i class="fas fa-users me-1"></i> {% trans "Applicants" %}
                                </a>
                            </li>
                        {% elif request.user.user_type == 'candidate' %}
                            <li class="nav-item">
                                <a class="nav-link text-white" href="{% url 'candidate_portal_dashboard' %}">
                                    <i class="fas fa-tachometer-alt me-1"></i> {% trans "Dashboard" %}
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" href="{% url 'kaauh_career' %}">
                                    <i class="fas fa-hand me-1"></i> {% trans "KAAUH Careers" %}
                                </a>
                            </li>
                        {% endif %}

                        
                        {% if request.user.is_authenticated %}
                            <li class="nav-item">
                                <a class="nav-link text-white" href="{% url 'user_detail' request.user.pk %}">
                                    <i class="fas fa-user-circle me-1"></i> <span>{% trans "My Profile" %}</span></a></li>
                        {% endif %}
                        <li class="nav-item me-2">
                        <a class="nav-link text-white" href="{% url 'message_list' %}">
                            <i class="fas fa-envelope"></i> <span>{% trans "Messages" %}</span>
                            </a>
                        </li>


                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle text-white" href="#" role="button" data-bs-toggle="dropdown"
                            data-bs-offset="0, 8" aria-expanded="false" aria-label="{% trans 'Toggle language menu' %}">
                                <i class="fas fa-globe me-1"></i>
                                <span class="d-none d-lg-inline">{{ LANGUAGE_CODE|upper }}</span>
                            </a>
                            <ul class="dropdown-menu {% if LANGUAGE_CODE == 'ar' %}dropdown-menu-start{% else %}dropdown-menu-end{% endif %}" data-bs-popper="static">
                                <li>
                                    <form action="/i18n/setlang/" method="post" class="d-inline">{% csrf_token %}
                                        <input name="next" type="hidden" value="{{ request.get_full_path }}">
                                        <button name="language" value="en" class="dropdown-item {% if LANGUAGE_CODE == 'en' %}active bg-light-subtle{% endif %}" type="submit">
                                            <span class="me-2">🇺🇸</span> English
                                        </button>
                                    </form>
                                </li>
                                <li>
                                    <form action="/i18n/setlang/" method="post" class="d-inline">{% csrf_token %}
                                        <input name="next" type="hidden" value="{{ request.get_full_path }}">
                                        <button name="language" value="ar" class="dropdown-item {% if LANGUAGE_CODE == 'ar' %}active bg-light-subtle{% endif %}" type="submit">
                                            <span class="me-2">🇸🇦</span> العربية (Arabic)
                                        </button>
                                    </form>
                                </li>
                            </ul>
                        </li>

                        <li class="nav-item ms-3">
                            {% if request.user.is_authenticated %}
                                <form method="post" action="{% url 'account_logout' %}" class="d-inline py-2 d-flex align-items-center">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-outline-light btn-sm">
                                        <i class="fas fa-sign-out-alt me-1"></i> {% trans "Logout" %}
                                    </button>
                                </form>
                            {% endif %}
                        </li>
                    </div>
                </div>
            </div>
        </nav>
    </div>

    <main id="message-container" class="container-fluid flex-grow-1" style="max-width: 1600px; margin: 0 auto;">
        {# Messages Block (Correct) #}
        {% if messages %}
            {% for message in messages %}
                <div class="alert alert-{{ message.tags }} alert-dismissible fade show mt-2" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="{% trans 'Close' %}"></button>
                </div>
            {% endfor %}
        {% endif %}
        {% block content %}
        {% endblock %}
    </main>

    {# Footer (Correct) #}
    <footer class="mt-auto">
        <div class="footer-bottom py-3 small text-muted" style="background-color: #00363a;">
            <div class="container-fluid">
                <div class="d-flex justify-content-between align-items-center flex-wrap" style="max-width: 1600px; margin: 0 auto;">
                    <p class="mb-0 text-white-50">
                        &copy; {% now "Y" %} {% trans "King Abdullah Academic University Hospital (KAAUH)." %}
                        {% trans "All rights reserved." %}
                    </p>
                    <p class="mb-0 text-white-50">
                        {% if request.user.user_type == 'candidate' %}
                            {% trans "Candidate Portal" %}
                        {% elif request.user.user_type == 'agency' %}
                            {% trans "Agency Portal" %}
                        {% endif %}
                    </p>
                </div>
            </div>
        </div>
    </footer>

    {% include 'includes/delete_modal.html' %}

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.7/dist/htmx.min.js"></script>
    <script type="module" src="https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.0-RC.6/bundles/datastar.js"></script>

    {# JavaScript (Left unchanged as it was mostly correct) #}
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            // Navbar collapse auto-close on link click (Mobile UX)
            const navbarCollapse = document.getElementById('agencyNavbar');
            if (navbarCollapse) {
                const navLinks = navbarCollapse.querySelectorAll('.nav-link:not(.dropdown-toggle), .dropdown-item');
                const bsCollapse = bootstrap.Collapse.getInstance(navbarCollapse) || new bootstrap.Collapse(navbarCollapse, { toggle: false });

                navLinks.forEach(link => {
                    link.addEventListener('click', () => {
                        if (navbarCollapse.classList.contains('show')) {
                            if (!link.classList.contains('dropdown-toggle')) {
                                bsCollapse.hide();
                            }
                        }
                    });
                });
            }

            // Mobile logout confirmation
            const logoutButton = document.querySelector('form[action*="logout"] button');
            if (logoutButton) {
                logoutButton.addEventListener('click', (e) => {
                    if (window.innerWidth < 992) {
                        const confirmed = confirm('{% trans "Are you sure you want to logout?" %}');
                        if (!confirmed) e.preventDefault();
                    }
                });
            }
        });

        function form_loader(){
            const forms = document.querySelectorAll('form');
            forms.forEach(form => {
                form.addEventListener('submit', function(e) {
                    const submitButton = form.querySelector('button[type="submit"], input[type="submit"]');
                    if (submitButton) {
                        submitButton.disabled = true;
                        submitButton.classList.add('loading');
                        window.addEventListener('unload', function() {
                            submitButton.disabled = false;
                            submitButton.classList.remove('loading');
                        });
                    }
                });
            });
        }

        try {
            document.addEventListener('htmx:afterSwap', form_loader);
        } catch(e) {
            console.error(e);
        }
    </script>


    {% block customJS %}{% endblock %}
</body>
</html>
