<template>
  <div class="space_around" :style="{'width': width}">
    <el-button  v-for="(button, idx) in buttons" :loading="loading[idx]" :key="idx.toString()" @click="buttonClick(button.event, idx)">
      <i :class="button.icon"></i>{{button.name}}
    </el-button>
    <user-select-dialog :show.sync="userSelectModal" :workflowID="workflowID" :show-all="showAll" :instanceID="instanceID" :actionID="actionID" @callback="userSelectCallback"></user-select-dialog>
  </div>
</template>

<style scoped>
  .space_around {
    display: flex;
    justify-content: space-around;
  }
  .el-toolbar .el-button {
    /* font-size: 17px; */
  }
</style>

<script>
import util from '@src/common/util.js'
import emitter from '@src/mixins/emitter.js'
import userSelectDialog from '../userSelectDialog.vue'
export default {
  mixins: [emitter],
  components: {
    'user-select-dialog': userSelectDialog
  },
  props: {
    catalogId: {
      type: [String, Number]
    },
    contentId: {
      type: [String, Number]
    },
    contentType: {
      type: String
    }
  },
  data() {
    return {
      buttons: [],
      localEditor: {},
      userSelectModal: false,
      workflowID: 0,
      instanceID: 0,
      actionID: 0,
      contentID: 0,
      nextStepUser: null,
      memo: null,
      loading: [false, false, false, false, false, false, false],
      loadingidx: 0,
      allowAddActionMessage: false,
      showAll: false,
      width: ''
    }
  },
  mounted() {
    this.contentID = this.contentId
    if (this.$route.params.id !== 'new') {
      this.contentID = this.$route.params.id
    }
    this.loadButton()
    this.$nextTick(() => {
      if (this.$parent && this.$parent.$parent && this.$parent.$parent.isShowBottomBtn) {
        this.width = this.buttons.length && this.$parent.$parent.$el.clientWidth / 4 + 'px'
      }
    })
  },
  methods: {
    async loadButton() {
      if (!this.catalogId) {
        return
      }
      let initbutton = await axios.get(`/ui/contenteditor/initbutton`, {
        params: {
          catalogID: this.catalogId,
          contentID: this.contentID,
          contentType: this.contentType
        }
      })
      this.localEditor = initbutton.data.data
      let loadbuttons = await axios.get(`/ui/contentworkflow/loadbuttons`, {
        params: {
          configProps: this.localEditor.configProps,
          status: this.localEditor.status,
          catalogID: this.catalogId,
          contentType: this.contentType
        }
      })
      this.buttons = loadbuttons.data.data
      if (this.buttons && this.buttons.length > 0) {
        this.$emit('callback', false, false, false)
      } else {
        if (this.localEditor.statusName === '流转中') {
          this.$emit('callback', false, false, false)
        } else {
          this.$emit('callback', true, true, true)
        }
      }
    },
    async userSelectCallback(rows) {
      this.nextStepUser = rows.join(',')
      if (this.allowAddActionMessage) {
        try {
          let event = await this.$prompt('内容：', '消息')
          this.memo = event.value
          if (this.showAll) {
            this.saveWorkflowReviewStep()
          } else {
            this.workflowCommitActionToServer()
          }
        } catch (e) {
        }
      } else {
        if (this.showAll) {
          this.saveWorkflowReviewStep()
        } else {
          this.workflowCommitActionToServer()
        }
      }
      this.showAll = false
    },
    buttonClick(event, idx) {
      let index = event.lastIndexOf('(')
      let methodName = event.slice(0, index)
      let propStr = event.slice(index + 1, event.length - 1)
      let props = propStr.split(',')
      if (methodName === 'publish') {
        this.dispatch('contentDetailToolBar', 'publishContent', {data: {'contentID': this.contentId, 'type': 'pub'}})
      } else {
        this.$set(this.loading, idx, true)
        this.loadingidx = idx
        this[methodName].apply(this, props)
      }
    },
    // 申请工作流步骤
    async workflowApplyStep(workflowID, instanceID, nodeID) {
      let res = await axios.put('/ui/contentworkflow/applystep', {
        workflowID,
        instanceID,
        nodeID,
        catalogID: this.catalogId
      })
      if (res.data.status === 1) {
        this.refreshContentList()
      }
      util.showResponseMessage(res.data)
      this.$set(this.loading, this.loadingidx, false)
      this.loadButton()
    },
    // 申请修改（己发布内容重新修改）
    async workflowRestart(workflowID, instanceID) {
      let res = await axios.put('/ui/contentworkflow/restart', {
        workflowID,
        instanceID,
        catalogID: this.catalogId,
        contentID: this.contentID,
        instanceType: this.contentType
      })
      if (res.data.status === 1) {
        this.refreshContentList()
      }
      util.showResponseMessage(res.data)
      this.$set(this.loading, this.loadingidx, false)
      this.loadButton()
    },
    // 执行工作流动作
    async workflowCommitAction(workflowID, instanceID, actionID, allowSelectUser, allowAddActionMessage) {
      this.workflowID = workflowID
      this.instanceID = instanceID
      this.actionID = actionID
      this.allowAddActionMessage = allowAddActionMessage === 'true'
      this.$set(this.loading, this.loadingidx, false)
      if (allowSelectUser === 'true') {
        this.userSelectModal = true
      } else {
        if (this.allowAddActionMessage) {
          try {
            let event = await this.$prompt('内容：', '消息')
            this.memo = event.value
            this.workflowCommitActionToServer()
          } catch (e) {

          }
        } else {
          this.workflowCommitActionToServer()
        }
      }
    },
    // 提交工作流动作数据到服务器
    async workflowCommitActionToServer() {
      try {
        await Window.saveContent(false)
      } catch (e) {
        this.$set(this.loading, this.loadingidx, false)
        return
      }
      this.contentID = this.$route.params.id
      let res = await axios.put('/ui/contentworkflow/commitaction', {
        workflowID: this.workflowID,
        instanceID: this.instanceID,
        actionID: this.actionID,
        nextStepUser: this.nextStepUser,
        memo: this.memo,
        catalogID: this.catalogId,
        contentID: this.contentID,
        instanceType: this.contentType
      })
      if (res.data.status === 1) {
        this.refreshContentList()
      }
      util.showResponseMessage(res.data)
      this.$set(this.loading, this.loadingidx, false)
      if (res.data.status === 1) {
        this.$root.reload()
      }
    },
    // 工作流临时保存
    async workflowTempSave(workflowID, instanceID, actionID) {
      try {
        await Window.saveContent(false)
        this.refreshContentList()
      } catch (e) {
        this.$set(this.loading, this.loadingidx, false)
        return
      }
      this.$set(this.loading, this.loadingidx, false)
    },
    // 工作流发布
    workflowPublish() {
      Window.publish(true)
      this.$set(this.loading, this.loadingidx, false)
    },
    /**
       * 工作流撤销
       */
    async workflowRevokeStep(workflowID, instanceID, actionID) {
      let res = await axios.put('/ui/contentworkflow/revokestep', {
        workflowID,
        instanceID,
        actionID,
        catalogID: this.catalogId,
        contentID: this.contentID,
        instanceType: this.contentType
      })
      if (res.data.status === 1) {
        this.refreshContentList()
      }
      util.showResponseMessage(res.data)
      this.$set(this.loading, this.loadingidx, false)
      if (res.data.status === 1) {
        this.loadButton()
      }
    },

    /**
      * 工作流驳回
      */
    async workflowRebutStep(workflowID, instanceID, actionID) {
      try {
        let event = await this.$prompt('内容：', '消息')
        this.memo = event.value
      } catch (e) {
        this.$set(this.loading, this.loadingidx, false)
        return
      }
      await Window.saveContent(false)
      let res = await axios.put('/ui/contentworkflow/rebutstep', {
        workflowID,
        instanceID,
        actionID,
        memo: this.memo,
        catalogID: this.catalogId,
        contentID: this.contentID,
        instanceType: this.contentType
      })
      if (res.data.status === 1) {
        this.refreshContentList()
      }
      util.showResponseMessage(res.data)
      this.$set(this.loading, this.loadingidx, false)
      if (res.data.status === 1) {
        this.loadButton()
      }
    },

    /**
      * 工作流转审到服务器
    */
    async saveWorkflowReviewStep() {
      try {
        await Window.saveContent(false)
      } catch (e) {
        this.$set(this.loading, this.loadingidx, false)
        return
      }
      this.contentID = this.$route.params.id
      let res = await axios.put('/ui/contentworkflow/reviewstep', {
        workflowID: this.workflowID,
        instanceID: this.instanceID,
        actionID: this.actionID,
        selectedUsers: this.nextStepUser,
        message: this.memo,
        catalogID: this.catalogId,
        contentID: this.contentID,
        instanceType: this.contentType
      })
      if (res.data.status === 1) {
        this.refreshContentList()
      }
      util.showResponseMessage(res.data)
      this.$set(this.loading, this.loadingidx, false)
      if (res.data.status === 1) {
        this.$root.reload()
      }
    },

    // 执行工作流转审
    async workflowReviewStep(workflowID, instanceID, actionID) {
      this.workflowID = workflowID
      this.instanceID = instanceID
      this.actionID = actionID
      this.allowAddActionMessage = true
      this.$set(this.loading, this.loadingidx, false)
      this.showAll = true
      this.userSelectModal = true
    },

    // 工作流显示消息
    workflowShowMessage(msg){
      let index = msg.indexOf("'") + 1
      let lastIndex = msg.lastIndexOf("'")
      msg = msg.substring(index, lastIndex)
      this.$set(this.loading, this.loadingidx, false)
      this.$prompt(msg, {title: '消息', showInput: false})
    },

    refreshContentList() {
      window.refreshOpenerContentList && window.refreshOpenerContentList()
    }
  }
}
</script>
