<template>
  <div>
    <div class="cim-layer-tree" :style="{'left':roleInfo.posVisible?'445px':'50px'}">
      <div class="cim-layer-tree-body">
        <div class="cim-layer-tree-body-bat" @click="layersbatClick"></div>
        <!-- 图层服务 -->
        <div class="cim-layer-tree-body-shadow" :class="roleInfo.shadowisShow?'shadowShow':'shadowHide'">
          <div class="cim-layer-tree-body-shadow-ul" v-for="(item,index) in roleInfo.layersChildren" :key="index">
            <div class="cim-layer-tree-body-shadow-ul-li" :class="roleInfo.checkboxIndex.includes(index)?'active':''" >
              <img class="cim-layer-tree-body-shadow-ul-li-img" :src="(item.image)"  @click="layersClick(item,index)"/>
              <div class="cim-layer-tree-body-shadow-ul-li-text">
                <p style="min-height:40px;"><span>{{item.name}}</span></p>
                <div class="slider-demo-block">
                  <el-slider v-model="item.opacity" @change="changeOpacity($event,item)" />
                </div> 
              </div>
              <div class="cim-layer-tree-body-shadow-ul-li-span">{{item.type_name}}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

   
    
  </div>
</template>

<script>
import {reactive, toRefs,onMounted,watch} from "vue";
import {useRouter} from 'vue-router'
import { postMethodCommon,getMethodCommon } from "../../../../api/common"
import {BASE_API,BASE_IMAGE_URL} from '../../../../../public/config/server'
export default {
  name: "LayerTreeLocal",
  props: ['server','location','layersVisible'],
  setup(props, context) {
    const data = reactive({
      server: props.server,
      BASE_IMAGE_URL:BASE_IMAGE_URL,
      roleInfo:{
        posVisible: props.location,
        treeMenuIndex: null,
        treeMenuVisible: true,
        layersbatVisible: true,
        layersData: props.server[1],
        radioIndex: 0,
        checkboxIndex: [],
        shadowisShow : false,
        layersIsShow: false,
        layersChildren: props.server[1].children,
        navList: [
          {
            cover_img: "CimPlatform\\2023\\20231030\\2023103015421014330058.png",
            id: "1718896072287322112",
            name: "上冶镇农业数字底座",
            page_code: 'shangyezhen'
          },
          {
            cover_img: "CimPlatform\\2023\\20231118\\2023111810272492150162.png",
            id: "1725702238753521664",
            name: "太平镇农业数字底座",
            page_code: 'taipingzhen'
          }
        ],
        layersType: 100,
        layersIndex: null
      },
      serveType: ''
    })
    const route = useRouter()
    watch(
      () => props.location,
      (newVal, oldVal) => {
        data.roleInfo.posVisible = newVal
      }
    )
    watch(
      () =>props.layersVisible,
      () =>{
        data.roleInfo.layersIsShow = false
        data.roleInfo.shadowisShow = false
        data.roleInfo.treeMenuVisible = false
        data.roleInfo.layersbatVisible = false
      }
    )
    onMounted(() => {
      data.roleInfo.layersType = route.currentRoute.value.query.type
      data.roleInfo.layersChildren.forEach((item,index)=>{
        data.roleInfo.layersChildren[index].opacity=80;
        if(item.state == 1){
          data.roleInfo.checkboxIndex.push(index)
        }
      })
      // data.roleInfo.checkboxIndex.push(data.roleInfo.layersChildren.length - 1)
      // checkbox_methods.getMenu()
      checkbox_methods.getServeType()
      data.roleInfo.navList.forEach((item,index) =>{
        if(item.page_code == data.roleInfo.layersType){
          data.roleInfo.layersIndex = index
        }
      })
    })
    // 负责选择、取消选择图层的函数
    const checkbox_methods = {
      getMenu() {
          getMethodCommon("/CimApplication/GetSysCimList", {}).then(res => {
              if (res.code === 200) {
                  data.roleInfo.navList = res.result
                  res.result.forEach((item,index) =>{
                    if(item.page_code == data.roleInfo.layersType){
                      data.roleInfo.layersIndex = index
                    }
                  })
              }
          })
      },
      changeOpacity(e,item){
        context.emit('on-opacity-change',item)
      },
      treemenuClick(param,index){
        data.roleInfo.shadowisShow = true
        data.roleInfo.layersIsShow = false
        data.roleInfo.treeMenuIndex = index
        let obj = data.roleInfo.layersData
        data.roleInfo.layersChildren = data.roleInfo.layersData.children.filter(res =>{
          return res.type_name == param.name
        })
      },
      getServeType(){
        getMethodCommon('/Categorys/LoadList',{typeid: "CIM_SERVICE_BIGTYPE"}).then(res => {
            let result = res.result
            data.serveType = result
        })
      },
      layersClick(item,index){
        data.roleInfo.layersChildren[index].opacity = 80;
        if(item.parentId == "baseMap"){
          data.roleInfo.radioIndex = index
          checkbox_methods.handlerRadioChange(item,index)
        }else{
          checkbox_methods.handlerCheckboxChange(item,index)
        }
      },
      layersNavClick(item,index){
        data.roleInfo.layersIndex = index
        route.push({
            path: '/cim',
            query: {
                type: item.page_code,
                id: item.id,
                title: item.name,
            }
        })
        setTimeout(function(){
          location.reload()
        },500)
      },
      layersbatClick(){
        data.roleInfo.layersbatVisible = !data.roleInfo.layersbatVisible
        data.roleInfo.shadowisShow = !data.roleInfo.shadowisShow
        data.roleInfo.treeMenuVisible = !data.roleInfo.treeMenuVisible
        data.roleInfo.layersIsShow = false
        if(data.roleInfo.layersbatVisible){
          context.emit('handle-tool')
        }
      },
      layersImgClick(){
        data.roleInfo.layersbatVisible = false
        data.roleInfo.shadowisShow = false
        data.roleInfo.treeMenuVisible = false
        data.roleInfo.layersIsShow = !data.roleInfo.layersIsShow
        context.emit('handle-tool')
      },
      // 单选
      handlerRadioChange(item,index) {
        context.emit('change-basemap', item)
        
      },
      // 多选
      handlerCheckboxChange(item,index) {
        if(item.state == 1){
          data.server[1].children[index].state = 0
          data.roleInfo.checkboxIndex.splice(data.roleInfo.checkboxIndex.findIndex(item => item === index), 1)
          context.emit('on-del-layer', item)
        }else{
          data.roleInfo.checkboxIndex.push(index)
          context.emit('on-add-layer', item)
          data.server[1].children[index].state = 1
        }
      },

      handlerFlyToLayer(index1,index2){
        context.emit('on-layer-fly', data.server[index1].children[index2])
      }
    }
    // 用来操作dom样式结构的函数
    const dom_methods = {
      treeLiClick(item,index) {
        if(index==1){
          data.roleInfo.treeMenuVisible = true
          data.server[0].image = '/images/echartsimg/mapbottom.png'
          data.server[1].image = '/images/echartsimg/maplayersactive.png'
        }else{
          data.roleInfo.treeMenuVisible = false
          data.server[0].image = '/images/echartsimg/mapbottomactive.png'
          data.server[1].image = '/images/echartsimg/maplayers.png'
        }
        data.roleInfo.layersData = item
      }
    }
    // 用来父组件调用子组件的方法
    const prev_methods = {
      // 初始化默认选择的底图
      // checkRadioDefault(index) {
      //   console.log('00',data.server[index[0]].children[index[1]])
      //   data.server[index[0]].select = data.server[index[0]].children[index[1]].id
      // }
    }

    return {
      ...toRefs(data),
      ...checkbox_methods,
      ...dom_methods,
      ...prev_methods
    }

  },

}
</script>

