{% extends "core/base.html" %}
{% load static %}
{% block title %}Role Permissions - SwiftPOS{% endblock %}
{% block topbar_title %}Staff Role Permissions{% endblock %}

{% block content %}
<div class="container-fluid py-4" style="max-width:1000px;">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h5 class="mb-0 fw-bold"><i class="fas fa-shield-halved me-2 text-primary"></i>Staff Role Permissions</h5>
            <p class="text-muted small mb-0">Configure what each staff role can do in your company. Admin always has full access.</p>
        </div>
    </div>

    {% if messages %}
    <div class="mb-3">
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
            {{ message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <form method="post">
        {% csrf_token %}
        <div class="row g-4">
            {% for role, perm_obj in perms.items %}
            {% if role != 'admin' %}
            <div class="col-md-6">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-header py-3" style="background:linear-gradient(135deg,{% if role == 'cashier' %}#4f46e5,#7c3aed{% else %}#0ea5e9,#0284c7{% endif %});border-radius:12px 12px 0 0;">
                        <h6 class="mb-0 text-white fw-bold">
                            {% if role == 'cashier' %}<i class="fas fa-cash-register me-2"></i>Cashier
                            {% elif role == 'manager' %}<i class="fas fa-id-badge me-2"></i>Manager
                            {% endif %}
                        </h6>
                    </div>
                    <div class="card-body">
                        <div class="d-flex flex-column gap-3">
                            {% for field, label in permission_fields %}
                            <div class="d-flex justify-content-between align-items-center py-1 border-bottom">
                                <label class="small fw-semibold mb-0 text-dark" for="id_{{ role }}_{{ field }}">{{ label }}</label>
                                <div class="form-check form-switch mb-0">
                                    <input class="form-check-input" type="checkbox"
                                           role="switch"
                                           name="{{ role }}_{{ field }}"
                                           id="id_{{ role }}_{{ field }}"
                                           style="cursor:pointer;"
                                           {% if field == 'can_delete_orders' and perm_obj.can_delete_orders %}checked{% endif %}
                                           {% if field == 'can_reverse_orders' and perm_obj.can_reverse_orders %}checked{% endif %}
                                           {% if field == 'can_change_price_at_pos' and perm_obj.can_change_price_at_pos %}checked{% endif %}
                                           {% if field == 'can_apply_discount' and perm_obj.can_apply_discount %}checked{% endif %}
                                           {% if field == 'can_view_profit' and perm_obj.can_view_profit %}checked{% endif %}
                                           {% if field == 'can_view_pnl' and perm_obj.can_view_pnl %}checked{% endif %}
                                           {% if field == 'can_view_analytics' and perm_obj.can_view_analytics %}checked{% endif %}
                                           {% if field == 'can_export_data' and perm_obj.can_export_data %}checked{% endif %}
                                           {% if field == 'can_manage_products' and perm_obj.can_manage_products %}checked{% endif %}
                                           {% if field == 'can_manage_customers' and perm_obj.can_manage_customers %}checked{% endif %}
                                           {% if field == 'can_change_settings' and perm_obj.can_change_settings %}checked{% endif %}>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}
            {% endfor %}
        </div>

        <!-- Admin always full access notice -->
        <div class="alert alert-light border mt-4 py-2 small">
            <i class="fas fa-info-circle text-primary me-1"></i>
            <strong>Admin</strong> role always has full access to all features and cannot be restricted.
        </div>

        <div class="mt-3 d-flex gap-2 justify-content-end">
            <a href="{% url 'analytics:insights' %}" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">
                <i class="fas fa-save me-1"></i> Save Permissions
            </button>
        </div>
    </form>
</div>
{% endblock %}
