<template>
    <div class="m-system-tabs">
        <el-tabs v-model="topTabsConfig.active" type="card" @tab-click="handleClick">
            <el-tab-pane v-for="(item, index) in topTabsConfig.data" :key="index" :label="item.name" :name="item.id"></el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
import CoreAPI from "../../Common/JS/CoreAPI.js";
export default {
    data() {
        return {
            topTabsConfig: {
                active: "",
                type: ""
            }
        };
    },
    methods: {
        //处理接口返回的参数 拼装成当前需要的参数名等
        setResData(data) {
            if (Array.isArray(data)) {
                data.forEach(item => {
                    let obj = { directoryType: 1, directoryOrder: null };
                    ({ id: obj.directoryId, name: obj.directoryName, parentId: obj.parentDirectoryId, child: obj.children } = item);
                    item = Object.assign(item, obj);
                    if (item.children && item.children.length > 0) {
                        this.setResData(item.children);
                    }
                });
            } else {
                let obj = { directoryType: 1, directoryOrder: null };
                ({ id: obj.directoryId, name: obj.directoryName, parentId: obj.parentDirectoryId, child: obj.children } = data);
                data = Object.assign(data, obj);
            }
        },
        //获取系统tab标签
        getSystemTabsData() {
            let postData = {
                sortModels: null,
                filterModel: null,
                lazyLoad: false
            };
            CoreAPI.getAuthorizeSystemTree(postData)
                .then(res => {
                    if (res && res.length > 0) {
                        this.setResData(res);
                        this.topTabsConfig.data = res;
                        this.topTabsConfig.active = res[0].id;
                        this.$emit("systemChange", { tabs: null, e: null, active: this.topTabsConfig.active });
                    } else {
                        this.$emit("systemChange", { tabs: null, e: null, active: null });
                    }
                })
                .catch(err => {
                    if (err.code) {
                        this.$message.error(
                            this.$t(`cs.common.errorCode.${err.code}`).indexOf(err.code) == -1 ? this.$t(`cs.common.errorCode.${err.code}`) : err.msg
                        );
                    }
                    CloudServiceCommon.Log.debug(err);
                });
        },
        //点击切换系统
        handleClick(tabs, e) {
            this.$emit("systemChange", { tabs, e, active: this.topTabsConfig.active });
        }
    },
    created() {
        this.getSystemTabsData();
    }
};
</script>

<style lang="less" scoped>
.m-system-tabs {
    background: #ffffff;
}
</style>
