<template>
  <div id="baseMapSwitch" @mouseleave="leave" @mouseenter="enter">
    <Transition name="fade" >
      <div class="hide_part" v-show="show">
        <div v-for="item in mapList" :key="item.id" >
          <transition name="width">
            <img class="title_img" v-if="!item.chosen" :src="imgUrl(item.name)" @click="changeBaseMap(item.id)">
          </transition>
        </div>
      </div>
    </Transition>
    <div class="cur">
      <div class="single-chosen" v-for="item in mapList" :key="item.id" >
        <img class="title_img_next" v-if="item.chosen" :src="imgUrl(item.name)" @click="changeBaseMap(item.id)" alt="">
        <div v-if="item.chosen" class="img-name">
          <span>{{item.label}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {useMapStore} from "@/store/mapStore.js";
import {ref} from "vue";
import {AddBaseMapLayer, AddYaoganMapLayer, AddZhenwuMapLayer} from "@/unit/map/addLayer.js";

export default {
  name: "BaseMapSwitch",
  setup(){
    const mapStore = useMapStore();
    let show = ref(false);
    let mapList = ref([
      {
        id:1,
        label:"政务",
        name:"zhenwu",
        chosen:false
      },
      {
        id:2,
        label:"遥感",
        name:"yaogan",
        chosen:false
      },
      {
        id:3,
        label:"暗色",
        name:"ditu",
        chosen:true
      },
    ]);
    function imgUrl(name){
      return new URL(`../../assets/imgs/${name}.png`, import.meta.url).href
    }
    function changeBaseMap(id) {
      for (let i = 0; i < mapList.value.length; i++) {
        if (id == mapList.value[i].id) {
          if (mapList.value[i].chosen) {
            return
          } else {
            switch (mapList.value[i].id) {
              case 1:
                AddZhenwuMapLayer();
                break;
              case 2:
                AddYaoganMapLayer();
                break;
              case 3:
                AddBaseMapLayer()
                break;
            }
            mapList.value[i].chosen = true;
          }
        } else {
          mapList.value[i].chosen = false;
        }
      }
    }
    function enter(){
      show.value = true;
    }
    function leave(){
      show.value = false;
    }
    return{
      imgUrl,
      changeBaseMap,
      enter,
      leave,
      mapList,
      show
    }
  }
}
</script>

<style scoped lang="scss">
.fade-enter,.fade-leave-to{
  transform: translateX(100px);
  .title_img{
    width: 25px !important;
  }
}
.fade-enter-active,.fade-leave-active{
  transition: 0.5s all;
}
.width-enter,.width-leave-to{
  width: 25px !important;
}
.width-enter-active,.width-leave-active{
  transition: 0.5s all;
}
#baseMapSwitch{
  background-color: rgba(0, 22, 52, 0.75);
  position: absolute;
  z-index: 2;
  right: 460px;
  bottom: 80px;
  width: 92px;
  height: 92px;
  cursor: pointer;
  .cur{
    border: 1px solid #4CD2FF;
    box-sizing: border-box;
    position: relative;
    z-index: 2;
  }
  .single-chosen{
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    .img-name{
      position: absolute;
      width: 100%;
      height: 30px;
      bottom: 0px;
      left:0px;
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgba(0,71,141,0.5);
      span{
        font-weight: 400;
        font-size: 14px;
        color: #FFFFFF;
      }
    }
  }

  .hide_part{
    display: flex;
    position: absolute;
    right: 100px;

  }
  .title_img{
    transition: 0.5s all;
    width: 90px;
    height: 90px;
  }
  .title_img_next{
    width: 90px;
    height: 90px;
  }
}
</style>