<template>
    <div class="homePage">
        <!-- <div class="leftMenu">
            <div class="logoSty">
                <img src="../assets/logoG.png" alt="">
            </div>
            <div class="firstLevel">
                <div class="firstLevel_icon" v-for="item in leftLevelData" :key="item.index" @click="changeRoute(item)">
                    <div class="firstLevel_img" :class="getBac(item.showBac)">
                        <img :src="item.image">
                    </div>
                    <div class="firstLevel_title">
                        <span>{{item.name}}</span>
                    </div>
                </div>
            </div>
        </div> -->
        <div class="frameMain" :style="allHeight">
            <div class="treeList">
                <p>全业务域对标考评系统</p>
                <el-tree ref="tree" :data="forecastDatas" :props="defaultProps" highlight-current node-key="id"
                :default-expanded-keys="expendData" @node-click="handleNodeClick" @node-expand="handleNodeExpand">
                    <template #default="{ node, data }">
                        <span class="custom-tree-node">
                            <el-icon size="small" v-if="node.level === 1 && data.id !== '0'"><FolderOpened /></el-icon>
                            <span v-if="data.id === '0'" style="margin-left:17px;font-size:14px;line-height:35px;display:inline-block">{{node.label}}</span>
                            <span v-else style="margin-left:5px;font-size:14px;line-height:35px;display:inline-block">{{node.label}}</span>
                        </span>
                    </template>
                </el-tree>
            </div>
            <div class="homeMain" :style="mainHeight">
                <div style="height:40px">
                    <el-tag
                        v-for="tag in routeTags"
                        :key="tag.name"
                        closable
                        size="large"
                        class="tagArr"
                        effect="dark"
                        :type="tag.type"
                        @close="handleClose(tag)"
                        @click="handleShowPage(tag)"
                    >
                        <span>{{ tag.name }}</span>
                    </el-tag>
                </div>
                <div class="mainMessage">
                    <router-view></router-view>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import Assessment from "../assets/indexIcon/Assessment.png"