<style lang="scss">
.cim-layer-tree {
  position: fixed;
  right: 422px;
  top: 120px;
  // background: rgba(0, 0, 0, 0.4);
  border-radius: 10px;
  z-index: 3;
  color: #eeeeee;
  &-body {
    width: 100%;
    padding: 5px;
    display: flex;
    flex-direction: column;
    position: relative;
    &-bat{
      width: 42px;
      height: 42px;
      background: url(/public/images/cim/mapbatbg.png);
      cursor: pointer;
      animation: rotation 10s infinite linear;
    }
    &-nav{
      position: absolute;
      right: 80px;
      width: 120px;
      height: 46px;
      display: flex;
      justify-content: center;
      align-items: center;
      background: url(/public/images/echartsimg/layersbg.png);
    }
    &-item {
      width: 50%;
      &-first {
        width: 100%;
        text-align: center;
        display: flex;
        align-items: center;
        justify-content: center;
        img {
          width: 32px;
          height: 32px;
          cursor: pointer;
        }

        &-title {
          font-size: 15px;
          cursor: pointer;
          margin-left: 5px;
        }
      }

      &-second {
        width: 100%;
        padding-left: 38px;

        &-box {
          display: flex;
          align-items: center;

          &-button {
            cursor: pointer;
            padding: 0 5px;
            height: 20px;
            min-width: 40px;
            background: #0EA5E9;
            text-align: center;
            border-radius: 5px;
            border: 1px solid gray;
            margin-left: 10px;
            color: #eeeeee;
            line-height: 18px;

            span {
              font-size: 12px;
            }
          }

        }

        &-check-group {
          display: flex;
          flex-wrap: wrap;
          flex-direction: column;
        }

        &-check-slot {
          display: flex;
          align-items: center;


        }

      }
    }
    &-shadow{
      width: 150px;
      position: absolute;
      left: 80px;
      // top: 55px;
      // top: 15px;
      background: rgba(0, 0, 0, 0.4);
      max-height: calc(100vh - 250px);
      overflow: auto;
      border-radius: 8px;
      &-title{
        position:relative;
        margin-bottom: 10px;
        span{
          position: absolute;
          right:10px;
          top: 0;
          color:#fff;
          cursor: pointer;
        }
      }
      &-ul{
        display: flex;
        margin-top: 5px;
        border-radius: 8px;
        box-sizing: border-box;
        &-li{
          width:140px;
          height: 100px;
          border: 1px solid #fff;
          position: relative;
          margin: 0 auto 5px;
          cursor: pointer;
          border-radius: 8px;
          &-img{
            width: 100%;
            height:100%;
            border-radius: 8px;
          }
          &-text{
            width:100%;
            height:40px;
            line-height: 18px;
            background: rgba(0, 0, 0, 0.6);
            position:absolute;
            bottom: 0;
            font-size:10px;
            overflow:hidden;  
            border-radius: 0 0 8px 8px;
            transition: all 0.3s;
            text-align: left;
            // word-wrap: break-word;
            word-break: break-all;
            padding:5px 3px;
            box-sizing: border-box;
          }
          &-text:hover{
            height:70px;
          }
          &-span{
            position: absolute;
            top: 0;
            padding: 3px 5px;
            font-size: 12px;
            background: steelblue;
            border-radius: 8px 0 8px 0;
          }
        }
      }
    }
  }
}
.cim-layer-tree-body-layer{
  position: fixed;
  top: 190px;
  border-radius: 10px;
  z-index: 3;
  cursor: pointer;
}
.cim-layer-tree-body-layer .imgs{
  width: 58px;
  height: 52px;
  background: url(/public/images/layersimg.png);
}
.cim-layer-tree-body-layer .span{
  margin-top: 10px;
  text-shadow: 0 0 4px #000000;
  color:#fff;
  font-size: 16px;
}
.shadowHide{
  display: none;
}
.shadowShow{
  animation:mymove 1s 1;
  -webkit-animation:mymove 1s 1;
  top: 15px;
}
.layersNavShow{
  animation:mymove 1s 1;
  -webkit-animation:mymove 1s 1;
  top: 15px;
}
.layersNavHide{
  animation: myTopmove 1s 1;
  -webkit-animation: myTopmove 1s 1;
  top: -10000px;
}
@keyframes mymove
{
  from {top:-100px;}
  to {top:15px;}
}

