<template>
    <v-card
        flat
        tile
        :height="height"
        class="pa-1"
    >
        <v-card-title class="py-1 text-truncate text-body-1 font-weight-bold">
            {{__('Rol :role. Setări permisiuni :permission.', {permission: permission.name, role: role.name}) }}
        </v-card-title>

        <v-sheet
            :height="height - 76"
            width="100%"
            class="overflow-y-auto"
        >
            <permissions-setting-tree 
                :app="app"
                :role="role"
                :permission="permission"
                :allpermissions="allpermissions"
                v-model="role.permissions"
            />
        </v-sheet>

        <v-card-actions class="py-1">
            <cs-btn 
                caption="Salvează"
                outlined
                color="primary"
                @click="OnClickSave"
            />

            <v-spacer></v-spacer>

            <cs-btn 
                caption="Renunță"
                outlined
                @click="$emit('updated')"
            />
        </v-card-actions>
    </v-card>
</template>

<script>
    export default {
        props: {
            app: {required: true},
            role: {required: true},
            permission: {required: true},
            height: {default: null},
            allpermissions: {required: true},
        },

        methods: {
            OnClickSave() {
                this.app.requests.SaveRolePermissions
                    .ResetData()
                    .SetData({
                        id: this.role.id,
                        permissions: this.role.permissions,
                    })
                    .Request([
                        response => {
                            this.app.dataSession.Roles.Post();
                            this.$emit('updated');
                        }
                    ])
            }
        },

        components: {
            CsBtn: require('@CsVueComp/buttons/CsBtn').default,
        },

        name: 'RolePermissionSettings',
    }
</script>