<template>
  <div class="menus-edit--control" v-show="editMenus">
    <div class="content" >
      <div class="dropdown-menu quick-links-panel" >
        <!-- 快捷入口 -->
        <div class="clearfix quick-links-container">
          <a class="fl"><i class="fa fa-thumb-tack"></i></a>
          <div class="quick-items fl">
            <a class="quick-item" v-for="(item, index) in quickItems" :key="index"> {{item.name}} </a>
          </div>
          <span class="tips inline">Tips:快速触达，最多10个</span>
          <a class="fr" @click="hideContentBox()" ><i class="fa fa-times"></i></a>
          <a class="fr" @click="showEditMenus()" ><i class="fa fa-ellipsis-h"></i></a>
        </div>
      </div>
    </div>
    <div class="warpper">
      <el-dialog :visible.sync="dialogFormVisible" width="100%" center :show-close="false" :top="'4vh'">
        <menusTemplete :select-list="selectMenus" @choose="chooseMenus"  />
        <div slot="footer" class="dialog-footer">
          <el-button @click="hideEditMenus()">取 消</el-button>
          <el-button type="primary" @click="sure()">确 定</el-button>
          <span class="tip">已选：{{selectList.length}}个快捷菜单，最多可选10个。</span>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<style>
  .dropdown-menu.quick-links-panel,.dropdown-menu.quick-links-panel a {
    color: #73879c;
    margin-right: 20px;
  }
  .dropdown-menu.quick-links-panel a i {
    font-size: 24px;
    line-height: 42px;
  }

  .dropdown-menu.quick-links-panel .quick-links-container {
    padding-left: 15px;
  }

  .dropdown-menu.quick-links-panel {
    left: 0;
    width: 100%;
    line-height: 42px;
    border-top: 0;
    z-index: 999;
    border: 0;
    background: #f9f9f9;
  }

  .dropdown-menu.menu-links-panel.in, .dropdown-menu.quick-links-panel.in {
    transform: translateY(0);
    -webkit-transform: translateY(0);
    opacity: 1;
  }

  .menus-edit--control .el-dialog__header {
    display: none;
  }

  .menus-edit--control .quick-item {
    margin: 0 15px 0 0;
    font-size: 14px;
    line-height: 42px
  }

  .warpper .el-dialog--center .el-dialog__body {
    height: 600px;
    overflow: auto;
  }

  .warpper .el-dialog__body .menus-templete {
    padding-bottom: 0;
  }

</style>
<style scoped>
  .tip {
    margin-left: 10px;
    font-size: 12px;
    color: #aaa;
  }
</style>
<script>
import { mapActions, mapGetters } from 'vuex'
import menusTemplete from './menusTemplete'
export default {
  components: {
    menusTemplete
  },
  data () {
    return {
      dialogFormVisible: false,
      selectList: [],
      tempSelectList: [],
      quickItems: [],
      tempQuickItems: [],
      limitLength: 10,
      isSure: false
    }
  },
  created () {
    let quickItemsInfo = localStorage.getItem('_quickItemsInfo')
    if (quickItemsInfo) {
      quickItemsInfo = JSON.parse(quickItemsInfo)
      this.quickItems = quickItemsInfo.quickItems
      this.selectList = quickItemsInfo.selectList
    }
  },
  props: ['showControl'],
  computed: {
    selectMenus () {
      return [...this.tempSelectList, ...this.selectList]
    },
    ...mapGetters({editMenus: 'getEditMenus'})
  },
  methods: {
    ...mapActions({
      hideMenus: 'hideEditMenus'
    }),
    hideEditMenus () {
      this.dialogFormVisible = false
      this.tempSelectList = []
      this.tempQuickItems = []
    },
    showEditMenus () {
      this.isSure = false
      this.dialogFormVisible = true
    },
    hideContentBox () {
      this.hideMenus()
    },
    sure () {
      this.quickItems = [...this.quickItems, ...this.tempQuickItems]
      this.selectList = [...this.selectList, ...this.tempSelectList]
      const quicksInfo = {quickItems: this.quickItems, selectList: this.selectList}
      localStorage.setItem('_quickItemsInfo', JSON.stringify(quicksInfo))
      this.hideEditMenus()
      this.$message({
        message: '快捷菜单设置成功',
        type: 'success'
      })
    },
    chooseMenus ({item, vNode}) {
      const totalLength = this.tempQuickItems.length + this.quickItems.length
      if (this.limitLength < totalLength) {
        this.$message({
          message: '最多选择十个快捷菜单',
          type: 'warning'
        })
        return false
      }
      const index = vNode.index
      const canAdd = this.tempSelectList.indexOf(index) === -1 && this.selectList.indexOf(index) === -1
      if (canAdd) {
        this.tempSelectList.push(index)
        this.tempQuickItems.push(item)
      } else {
        let idx = this.tempSelectList.indexOf(index)
        if (idx === -1) {
          this.selectList.splice(idx, 1)
          this.quickItems.slice(idx, 1)
        } else {
          idx = this.selectList.indexOf(index)
          this.tempSelectList.splice(idx, 1)
          this.tempQuickItems.slice(idx, 1)
        }
      }
    },
    handlerLength (length) {
      if (this.limitLength < length) {
        this.$message({
          message: '最多选择十个快捷菜单',
          type: 'warning'
        })
        return false
      }
    }
  }
}
</script>
