<template>
  <div v-if="copyVisible">
    <div class="obpm-title">
      <div class="d-inline-block">
        <i class="el-icon-folder title-icon"></i>
        <div class="title-name d-inline-block ml-2 ">{{this.$t('menu.copy')}}{{type}}{{this.$t('menu.menu')}}</div>
      </div>
      <div class="bar d-inline-block float-right ">
        <el-button size="small" @click="copyMenuBtnClick" :loading="btnLoading">{{this.$t('btns.copy_menu')}}</el-button>
      </div> 
    </div>
    <div class="paddingT20 ">
      <span class="mr-2">{{this.$t('menu.copyto')}}{{copyto}}{{this.$t('menu.menu')}}</span>
      <el-select v-model="destid" :placeholder="$t('placeholder.select')" size="small" filterable>
        <el-option v-for="item in menuOption" :key="item.id" :label="item.value" :value="item.id"></el-option>
      </el-select>
    </div>
  </div>
</template>

<script>
import { getAllMenu, copyMenu } from "@/service/CommontoolsAPI.js";
import { mapState } from 'vuex'
export default {
  name: 'copyMenu',
  props: ['type', 'visible'],
  components: {
  },
  data () {
    return {
      appId: sessionStorage.getItem("appId"),
      destid: '',
      menuOption: [],
      copyto: 'mobile',
      btnLoading: false, // 按钮显示加载中
    }
  },

  created () {
    if (this.type == 'pc') this.copyto = 'mobile'; else if (this.type == 'mobile') this.copyto = 'pc'
    this.getAllMenu();

  },
  computed: {
    ...mapState({
      selectTreeNodeArr: state => state.selectTreeNodeArr
    }),
    copyVisible: {
      get () {
        return this.visible;
      },
      set (val) {
        this.$emit("update:visible", val);
      }
    }
  },
  mounted () { },
  methods: {
    getAllMenu () {
      getAllMenu({ applicationId: this.appId, currentMenuId: "", type: "", showType: this.copyto }, {
        onSucess: response => {
          if (response.data.errcode == 0) {
            this.menuOption = response.data.data;
          }
        }
      })
    },
    async copyMenuBtnClick () {
      let isMobile;
      if (this.type == 'pc') isMobile = false; else isMobile = true;
      if (this.selectTreeNodeArr.length > 0) {
        let isCopy = this.selectTreeNodeArr.every(item => item.hasChild == true)
        let checkedMenu = []
        this.selectTreeNodeArr.forEach(item => checkedMenu.push(item.id))
        if (isCopy) {
          this.btnLoading = true
          let response = await copyMenu({ applicationId: this.appId, destid: this.destid, isMobile: isMobile, data: checkedMenu })
          if(response.data.errcode == 0){
            this.btnLoading = false
            this.$message.success(this.$t('msg.copy_ss'))
            this.destid = ""
            this.$emit("refreshMenu");
            this.$store.dispatch('selectTreeNode', [])
          }
        } else {
          return this.$message.error(this.$t('msg.cannot_copy'))
        }
      } else {
        return this.$message.error(this.$t('msg.least_one_menu'))
      }
    },
    createStateFilter(queryString) {
      return (destid) => {
        return (destid.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
  }
}
</script>