<template>
    <div>
        <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList">
        </QueryListPage>
        <select-role ref="selectRole" @reloadTable="reloadTable"></select-role>
    </div>
</template>

<script>
    import { listUserPermision } from '@/api/management/author';
    import { UrlConfig } from '@/router/index';
    import { superAdmin } from '@/router';
    import selectRole from './selectRole/list';

    export default {
        name: 'author',
        components: {
            selectRole,
        },
        data() {
            return {
                param: '',
                WhetherTypeList: [],
                EffectiveTypeList: [],
                PermissionTypeList: [],
                pagerConfig: {
                    pageSize: 'pageSize',
                    pageIndex: 'pageNum'
                },
                queryForm: {
                    labelWidth: '120px',
                    reset: true,
                    queryObject: {
                        'type': {
                            type: 'select',
                            label: '权限类型',
                            config: {
                                data: []
                            }
                        },
                        'status': {
                            type: 'select',
                            label: '权限状态',
                            value: '1',
                            config: {
                                data: []
                            }
                        },
                        'canDistribute': {
                            type: 'select',
                            label: '公用/专用',
                            config: {
                                data: []
                            }
                        }
                    }
                },
                queryList: {
                    query: this.queryFunction,
                    selectCheckShow: false,
                    indexShow: true,
                    columns: [
                        {
                            title: '权限类型',
                            prop: 'type',
                            type: 'tag',
                            columnValue: (row) => { return this.convertField(row.type, this.PermissionTypeList, ['value', 'label']) },
                            tagType: (row) => { return '' }
                        },
                        {
                            title: '地图名称',
                            prop: 'mapName',
                        },
                        {
                            title: '地图产品名称',
                            prop: 'mapProductName',
                        },
                        {
                            title: '课程名称',
                            prop: 'lessonName',
                        },
                        {
                            title: '公用/专用',
                            prop: 'canDistribute',
                            type: 'tag',
                            columnValue: (row) => { return this.$ConstSelect.translate(row.canDistribute, 'PermissionUseList') },
                            tagType: (row) => {
                                switch (row.canDistribute) {
                                    case true: return 'success';
                                    case false: return 'danger';
                                }
                            }
                        },
                        {
                            title: '是否永久',
                            prop: 'forever',
                            type: 'tag',
                            columnValue: (row) => { return this.$ConstSelect.translate(row.forever, 'Whether') },
                            tagType: (row) => {
                                switch (row.forever) {
                                    case true: return 'success';
                                    case false: return 'danger';
                                }
                            }
                        },
                        {
                            title: '权限总数',
                            prop: 'amount'
                        },
                        {
                            title: '剩余权限数量',
                            prop: 'remains'
                        },
                        {
                            title: '开始时间',
                            prop: 'startTime',
                            type: 'formatter',
                            formatter: this.formatterDate

                        },
                        {
                            title: '结束时间',
                            prop: 'endTime',
                            type: 'formatter',
                            formatter: this.formatterDate
                        },
                        {
                            title: '权限归属人',
                            prop: 'ownerName',
                            isShow: () => { return this.$store.state.user.roles.indexOf(superAdmin) != -1; },
                        },
                        {
                            title: '权限状态',
                            prop: 'status',
                            type: 'tag',
                            columnValue: (row) => { return this.convertField(row.status, this.EffectiveTypeList, ['value', 'label']) },
                            tagType: (row) => {
                                switch (row.status) {
                                    case '1': return 'success';
                                    default: return 'danger';
                                }
                            }
                        },
                        {
                            type: 'button',
                            title: '操作',
                            width: '200',
                            hide: () => { return this.$store.state.user.roles.indexOf(superAdmin) < 0; },
                            buttons: [
                                {
                                    name: '设置归属人',
                                    handleClick: this.handleRoleVest,
                                    type: '',
                                }
                            ]
                        }
                    ],
                    actions: [
                        { text: '打包权限', btnCode: 'employee_insert', handler: this.handlePermissionPack },
                    ]
                }
            }
        },

        mounted() {
            this.loadInitData();
        },
        methods: {
            convertList(FromList, ToList, ChecktypeFunction) {
                if (FromList) {
                    ToList.length = 0;
                    FromList.forEach(elem => {
                        if (ChecktypeFunction(elem)) {
                            ToList.push({ value: elem.code, label: elem.name });
                        }
                    });
                }
            },
            convertField(fieldValue, enumList, converFormat) {
                if (converFormat && converFormat.length >= 2) {
                    let value = converFormat[0];
                    let label = converFormat[1];
                    for (let i = 0; enumList && i < enumList.length; i++) {
                        if ('' + fieldValue === '' + enumList[i][value]) {
                            return enumList[i][label];
                        }
                    }
                }
            },
            handleRoleVest(index, row) {
                this.$refs.selectRole.doShow(row.id);
            },
            loadInitData() {
                this.queryForm.queryObject.canDistribute.config.data = this.$ConstSelect.PermissionUseList;

                this.$Dictionary.effectiveType().then(list => {
                    list.forEach(elem => {
                        this.queryForm.queryObject.status.config.data.push({ value: elem.code, label: elem.name });
                    });
                    this.convertList(list, this.EffectiveTypeList, elem => {
                        return true;
                    });
                });

                this.$Dictionary.permissionType().then(list => {
                    list.forEach(elem => {
                        this.queryForm.queryObject.type.config.data.push({ value: elem.code, label: elem.name });
                    });
                    this.convertList(list, this.PermissionTypeList, elem => {
                        return true;
                    });
                });
            },
            formatterDate(row, porpInfo) {
                if (row.hasOwnProperty(porpInfo.property)) {
                }
                return row[porpInfo.property];
            },
            queryFunction(params) {
                return listUserPermision(params);
            },
            handlePermissionPack() {
                this.$router.push({ path: `${UrlConfig.permission.permissionDraft}` })
            },
            reloadTable() {
                if (this.queryList && this.queryList.reload) {
                    this.queryList.reload()
                }
            }
        }
    }
</script>