export default {
    data() {
        return {
            leftLevelData: [],
            forecastDatas: [],
            expendData: ['0'],
            routeTags: [],
            defaultProps: {
                children: 'children',
                label: 'label',
            }
        }
    },
    created(){
        this.leftLevelData = [
            // {
            //     index: '/index',
            //     name: '系统门户',
            //     image: gateway,
            //     showBac: true
            // },
            // {
            //     index: '/benchmark',
            //     name: '对标业务',
            //     image: benchmarking
            // },
            {
                index: '/assessment',
                name: '考评业务',
                image: Assessment
            },
            // {
            //     index: '/configure',
            //     name: '系统配置',
            //     image: configure
            // }
        ]
        this.forecastDatas = [
            // {
            //     id: '0',
            //     label: '系统首页',
            //     path: '/home'
            // },
            {
                id: '1',
                label: '对标业务',
                children: [
                    {
                        id: '1-1',
                        label: '排行榜',
                    },
                    {
                        id: '1-2',
                        label: '分类对标',
                    },
                    {
                        id: '1-3',
                        label: '综合对标',
                    },
                    {
                        id: '1-4',
                        label: '立标管理',
                    },
                    {
                        id: '1-5',
                        label: '寻标管理',
                    },
                    {
                        id: '1-6',
                        label: '达标管理',
                    },
                    {
                        id: '1-7',
                        label: '超标管理',
                    },
                    {
                        id: '1-8',
                        label: '对标报告'
                    }
                ]
            },
            {
                id: '2',
                label: '考评业务',
                children: [
                    {
                        id: '2-1',
                        label: '考评总览'
                    },
                    {
                        id: '2-2',
                        label: '考评启动',
                        path: '/assessment/evaluationStart'
                    },
                    {
                        id: '2-3',
                        label: '考评修订'
                    },
                    {
                        id: '2-4',
                        label: '考评目标分解'
                    },
                    {
                        id: '2-5',
                        label: '月/季度考评'
                    },
                    {
                        id: '2-6',
                        label: '考评项对标'
                    },
                    {
                        id: '2-7',
                        label: '考评项预警'
                    },
                    {
                        id: '2-8',
                        label: '年度考评'
                    },
                    {
                        id: '2-9',
                        label: '人员考评'
                    },
                    {
                        id: '2-10',
                        label: '任期考评'
                    },
                    {
                        id: '2-11',
                        label: '考评监督'
                    },
                    {
                        id: '2-12',
                        label: '考评评级'
                    },
                    {
                        id: '2-13',
                        label: '考评报告'
                    },
                    {
                        id: '2-14',
                        label: '考评公告'
                    },
                    {
                        id: '2-15',
                        label: '奖金及薪资总额调整'
                    },
                ]
            },
            {
                id: '3',
                label: '对标考评知识库',
                children: [
                    {
                        id: '3-1',
                        label: '对标指标标准',
                    },
                    {
                        id: '3-2',
                        label: '对标算法规则',
                    },
                    {
                        id: '3-3',
                        label: '对标预警规则',
                    },
                    {
                        id: '3-4',
                        label: '考评得分规则',
                        path: '/knowledgePage/scoringRules'
                    },
                    {
                        id: '3-5',
                        label: '考评评级规则',
                    },
                    {
                        id: '3-6',
                        label: '考评预警规则',
                    },
                ]
            },
            {
                id: '4',
                label: '对标体系配置',
                children: [
                    {
                        id: '4-1',
                        label: '对标指标管理',
                    },
                    {
                        id: '4-2',
                        label: '指标规则配置',
                    },
                    {
                        id: '4-3',
                        label: '对标结构配置',
                    },
                ]
            },
            {
                id: '5',
                label: '考评体系配置',
                children: [
                    {
                        id: '5-1',
                        label: '考评指标管理',
                        path: '/evaluationSystem/evaluationIndex'
                    },
                    {
                        id: '5-2',
                        label: '考评规则配置',
                        path: '/evaluationSystem/evaluationRules'
                    },
                    {
                        id: '5-3',
                        label: '考评单位配置',
                        path: '/evaluationSystem/company'
                    },
                    {
                        id: '5-4',
                        label: '考评部门配置',
                        path: '/evaluationSystem/department'
                    },
                    {
                        id: '5-5',
                        label: '考评人员配置',
                        path: '/evaluationSystem/personnel'
                    },
                ]
            },
            {
                id: '6',
                label: '基础信息配置',
                children: [
                    {
                        id: '6-1',
                        label: '人员权限配置',
                    },
                    {
                        id: '6-2',
                        label: '工作流程配置',
                    }
                ]
            }
        ]
        if (window.localStorage.getItem('routeTags')) {
            this.routeTags = JSON.parse(window.localStorage.getItem('routeTags'))
            if (this.$route?.path === '/') {
                this.$router.push({ path: this.routeTags[0].path})
            }
            // this.$nextTick(() =>{
            //     if (this.$refs.tree) {
            //         this.$refs.tree.setCurrentKey(this.routeTags[0].id)
            //         this.expendData = [this.routeTags[0].id]
            //     }
            // })
        } else {
            this.routeTags = [
                {
                    id: '5-1',
                    label: '考评指标管理',
                    path: '/evaluationSystem/evaluationIndex'
                }
            ]
            window.localStorage.setItem('routeTags', JSON.stringify(this.routeTags))
        }
        
    },
    mounted() {
         this.$nextTick(() => {
            if (this.$route?.path) {
                let path = this.$route.path
                this.expendData = []
                for(let i =0; i<this.forecastDatas.length; i++) {
                    let item = this.forecastDatas[i]
                    if (item.id !== '0') {
                        for(let j =0; j<item.children.length; j++) {
                            let it = item.children[j]
                            if (it.path === path) {
                                if (this.$refs.tree) {
                                    this.$refs.tree.setCurrentKey(it.id)
                                    this.expendData = [item.id]
                                }
                            }
                        }
                    } else {
                        if (this.$refs.tree) {
                            this.$refs.tree.setCurrentKey(item.id)
                            this.expendData = [item.id]
                        }
                    }
                }
            }
        })
    },
    computed:{
        mainHeight() {
            return {
                // 'height': document.documentElement.clientHeight-50 + 'px'
                'width': '84%',
                'height': '95vh'
            }
        },
        allHeight() {
            return {
                'width': '100%',
                // 'height': document.documentElement.clientHeight-40 + 'px'
                'height': '96vh'
            }
        },
    },
    methods:{
        getBac(val) {
            let strWea = ''
            debugger
            if (val) {
                strWea = 'changeBacksty'
            } else {
                strWea = 'defaultBacksty'
            }
            return strWea
        },
        changeRoute(item) {
            if (item.index) {
                this.$router.push({ path: item.index})
            }
        },
        handleNodeClick(val) {
            if (val.path) {
                this.$router.push({ path: val.path})
                let showM = 0
                this.routeTags.forEach(item =>{
                    if (item.name === val.label) {
                        showM++
                    }
                })
                if (showM === 0) {
                    this.routeTags.push({
                        id: val.id,
                        name: val.label,
                        path: val.path
                    })
                }
                window.localStorage.setItem('routeTags', JSON.stringify(this.routeTags))
            }
            if (val.id === '0') {
                let node = this.$refs.tree.getNode(val)
                this.handleNodeExpand(val, node)
            }
        },
        handleNodeExpand(data, node) {
            node.parent.childNodes.forEach(item =>{
                if (data.id !== item.data.id) {
                    item.expanded = false
                }
            })
        },
        handleClose(tag) {
            if (this.routeTags.length > 1) {
                this.routeTags.splice(this.routeTags.indexOf(tag), 1)
                window.localStorage.setItem('routeTags', JSON.stringify(this.routeTags))
            }
        },
        handleShowPage(tag) {
            this.$router.push({ path: tag.path})
            let node = this.$refs.tree.getNode(tag)
            this.handleNodeExpand(tag, tag.id === '0'?node:node.parent)
            this.$refs.tree.setCurrentKey(tag.id)
            this.expendData = [tag.id]
        }
    }
}
</script>

