<template>
    <el-card v-loading="loading" class="map-list-main">
        <div slot="header" class="clearfix">
            <span>{{ $t('global.mapList') }}</span>
        </div>
        <filter-city ref="filerCity" filter-empty :query-function="queryFunction" @filterSelectChange="refresh" />
        <el-input v-model="filterText" :placeholder="this.$t('global.filteringKeywords')" clearable />
        <el-scrollbar wrap-class="scrollbar-wrapper" :style="{ height: (height-125) +'px' }">
            <el-tree
                ref="tree"
                :data="treeList"
                node-key="id"
                :props="defaultProps"
                highlight-current
                :span="22"
                :filter-node-method="filterNode"
                @node-click="clickEvent"
                @node-contextmenu="showContextMenu"
            >
        <span slot-scope="{ node:tnode, data }">
          <span
              class="el-icon-tickets"
              :style="{color: data.valid ? 'green':''}"
          >&nbsp;{{ tnode.label }}</span>
        </span>
            </el-tree>
        </el-scrollbar>
    </el-card>
</template>
<script>
    import { getPublishMapTree } from '@/api/management/mapprd';
    import { UrlConfig } from '@/router/index';
    import { getSessionStorage, setSessionStorage, removeSessionStorage } from '@/utils/auth';
    import FilterCity from '@/views/components/filterCity';

    export default {
        name: 'ExamDetailList',
        components: {
            FilterCity
        },
        props: {
            height: {
                type: Number,
                required: true
            }
        },
        data() {
            return {
                loading: true,
                defaultShowKeys: [],
                queryFunction: getPublishMapTree,
                filterText: '',
                treeData: [],
                treeList: [],
                selected: {},
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                node: {
                },
                mapId: ''
            };
        },
        computed: {
            role() {
                return this.$store.state.user.roles.includes('04') ||
                    this.$store.state.user.roles.includes('05') ||
                    this.$store.state.user.roles.includes('01');
            }
        },
        watch: {
            filterText(val) {
                this.treeList = this.treeData.filter((res) => {
                    return res.name.includes(val);
                });
            }
        },
        beforeDestroy () {
            removeSessionStorage('demonList');
        },
        methods: {
            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            },
            showContextMenu(e, obj, node, vueElem) {
                if (obj) {
                    this.node = node;
                    this.selected = obj;
                }
            },
            clickEvent(obj, data, ele) {
                // this.$router.push({ path: `${UrlConfig.trainingPlatform.course}/16`});
                // return;
                if (data.parent && data.parent.data){
                    this.mapId = data.parent.data.id;
                }
                if (obj.type === 'prd') {
                    setSessionStorage('demonList', this.mapId);
                    this.$router.push({ path: `${UrlConfig.trainingPlatform.prodDetail}/${this.mapId}/${obj.id}` });
                } else if ( obj.type === 'map'){
                    this.mapId = obj.id;
                } else if ( obj.type === 'lesson' ){
                    this.$router.push({ path: `${UrlConfig.trainingPlatform.course}/${obj.id}`});
                } else if ( obj.type === 'exam' ){
                    this.$router.push({ path: `${UrlConfig.trainingPlatform.examDetail}/${obj.id}` });
                }
            },
            async refresh(filterSelect) {
                this.loading = true;
                this.treeData = this.treeList = [];
                try {
                    const res = await getPublishMapTree(filterSelect);
                    this.treeData = res.data;
                    this.treeList = this.filterText
                        ? res.data.filter(elem => { return elem.name.includes(this.filterText); })
                        : res.data;
                    this.$nextTick(() => {
                        const mapId = getSessionStorage('demonList') || null;
                        this.$refs.tree.setCurrentKey(mapId);
                        this.loading = false;
                    });
                } catch (error) {
                    this.loading = false;
                    this.$messageBox(this.$t('error.refreshFailed'));
                }
            }
        }
    };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    .back-home {
        float: right;
        cursor: pointer;

    &:hover {
         color: #3ea726;
     }
    }
</style>
<style>
    .el-tree {
        overflow-x: hidden;
    }

    .el-tree-node.is-current>.el-tree-node__content {
        background-color: #e4e3e3 !important;
    }
</style>
