<template>
    <v-card tile flat :height="cardHeight" :width="width" outlined color="grey lighten-1">
        <v-card-title class="pa-1">
            <span class="text-caption">
                {{  __('Permisiuni :name', {name: user.full_name}) }}
            </span>
        </v-card-title>

        <v-sheet :height="cardHeight - 30" class="overflow-y-auto overflow-x-hidden">

            <cs-permissions-tree 
                :permissions="permissions"
                :values="userpermissions"
                :selectedcolor="role.color"
                @input="OnInput"
            />

        </v-sheet>
    </v-card>
</template>

<script>

    import Userupdatepermissions from './Requests/Userupdatepermissions';

    export default {
        props: {
            /**
             * Toate permisiunile definite in sistem
             */
            permissions: {required: true},

            user: {required: true},
            role: {required: true},
            width: {default: 320},
            height: {default: null},
        },

        data: () => ({
            t: null,
        }),

        computed: {
            cardHeight() {
                return this.height ? this.height :  this.width * 1.618033;
            },

            userpermissions() {
                return {
                    ...this.role.permissions,
                    ...this.user.permissions,
                }
            },
        },

        methods: {

            SavePermissions(permissions) {

                if(this.t)
                {
                    clearTimeout(this.t);
                }

                this.t = setTimeout( () => {                    
                    new Userupdatepermissions()
                        .ResetData()
                        .SetData({
                            user_id: this.user.id,
                            permissions,
                        })
                        .Request([
                            response => {
                            }
                        ]);

                }, 350);
            },

            OnInput(permissions) {
                this.SavePermissions(permissions);
            },
        },

        name: 'CsUserPermissions'
    }
</script>