<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <p:panel header="#{mode == 'create' ? 'Nouveau Rôle' : 'Modifier Rôle'}" 
             styleClass="w-full">
        
        <p:panelGrid columns="2" styleClass="w-full" columnClasses="col-12 md:col-6">
            
            <!-- Nom du rôle -->
            <p:outputLabel for="roleName" value="Nom du rôle *" />
            <p:inputText id="roleName" 
                       value="#{role.name}" 
                       required="true"
                       readonly="#{readonly}"
                       placeholder="ADMIN, USER, MODERATOR..."
                       styleClass="w-full">
                <f:validateLength minimum="2" maximum="100" />
                <f:validateRegex pattern="^[A-Z_]+$" />
            </p:inputText>
            
            <!-- Description -->
            <p:outputLabel for="description" value="Description" />
            <p:inputTextarea id="description" 
                           value="#{role.description}" 
                           readonly="#{readonly}"
                           placeholder="Description du rôle..."
                           rows="3"
                           styleClass="w-full" />
            
            <!-- Type de rôle -->
            <p:outputLabel for="typeRole" value="Type de rôle *" />
            <p:selectOneMenu id="typeRole" 
                           value="#{role.typeRole}" 
                           required="true"
                           readonly="#{readonly or mode == 'edit'}"
                           styleClass="w-full">
                <f:selectItem itemLabel="Sélectionner..." itemValue="" />
                <f:selectItem itemLabel="Rôle Realm" itemValue="REALM_ROLE" />
                <f:selectItem itemLabel="Rôle Client" itemValue="CLIENT_ROLE" />
                <f:selectItem itemLabel="Rôle Composite" itemValue="COMPOSITE_ROLE" />
            </p:selectOneMenu>
            
            <!-- Realm (si affiché) -->
            <c:if test="#{showRealmSelector}">
                <p:outputLabel for="realmName" value="Realm *" />
                <p:selectOneMenu id="realmName" 
                               value="#{role.realmName}" 
                               required="#{showRealmSelector}"
                               readonly="#{readonly}"
                               styleClass="w-full">
                    <f:selectItem itemLabel="Sélectionner..." itemValue="" />
                    <f:selectItems value="#{roleBean.availableRealms}" />
                </p:selectOneMenu>
            </c:if>
            
            <!-- Client (si affiché) -->
            <c:if test="#{showClientSelector}">
                <p:outputLabel for="clientId" value="Client *" />
                <p:selectOneMenu id="clientId" 
                               value="#{role.clientId}" 
                               required="#{showClientSelector}"
                               readonly="#{readonly}"
                               styleClass="w-full">
                    <f:selectItem itemLabel="Sélectionner..." itemValue="" />
                    <f:selectItems value="#{roleBean.availableClients}" />
                </p:selectOneMenu>
            </c:if>
            
            <!-- Rôle composite -->
            <c:if test="#{showCompositeOptions}">
                <p:outputLabel for="composite" value="Rôle composite" />
                <p:selectBooleanCheckbox id="composite" 
                                       value="#{role.composite}" 
                                       readonly="#{readonly}" />
            </c:if>
            
        </p:panelGrid>
        
        <!-- Boutons d'action -->
        <f:facet name="footer">
            <div class="flex gap-2 justify-content-end">
                <c:if test="#{not readonly}">
                    <c:choose>
                        <!-- Si hasSubmitAction est explicitement défini à true, utiliser action -->
                        <c:when test="#{hasSubmitAction == true}">
                            <p:commandButton 
                                value="#{mode == 'create' ? 'Créer' : 'Modifier'}"
                                icon="pi pi-check"
                                styleClass="p-button-success"
                                action="#{submitAction}"
                                update="#{not empty update ? update : '@form'}"
                                process="@form" />
                        </c:when>
                        <!-- Si submitOutcome est fourni, utiliser outcome -->
                        <c:when test="#{not empty submitOutcome}">
                            <p:commandButton 
                                value="#{mode == 'create' ? 'Créer' : 'Modifier'}"
                                icon="pi pi-check"
                                styleClass="p-button-success"
                                outcome="#{submitOutcome}"
                                update="#{not empty update ? update : '@form'}"
                                process="@form" />
                        </c:when>
                        <!-- Sinon, essayer d'utiliser submitAction si fourni -->
                        <c:otherwise>
                            <p:commandButton 
                                value="#{mode == 'create' ? 'Créer' : 'Modifier'}"
                                icon="pi pi-check"
                                styleClass="p-button-success"
                                action="#{submitAction}"
                                update="#{not empty update ? update : '@form'}"
                                process="@form" />
                        </c:otherwise>
                    </c:choose>
                </c:if>
                <p:commandButton 
                    value="Annuler"
                    icon="pi pi-times"
                    styleClass="p-button-secondary"
                    onclick="PF('createRealmRoleDialog').hide(); PF('createClientRoleDialog').hide();"
                    immediate="true" />
            </div>
        </f:facet>
    </p:panel>

</ui:composition>

