<template>
  <!-- 打开的菜单图标显示区 -->
  <div class="menu sds-desktop">
    <ul class="menushow" v-if="menuList.length">
      <li :class="menu.spacialCls" v-for="(menu,menuIndex) in menuList" :key="menu.index" :style="{background:menu.backgroundColor}"
      @click="clickMenuItem(menu.imgUrl,menuIndex)">
        <div :class="menu.spacialCls" :style="{backgroundImage:`url(${menu.imgUrl})`}"></div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuList:[],
      isHaveMenuItem: false,
      menuIndex: -1,
    }
  },
  mounted() {
    this.$bus.on('clickShortcutItem',this.clickShortcutItem)
    this.$bus.on("changeDialogWindow",this.changeDialogWindow)
    this.$bus.on('closeDialogWindow',this.closeDialogWindow)
  },
  methods:{
    // 点击大图标打开小图标
    clickShortcutItem(obj){
      let spacialCls  = obj.spacialCls 
      if(this.menuList.length  > 0){
        this.menuList.forEach(menu => {
          if (menu.spacialCls === spacialCls) {
              menu.backgroundColor = 'rgba(225,225,225,0.5)'
          } else {
              menu.backgroundColor = 'rgba(50,60,70,0.9)'
          }
        })
        if(this.menuList.length){
          this.isHaveMenuItem = this.menuList.some(menu => {
            return menu.spacialCls === spacialCls 
          })
        }
      }
      if(!this.isHaveMenuItem){
        let menuItem = {
          id:new Date().getTime(),
          spacialCls:spacialCls,
          imgUrl:obj.imgUrl,
          backgroundColor:'rgba(225,225,225,0.5)'
        }
        this.menuList.push(menuItem)
      }
      
    },
    // 点击小图标打开大图标
    clickMenuItem(imgUrl,menuIndex){
      console.log('click')
      this.$bus.emit('clickMenuItem',imgUrl)
      this.menuList.forEach(menu => {
        menu.backgroundColor = 'rgba(50,60,70,0.9)'
      })
       this.menuList[menuIndex].backgroundColor = 'rgba(225,225,225,0.5)'
    },
    // 点击弹出窗页面切换小图标选中状态
    changeDialogWindow(menu){
      if(typeof(menu) == 'string'){
        this.getMenuItem(menu)
        if(this.menuIndex != -1){
          this.menuList.forEach(menu => {
            menu.backgroundColor = 'rgba(50,60,70,0.9)'
          })
          this.menuList[this.menuIndex].backgroundColor = 'rgba(225,225,225,0.5)'
        }
      }else if (typeof(menu) == 'object'){
        this.getMenuItem(menu.imgUrl)
        this.menuList.forEach(menu => {
          menu.backgroundColor = 'rgba(50,60,70,0.9)'
        })
        if(this.menuIndex == -1){
          this.clickShortcutItem(menu)
        }else{
          this.menuList[this.menuIndex].backgroundColor = 'rgba(225,225,225,0.5)'
        }
        
      }
    },
    // 点击弹出窗页面叉号删去小图标
    closeDialogWindow(imgUrl){
      this.getMenuItem(imgUrl)
      this.menuList.splice(this.menuIndex,1)
      if(!this.menuList.length){
        this.isHaveMenuItem = false
      }else{
        let hasCheckedMenu = this.menuList.some(item=>{
          return item.backgroundColor == 'rgba(225,225,225,0.5)' 
        })
        if (!hasCheckedMenu){
          this.menuList[this.menuList.length - 1].backgroundColor = 'rgba(225,225,225,0.5)'
        }
      }
    },
    // 通过imgUrl获取对应的小图标下标
    getMenuItem(imgUrl){
      this.menuIndex = this.menuList.findIndex(item =>{
        return item.imgUrl === imgUrl
      })
    }
  },
  beforeDestroy() {
    this.$bus.off('clickShortcutItem',this.clickShortcutItem)
    this.$bus.off("changeDialogWindow",this.changeDialogWindow)
    this.$bus.off('closeDialogWindow',this.closeDialogWindow)
  },
  name:'Menu',
  

}
</script>