@-webkit-keyframes mymove 
{
  from {top:-100px;}
  to {top:15px;}
}
@keyframes myTopmove
{
  from {top:15px;}
  to {top:-10000px;}
}

@-webkit-keyframes myTopmove 
{
  from {top:15px;}
  to {top:-10000px;}
}
.tree-menu{
  padding: 10px 20px;
  position: absolute;
  top: 50px;
  right: 80px;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
}
.tree-menu .activetag{
  border-color: #409eff;
  background: #409eff;
  color: #fff;
}
.cim-layer-tree .el-radio, .el-checkbox {
  height: 28px;
}

.cim-layer-tree-body-item-second .el-radio__label {
  color: #ffffff !important;
}

.cim-layer-tree-body-item-second .el-radio__input.is-checked + .el-radio__label {
  color: #0EA5E9 !important;
}

.cim-layer-tree-body-item-second .el-radio__input.is-checked .el-radio__inner {
  border-color: #0EA5E9 !important;
  background: #0EA5E9 !important;
}

.cim-layer-tree-body-item-second .el-radio__inner {
  border-radius: 2px
}

.cim-layer-tree-body-item-second .el-radio__inner::after {
  width: 100%;
  height: 100%;
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  border-radius: 0;
  transform: translate(-50%, -50%) scale(0);
  transition: transform 0.15s ease-in;
  background-image: url("/public/images/img6-1.png");
  background-size: 100% 100%;
}

.cim-layer-tree-body-item .el-checkbox__input.is-checked .el-checkbox__inner {
  border-color: #0EA5E9 !important;
  background: #ffffff !important;
  border-radius: 2px
}

.cim-layer-tree-body-item .el-checkbox__inner::after {
  width: 100%;
  height: 100%;
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  border: 0;
  border-radius: 0;
  transform: translate(-50%, -50%) scale(0);
  transition: transform 0.15s ease-in;
  background-image: url("/public/images/img6-1.png");
  background-size: 100% 100%;
}

.cim-layer-tree-body-item .el-checkbox__input.is-checked .el-checkbox__inner::after {
  transform: rotate(0);
}

.cim-layer-tree-body-item .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #0EA5E9 !important;
}

.cim-layer-tree-body-item .el-checkbox__label {
  color: #ffffff;
}
.active{
  border: 4px solid #0EA5E9;
}

.slider-demo-block{
  width:100px;
  height:10px;
  margin:0px auto;
  position:relative;
  top:-5px;
}

@keyframes rotation {
	0% {
		-webkit-transform: rotate(0deg);
	}

	25% {
		-webkit-transform: rotate(90deg);
	}

	50% {
		-webkit-transform: rotate(180deg);
	}

	75% {
		-webkit-transform: rotate(270deg);
	}

	100% {
		-webkit-transform: rotate(360deg);
	}
}

::v-deep .el-slider{
  height:20px!important;
  background:red;
}
</style>