<template>
  <div>
    <div class="cim-layer-tree">
      <div>

      </div>
      <div class="cim-layer-tree-title">
        <h2 style="color:#0EA5E9;">图层列表</h2>
      </div>
      <div class="cim-layer-tree-body">
        <div class="cim-layer-tree-body-bat" @click="layersbatClick"></div>
        <div class="cim-layer-tree-body-nav" v-if="roleInfo.layersbatVisible">
          <div class="cim-layer-tree-body-item"
             v-for="(item,index) in server" :key="index">
          <!--单选没有选项框-->
            <div class="cim-layer-tree-body-item-first" @click="treeLiClick(item)">
              <img  class="img" :src="(item.image)" />
            </div>
          </div>
        </div>
        
        <div class="cim-layer-tree-body-shadow" v-if="roleInfo.layersData.type=='single' && roleInfo.shadowisShow">
          <!-- <div class="cim-layer-tree-body-shadow-title">
            <h4 style="color:#fff;">在线底图</h4>
            <span @click="roleInfo.shadowisShow = false">X</span>
          </div>  -->
          <div class="cim-layer-tree-body-shadow-ul" v-for="(item,index) in roleInfo.layersData.children" :key="index">
            <div class="cim-layer-tree-body-shadow-ul-li" :class="roleInfo.radioIndex==index?'active':''" @click="layersClick(item,index)">
              <img class="cim-layer-tree-body-shadow-ul-li-img" :src="(item.image)" />
              <div class="cim-layer-tree-body-shadow-ul-li-text">{{item.name}}</div>
            </div>
          </div>
        </div>

        <div class="tree-menu">
          <div v-for="(item,index) in serveType" :key="index" @click="treemenuClick(item)">
            <el-tag style="margin: 0 5px;cursor: pointer;" size="mini">{{item.name}}</el-tag>
          </div>
        </div>

        <div class="cim-layer-tree-body-shadow"  v-if="roleInfo.layersData.type=='many' && roleInfo.shadowisShow">
          <!-- <div class="cim-layer-tree-body-shadow-title">
            <h4 style="color:#fff;">选择图层</h4>
            <span @click="roleInfo.shadowisShow = false">X</span>
          </div>  -->
          <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':''" @click="layersClick(item,index)">
              <img class="cim-layer-tree-body-shadow-ul-li-img" :src="(item.image)" />
              <div class="cim-layer-tree-body-shadow-ul-li-text">{{item.name}}</div>
            </div>
          </div>
        </div>
        
      </div>
    </div>
  </div>
</template>

<script>
import {reactive, toRefs,onMounted} from "vue";
import { postMethodCommon,getMethodCommon } from "../../../../api/common"
export default {
  name: "LayerTreeLocal",
  props: ['server'],
  setup(props, context) {
    const data = reactive({
      server: props.server,
      roleInfo:{
        layersbatVisible: true,
        layersData: props.server[1],
        radioIndex: 0,
        checkboxIndex: [0],
        shadowisShow : true,
        layersChildren: props.server[1].children
      },
      serveType: ''
    })
    console.log('server',data.server)
    onMounted(() => {
      checkbox_methods.getServeType()
    })
    // 负责选择、取消选择图层的函数
    const checkbox_methods = {
      treemenuClick(param){
        data.roleInfo.shadowisShow = true
        let obj = data.roleInfo.layersData
        console.log('obj',obj)
        data.roleInfo.layersChildren = data.roleInfo.layersData.children.filter(res =>{
          return res.type_name == param.name
        })
        console.log('data.roleInfo.layersChildren',data.roleInfo.layersChildren)
      },
      getServeType(){
        getMethodCommon('/Categorys/LoadList',{typeid: "CIM_SERVICE_BIGTYPE"}).then(res => {
            let result = res.result
            data.serveType = result
        })
      },
      layersClick(item,index){
        if(item.parentId == "baseMap"){
          data.roleInfo.radioIndex = index
          checkbox_methods.handlerRadioChange(item,index)
        }else{
          checkbox_methods.handlerCheckboxChange(item,index)
        }
      },
      layersbatClick(){
        data.roleInfo.layersbatVisible = !data.roleInfo.layersbatVisible
        data.roleInfo.shadowisShow = false
      },
      // 单选
      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) {
        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: 20px;
  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;
    }
    &-nav{
      position: absolute;
      right: 70px;
      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;
      right: 70px;
      top: 95px;
      background: rgba(0, 0, 0, 0.4);
      &-title{
        position:relative;
        margin-bottom: 10px;
        span{
          position: absolute;
          right:10px;
          top: 0;
          color:#fff;
          cursor: pointer;
        }
      }
      &-ul{
        display: flex;
        margin-top: 5px;
        &-li{
          width:140px;
          height: 100px;
          border: 1px solid #fff;
          position: relative;
          margin: 0 auto 5px;
          cursor: pointer;
          &-img{
            width: 100%;
            height:100%;
          }
          &-text{
            width:100%;
            height:30px;
            line-height: 30px;
            background: rgba(0, 0, 0, 0.6);
            position:absolute;
            bottom: 0;
            font-size:14px;
            overflow: hidden;
            font-size: 12px;
          }
        }
      }
    }
  }
}
.tree-menu{
  padding: 10px 20px;
  position: absolute;
  top: 50px;
  right: 70px;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
}
.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;
}

</style>