<style lang="less">
.homePage{
    display: flex;
    padding: 10px 10px;
    .leftMenu{
        width: 70px;
        .logoSty{
            padding: 10px 0 0 10px;
            img{
                width: 35px;
                height: 35px;
            }
        }
        .firstLevel{
            margin-top: 30px;
            .firstLevel_icon{
                margin-bottom: 30px;
                cursor: pointer;
                .firstLevel_img{
                    width: 35px;
                    height: 37px;
                    text-align: center;
                    border-radius: 5px;
                    position: relative;
                    left: 10px;
                    img{
                        position: relative;
                        top: 6px;
                        width: 20px;
                        height: 20px;
                    }
                    &:hover{
                        background: rgba(255,255,255,.2);
                    }
                }
                .changeBacksty{
                    background: rgba(255,255,255,.2);
                }
                .defaultBacksty{
                    background: rgba(255,255,255,0);
                }
                .firstLevel_title{
                    margin-left: 5px;
                    span{
                        font-size: 12px;
                        color: #fff;
                    }
                }
            }
        }
    }
    .frameMain{
        background: #f3f7f8;
        border-radius: 30px;
        padding: 10px 0;
        display: flex;
        .treeList{
            margin-left: 5px;
            width: 15%;
            // background: #fff;
            border-radius: 20px;
            p{
                font-size: 18px;
                font-family: '微软雅黑';
                font-weight: bold;
                color: #171e28;
                position: relative;
                left: 30px;
                top: 5px;
                letter-spacing: 1px;
            }
            .el-tree{
                position: relative;
                top: 30px;
                padding: 0 0 0 20px;
                background: transparent;
                .el-tree-node{
                    .el-tree-node__content{
                        height: 35px;
                        .custom-tree-node{
                            height: 35px;
                        }
                    }
                    .el-tree-node__children{
                        .el-tree-node__content{
                            height: 35px;
                            .custom-tree-node{
                                height: 35px;
                            }
                        }
                    }
                }
            }
            .el-tree--highlight-current
            .el-tree-node.is-current
            > .el-tree-node__content {
                background-color: rgba(135, 206, 235, 0.2);
                color: #007aab;
                font-weight: bold;
                border-radius: 5px;
            }
        }
        .homeMain{
            .tagArr{
                height: 25px;
                margin-right: 10px;
                cursor: pointer;
            }
            .mainMessage{
                width: 100%;
                padding-top: 10px;
                background: #fff;
                border-radius: 20px;
            }
        }
    }
}
</style>