<template>
    <div class="table-box">
         <div v-for="(item,index) in listData" :key="index" class="mt-1" :title="item.serverName">
            <div class="table-box-li cursor" :class="checkPidArr.includes(item.serverName)?'active':''"  @mouseenter="handleMouseEnter(item)" @click="layersParClick(item)">
                <!-- <span>{{ item.serverName }}</span> -->
                <img v-if="checkPidArr.includes(item.serverName)" class="img" :src="layerImgPath[index].activeurl" />
                <img v-else class="img" :src="layerImgPath[index].url" />
            </div>
         </div>
         <div class="tab-dia" v-if="tabDiaVisible"  @mouseleave="handleMouseLeave">
            <div v-for="(val,num) in tabDiaData" :key="num" @click="layerLiClick(val)">
                <div class="table-li cursor" :class="val.attribute.show?'active':''">
                    {{ val.serverName }}
                </div>
            </div>
            
            
        </div>
    </div>
</template>

<script>
import axios from 'axios'
import { postMethodCommon,getMethodCommon } from '@/api/common.js';
export default {
    name: 'setting',
    data(){
        return {
             listData:[],
             tabDiaVisible: false,
             tabDiaData:[],
             checkPidArr:[],
             checkLayersArr:[],
             layersGraphicArr:[],
             layerImgPath:[
                { url: '/img/layers/layers1.png', activeurl: '/img/layers/layersactive1.png' },
                { url: '/img/layers/layers2.png', activeurl: '/img/layers/layersactive2.png' },
                { url: '/img/layers/layers3.png', activeurl: '/img/layers/layersactive3.png' },
                { url: '/img/layers/layers4.png', activeurl: '/img/layers/layersactive4.png' },
                { url: '/img/layers/layers5.png', activeurl: '/img/layers/layersactive5.png' },
                { url: '/img/layers/layers6.png', activeurl: '/img/layers/layersactive6.png' },
                { url: '/img/layers/layers7.png', activeurl: '/img/layers/layersactive7.png' },
                { url: '/img/layers/layers8.png', activeurl: '/img/layers/layersactive8.png' },
                { url: '/img/layers/layers9.png', activeurl: '/img/layers/layersactive9.png' },
                { url: '/img/layers/layers10.png', activeurl: '/img/layers/layersactive10.png' },
                { url: '/img/layers/layers11.png', activeurl: '/img/layers/layersactive11.png' },
                { url: '/img/layers/layers12.png', activeurl: '/img/layers/layersactive12.png' },
                
             ]
        }
    },
    watch:{
         
    },
    created(){
        this.getLayerListData()
    },
    mounted(){
        // console.log('globalmap',window.globalmap)
    },
    methods:{
        handleMouseEnter(item){
            this.tabDiaVisible = true
            this.tabDiaData = item.children
        },
        handleMouseLeave(){
            this.tabDiaVisible = false
        },
        layersParClick(item){
            let arr = []

            item.children.forEach(res =>{
                if(res.attribute.show){
                    arr.push(res)
                }
            })
            
            this.listData.forEach(val =>{
                if(item.id == val.id){
                    if(arr.length == val.children.length){
                        val.children.forEach(res =>{
                            res.attribute.show = false
                        })
                    }else{
                        val.children.forEach(res =>{
                            res.attribute.show = true
                        })
                    }
                }
            })

            this.handleLayers()
        },
        layerLiClick(val){
            this.listData.forEach(item =>{
                item.children.forEach(res =>{
                    if(val.serverName == res.serverName && val.id == res.id){
                        res.attribute.show = !res.attribute.show
                    }
                })
            })
          
            this.handleLayers()
        },
        handleLayers(){
            let _this = this
            this.checkPidArr = []
            this.listData.forEach(item =>{
                item.children.forEach(res =>{
                    if(res.attribute.show){
                        _this.checkPidArr.push(res.projectTeamName)
                    }
                })
            })
            this.checkPidArr = [...new Set(this.checkPidArr)]
            this.uploadLayers()
        },
        uploadLayers(){
            let arr = []

            this.layersGraphicArr.forEach(item =>{
                window.globalmap.removeLayer(item)
            })

            this.listData.forEach(item =>{
                item.children.forEach(res =>{
                    if(res.attribute.show){
                        arr.push(res.attribute)
                    }
                })
            })

            arr.forEach(item =>{
                if(item.type == "wms"){
                    const tileLayer = new mars3d.layer.WmsLayer(item)
                    window.globalmap.addLayer(tileLayer)
                    this.layersGraphicArr.push(tileLayer)
                }
                if(item.type == "geojson"){
                    const graphicLayer = new mars3d.layer.GeoJsonLayer(item)
                    window.globalmap.addLayer(graphicLayer)
                    this.layersGraphicArr.push(graphicLayer)
                }
            })
        },
        getLayerListData(){
            let _this = this
            _this.checkPidArr = []
            axios({
                method: "get",
                url: "http://223.99.16.253:9006/applicationMars/getApplicationInfo/feixian"
            }).then(res=>{
                this.listData = res.data.data
                this.listData.forEach(item =>{
                    item.children.forEach(val =>{
                        val.attribute = JSON.parse(val.attribute)
                        if(val.attribute.show){
                            _this.checkPidArr.push(val.projectTeamName)
                        }
                    })
                })
                setTimeout(() =>{
                    this.uploadLayers()
                },1000)  
                
            })
        }
        
    }
}
</script>

