{% extends 'base.html' %}
{% load static %}
{% load permission_filters %}

{% block title %}Edit Permissions - {{ target_user.get_full_name }}{% endblock %}

{% block content %}
<div class="container-fluid px-4 py-6">
    <!-- Header -->
    <div class="mb-6">
        <div class="flex justify-between items-center">
            <div>
                <h1 class="text-2xl font-bold text-gray-900">Edit Permissions</h1>
                <p class="text-gray-600 mt-1">Managing permissions for: <strong>{{ target_user.get_full_name }}</strong> ({{ target_user.role|title }})</p>
                <p class="text-sm text-gray-500 mt-1">Logged in as: <strong>{{ user.get_full_name }}</strong> (Admin)</p>
            </div>
            <a href="{% url 'users:admin_list' %}" class="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                <span>←</span> Back to Staff List
            </a>
        </div>
    </div>

    <!-- User Info Card -->
    <div class="bg-white rounded-lg shadow p-6 mb-6">
        <div class="flex items-center space-x-4">
            <div class="flex-shrink-0">
                <div class="h-16 w-16 rounded-full bg-primary-100 flex items-center justify-center">
                    <span class="text-2xl font-bold text-primary-600">{{ target_user.first_name.0 }}{{ target_user.last_name.0 }}</span>
                </div>
            </div>
            <div>
                <h3 class="text-lg font-medium text-gray-900">{{ target_user.get_full_name }}</h3>
                <p class="text-sm text-gray-500">{{ target_user.email }}</p>
                <p class="text-sm text-gray-500">{{ target_user.phone_number }}</p>
                <span class="inline-flex mt-2 items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                    {{ target_user.role|title }}
                </span>
            </div>
        </div>
    </div>

    <!-- Messages -->
    {% if messages %}
        {% for message in messages %}
        <div class="mb-4 p-4 rounded-md {% if message.tags == 'error' %}bg-red-50 text-red-800{% else %}bg-green-50 text-green-800{% endif %}">
            <i class="fas {% if message.tags == 'error' %}fa-exclamation-circle{% else %}fa-check-circle{% endif %} mr-2"></i>
            {{ message }}
        </div>
        {% endfor %}
    {% endif %}

    <!-- Permissions Form -->
    <form method="post" action="">
        {% csrf_token %}
        
        <div class="bg-white rounded-lg shadow overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                <h3 class="text-lg font-medium text-gray-900">Module Permissions</h3>
                <p class="mt-1 text-sm text-gray-500">Select which modules and actions this user can access</p>
            </div>

            <div class="p-6">
                {% for module_name, actions in permissions.items %}
                <div class="mb-8 pb-8 border-b border-gray-200 last:border-b-0">
                    <h4 class="text-md font-semibold text-gray-900 mb-4 flex items-center">
                        <span>📦</span>
                        {{ module_name|title }}
                    </h4>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                        {% for action_code, perm_info in actions.items %}
                        {% comment %}Convert module name to code for field name - must match get_module_code() in view{% endcomment %}
                        {% with module_code=module_name|module_to_code %}
                        <div class="flex items-center p-3 border border-gray-200 rounded-lg hover:bg-gray-50">
                            <input type="checkbox" 
                                   name="perm_{{ module_code }}_{{ action_code }}"
                                   id="perm_{{ module_code }}_{{ action_code }}"
                                   {% if perm_info.allowed %}checked{% endif %}
                                   class="h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded cursor-pointer">
                            <label for="perm_{{ module_code }}_{{ action_code }}" class="ml-3 flex-1 cursor-pointer">
                                <span class="block text-sm font-medium text-gray-900">
                                    {{ perm_info.action_name }}
                                </span>
                                {% if perm_info.description %}
                                <span class="block text-xs text-gray-500">{{ perm_info.description|truncatewords:10 }}</span>
                                {% endif %}
                            </label>
                        </div>
                        {% endwith %}
                        {% endfor %}
                    </div>
                </div>
                {% empty %}
                <p class="text-center text-gray-500 py-8">No permissions configured for this role yet.</p>
                {% endfor %}
            </div>
        </div>

        <!-- Action Buttons -->
        <div class="mt-6" style="display: flex; justify-content: space-between; align-items: center;">
            <div>
                <button type="button" onclick="selectAll()" class="px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 bg-white hover:bg-gray-50" style="margin-right: 8px;">
                    <i class="fas fa-check-square mr-1"></i> Select All
                </button>
                <button type="button" onclick="deselectAll()" class="px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                    <i class="fas fa-square mr-1"></i> Deselect All
                </button>
            </div>
            <div style="display: flex; gap: 12px;">
                <a href="{% url 'users:admin_list' %}" class="px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 bg-white hover:bg-gray-50" style="text-decoration: none; display: inline-flex; align-items: center;">
                    Cancel
                </a>
                <button type="submit" id="save-permissions-btn" style="padding: 8px 24px; border: none; border-radius: 6px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); font-size: 14px; font-weight: 600; color: white; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); cursor: pointer; display: inline-flex; align-items: center;">
                    <span>💾</span> Save Permissions
                </button>
            </div>
        </div>
    </form>
</div>

<script>
function selectAll() {
    document.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
        checkbox.checked = true;
    });
}

function deselectAll() {
    document.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
        checkbox.checked = false;
    });
}

// Show loading state on form submit
document.addEventListener('DOMContentLoaded', function() {
    const form = document.querySelector('form[method="post"]');
    if (form) {
        form.addEventListener('submit', function() {
            const submitBtn = document.getElementById('save-permissions-btn');
            if (submitBtn) {
                submitBtn.disabled = true;
                submitBtn.innerHTML = '<span>⟳</span> Saving...';
            }
        });
    }
});
</script>
{% endblock %}

