<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">

    <!--
        Composant réutilisable: Actions Utilisateur (WOU/DRY Pattern)
        
        Auteur: Lions User Manager
        Version: 1.0.0
        Description: Boutons d'action pour un utilisateur (activate, deactivate, delete, etc.)
        
        Paramètres:
        - user: UserDTO (requis) - L'utilisateur concerné
        - showView: Boolean (défaut: true) - Afficher le bouton "Voir"
        - showEdit: Boolean (défaut: true) - Afficher le bouton "Modifier"
        - showDelete: Boolean (défaut: true) - Afficher le bouton "Supprimer"
        - showActivate: Boolean (défaut: true) - Afficher le bouton "Activer"
        - showDeactivate: Boolean (défaut: true) - Afficher le bouton "Désactiver"
        - showResetPassword: Boolean (défaut: true) - Afficher le bouton "Réinitialiser mot de passe"
        - showLogoutSessions: Boolean (défaut: false) - Afficher le bouton "Déconnecter toutes les sessions"
        - viewAction: String (optionnel) - Action pour "Voir"
        - editAction: String (optionnel) - Action pour "Modifier"
        - deleteAction: String (optionnel) - Action pour "Supprimer"
        - activateAction: String (optionnel) - Action pour "Activer"
        - deactivateAction: String (optionnel) - Action pour "Désactiver"
        - resetPasswordAction: String (optionnel) - Action pour "Réinitialiser mot de passe"
        - logoutSessionsAction: String (optionnel) - Action pour "Déconnecter sessions"
        - viewOutcome: String (optionnel) - Page pour "Voir"
        - editOutcome: String (optionnel) - Page pour "Modifier"
        - update: String (défaut: "@form") - Composants à mettre à jour
        - layout: String (défaut: "horizontal") - Layout: "horizontal" ou "vertical" ou "dropdown"
        
        Exemples d'utilisation:
        
        1. Actions horizontales (défaut):
        <ui:include src="/templates/components/user-management/user-actions.xhtml">
            <ui:param name="user" value="#{user}" />
            <ui:param name="update" value="userTable" />
        </ui:include>
        
        2. Actions en dropdown:
        <ui:include src="/templates/components/user-management/user-actions.xhtml">
            <ui:param name="user" value="#{user}" />
            <ui:param name="layout" value="dropdown" />
            <ui:param name="update" value="userTable" />
        </ui:include>
        
        3. Actions limitées:
        <ui:include src="/templates/components/user-management/user-actions.xhtml">
            <ui:param name="user" value="#{user}" />
            <ui:param name="showDelete" value="false" />
            <ui:param name="showResetPassword" value="false" />
        </ui:include>
    -->
    
    <c:set var="update" value="#{empty update ? '@form' : update}" />
    <c:set var="layout" value="#{empty layout ? 'horizontal' : layout}" />
    <c:set var="showView" value="#{empty showView ? true : showView}" />
    <c:set var="showEdit" value="#{empty showEdit ? true : showEdit}" />
    <c:set var="showDelete" value="#{empty showDelete ? true : showDelete}" />
    <c:set var="showActivate" value="#{empty showActivate ? true : showActivate}" />
    <c:set var="showDeactivate" value="#{empty showDeactivate ? true : showDeactivate}" />
    <c:set var="showResetPassword" value="#{empty showResetPassword ? true : showResetPassword}" />
    <c:set var="showLogoutSessions" value="#{empty showLogoutSessions ? false : showLogoutSessions}" />
    
    <c:choose>
        <!-- Layout Dropdown -->
        <c:when test="#{layout == 'dropdown'}">
            <p:commandButton 
                icon="pi pi-ellipsis-v"
                styleClass="p-button-text p-button-sm"
                type="button">
                <p:menu>
                    <c:if test="#{showView}">
                        <p:menuitem 
                            value="Voir le profil"
                            icon="pi pi-eye"
                            outcome="#{not empty viewOutcome ? viewOutcome : '/pages/user-manager/users/profile'}">
                            <f:param name="userId" value="#{user.id}" />
                        </p:menuitem>
                    </c:if>
                    
                    <c:if test="#{showEdit}">
                        <p:menuitem 
                            value="Modifier"
                            icon="pi pi-pencil"
                            outcome="#{not empty editOutcome ? editOutcome : '/pages/user-manager/users/edit'}">
                            <f:param name="userId" value="#{user.id}" />
                        </p:menuitem>
                    </c:if>
                    
                    <c:if test="#{showResetPassword}">
                        <p:menuitem 
                            value="Réinitialiser mot de passe"
                            icon="pi pi-key"
                            onclick="PF('resetPasswordDialog').show()" />
                    </c:if>
                    
                    <p:separator />
                    
                    <c:if test="#{showActivate and not user.enabled}">
                        <p:menuitem 
                            value="Activer"
                            icon="pi pi-check"
                            styleClass="text-green-600"
                            action="#{not empty activateAction ? activateAction : userBean.activateUser(user.id)}"
                            update="#{update}" />
                    </c:if>
                    
                    <c:if test="#{showDeactivate and user.enabled}">
                        <p:menuitem 
                            value="Désactiver"
                            icon="pi pi-times"
                            styleClass="text-orange-600"
                            action="#{not empty deactivateAction ? deactivateAction : userBean.deactivateUser(user.id)}"
                            update="#{update}" />
                    </c:if>
                    
                    <c:if test="#{showLogoutSessions}">
                        <p:menuitem 
                            value="Déconnecter toutes les sessions"
                            icon="pi pi-sign-out"
                            styleClass="text-blue-600"
                            action="#{not empty logoutSessionsAction ? logoutSessionsAction : userBean.logoutAllSessions(user.id)}"
                            update="#{update}" />
                    </c:if>
                    
                    <c:if test="#{showDelete}">
                        <p:separator />
                        <p:menuitem 
                            value="Supprimer"
                            icon="pi pi-trash"
                            styleClass="text-red-600"
                            onclick="PF('confirmDeleteDialog').show()" />
                    </c:if>
                </p:menu>
            </p:commandButton>
        </c:when>
        
        <!-- Layout Horizontal (défaut) -->
        <c:otherwise>
            <div class="flex gap-1">
                <c:if test="#{showView}">
                    <p:commandButton 
                        icon="pi pi-eye"
                        title="Voir le profil"
                        styleClass="p-button-text p-button-sm p-button-info"
                        outcome="#{not empty viewOutcome ? viewOutcome : '/pages/user-manager/users/profile'}">
                        <f:param name="userId" value="#{user.id}" />
                    </p:commandButton>
                </c:if>
                
                <c:if test="#{showEdit}">
                    <p:commandButton 
                        icon="pi pi-pencil"
                        title="Modifier"
                        styleClass="p-button-text p-button-sm p-button-warning"
                        outcome="#{not empty editOutcome ? editOutcome : '/pages/user-manager/users/edit'}">
                        <f:param name="userId" value="#{user.id}" />
                    </p:commandButton>
                </c:if>
                
                <c:if test="#{showResetPassword}">
                    <p:commandButton 
                        icon="pi pi-key"
                        title="Réinitialiser mot de passe"
                        styleClass="p-button-text p-button-sm p-button-help"
                        onclick="PF('resetPasswordDialog').show()" />
                </c:if>
                
                <c:if test="#{showActivate and not user.enabled}">
                    <p:commandButton 
                        icon="pi pi-check"
                        title="Activer"
                        styleClass="p-button-text p-button-sm p-button-success"
                        action="#{not empty activateAction ? activateAction : userBean.activateUser(user.id)}"
                        update="#{update}" />
                </c:if>
                
                <c:if test="#{showDeactivate and user.enabled}">
                    <p:commandButton 
                        icon="pi pi-times"
                        title="Désactiver"
                        styleClass="p-button-text p-button-sm p-button-warning"
                        action="#{not empty deactivateAction ? deactivateAction : userBean.deactivateUser(user.id)}"
                        update="#{update}" />
                </c:if>
                
                <c:if test="#{showLogoutSessions}">
                    <p:commandButton 
                        icon="pi pi-sign-out"
                        title="Déconnecter toutes les sessions"
                        styleClass="p-button-text p-button-sm p-button-info"
                        action="#{not empty logoutSessionsAction ? logoutSessionsAction : userBean.logoutAllSessions(user.id)}"
                        update="#{update}" />
                </c:if>
                
                <c:if test="#{showDelete}">
                    <p:commandButton 
                        icon="pi pi-trash"
                        title="Supprimer"
                        styleClass="p-button-text p-button-sm p-button-danger"
                        onclick="PF('confirmDeleteDialog').show()" />
                </c:if>
            </div>
        </c:otherwise>
    </c:choose>
    
    <!-- Dialog de confirmation de suppression -->
    <p:confirmDialog 
        id="confirmDeleteDialog"
        widgetVar="confirmDeleteDialog"
        message="Êtes-vous sûr de vouloir supprimer l'utilisateur #{user.username} ?"
        header="Confirmation de suppression"
        severity="warn">
        <p:commandButton 
            value="Oui"
            icon="pi pi-check"
            styleClass="p-button-danger"
            action="#{not empty deleteAction ? deleteAction : userBean.deleteUser(user.id)}"
            update="#{update}"
            oncomplete="PF('confirmDeleteDialog').hide()" />
        <p:commandButton 
            value="Non"
            icon="pi pi-times"
            styleClass="p-button-secondary"
            onclick="PF('confirmDeleteDialog').hide()" />
    </p:confirmDialog>
    
    <!-- Dialog de réinitialisation de mot de passe -->
    <p:dialog 
        id="resetPasswordDialog"
        widgetVar="resetPasswordDialog"
        header="Réinitialiser le mot de passe"
        modal="true"
        styleClass="w-full md:w-4">
        <h:form>
            <p:panelGrid columns="2" styleClass="w-full">
                <p:outputLabel for="newPassword" value="Nouveau mot de passe *" />
                <p:password id="newPassword" 
                          value="#{userBean.newPassword}" 
                          feedback="true"
                          required="true"
                          styleClass="w-full">
                    <f:validateLength minimum="8" maximum="100" />
                </p:password>
                
                <p:outputLabel for="newPasswordConfirm" value="Confirmer *" />
                <p:password id="newPasswordConfirm" 
                          value="#{userBean.newPasswordConfirm}" 
                          required="true"
                          styleClass="w-full" />
            </p:panelGrid>
            
            <f:facet name="footer">
                <div class="flex gap-2 justify-content-end">
                    <p:commandButton 
                        value="Réinitialiser"
                        icon="pi pi-check"
                        styleClass="p-button-primary"
                        action="#{not empty resetPasswordAction ? resetPasswordAction : userBean.resetPassword(user.id)}"
                        update="#{update}"
                        oncomplete="PF('resetPasswordDialog').hide()"
                        process="@form" />
                    <p:commandButton 
                        value="Annuler"
                        icon="pi pi-times"
                        styleClass="p-button-secondary"
                        onclick="PF('resetPasswordDialog').hide()" />
                </div>
            </f:facet>
        </h:form>
    </p:dialog>
    
</ui:composition>