<style scoped>
.table-box{
    width: 100%;
    color: #fff;
    border-radius: 100px;
    background: rgba(0,0,0,0.6);
    padding: 25px 0;
    position: relative;
}
.table-box-li{
    text-align: center;
}
.table-li{
    text-align: center;
    font-size: 14px;
    white-space: nowrap;
    margin: 5px 0;
    padding: 3px 10px;
}
.table-box-li.active{
    background-image:url(/img/layers/layerschecked.png);
    background-size: 100% 100%;
}
.table-box-li:hover{
    background-image:url(/img/layers/layerschecked.png);
    background-size: 100% 100%;
}
.table-li .img{
    width: 33px;
    height:32px;
}
.table-li.active{
    background: linear-gradient(90deg, rgba(73,191,145,0.3) 0%, rgba(73,191,145,0) 100%);
    color: #FED74C;
}
.table-li:hover{
    background: linear-gradient(90deg, rgba(73,191,145,0.3) 0%, rgba(73,191,145,0) 100%);
    color: #FED74C;
}
.tab-dia{
    position: absolute;
    left: calc(100% + 5px);
    top: 50%;
    padding: 10px 0;
    background: rgba(1,24,1,0.8);
    border: 0px solid #33E9E9;
    transform: translate( 0 , -50%);
}






::-webkit-scrollbar-track, ::-webkit-scrollbar-thumb {
    border: 0
}
  
::-webkit-scrollbar {
    height: 10px;
    width: 10px;
    background: transparent;
    border-radius: 5px
}

::-webkit-scrollbar-thumb {
    padding-top: 100px;
    -webkit-box-shadow: inset 1px 1px 0 rgba(0, 0, 0, .1), inset -1px -1px 0 rgba(0, 0, 0, .07);
    background-color: #797979;
    min-height: 28px;
    border-radius: 4px;
    background-clip: padding-box
}

::-webkit-scrollbar-track, ::-webkit-scrollbar-thumb {
    border: 0
}

::-webkit-scrollbar-thumb:hover {
    -webkit-box-shadow: inset 1px 1px 1px rgba(0, 0, 0, .25);
    background-color: rgba(0, 0, 0, .4)
}

::-webkit-scrollbar-thumb:active {
    -webkit-box-shadow: inset 1px 1px 3px rgba(0, 0, 0, .35);
    background-color: rgba(0, 0, 0, .5)
}
</style>