<template>
  <div>
    <a-spin :spinning="spinning != 0">
      <!-- 操作按钮区域 -->
      <div class="table-operator" v-if="showButton">
        <a-button @click="addUser" type="primary" icon="user-add"> {{$locale('m.bpm.other.addAssignee')}}</a-button>
        <a-button @click="removeUser" type="default" icon="user-delete"> {{$locale('m.bpm.other.subAssignee')}}</a-button>
        <a-button @click="addPassRound" type="primary" icon="user-add"> {{$locale('m.bpm.other.addPassUser')}}</a-button>
        <a-button @click="removePassRound" type="default" icon="user-delete"> {{$locale('m.bpm.other.subPassUser')}}</a-button>
        <a-button @click="jump" type="default" icon="rollback"> {{$locale('m.bpm.other.processJump')}}</a-button>
      </div>

      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="pagination"
          @change="handleTableChange"
        >
          <template slot="serialNum" slot-scope="text,record,index">
            {{(pagination.current-1) * pagination.pageSize + parseInt(index) + 1}}
          </template>
        </a-table>
      </div>
    </a-spin>

    <!--流程选人对话框-->
<!--    <bpm-select-user-dialog ref="bpmSelectUserDialog" @selectUserBack="selectUserBack"-->
<!--                            :getActorsUrl="url.getActors"></bpm-select-user-dialog>-->

    <!--跳转、退回指定节点等流程节点选择对话框-->
<!--    <bpm-button-common-dialog ref="activityDialog" :title="activityTitle" :dataVoList="activityList"-->
<!--                              @selectOk="doActivityBack"></bpm-button-common-dialog>-->
  </div>
</template>

<script>

  import { getAction, putAction } from '../../../utils/manage'
  // import BpmSelectUserDialog from '../bpmButton/BpmSelectUserDialog'
  import { ViewMixin } from '../../../utils/ViewMixin'
  // import BpmButtonCommonDialog from '../bpmButton/BpmButtonCommonDialog'
  import YchConstant from "../../../utils/YchConstant";
  import locleUtil from "../../../utils/LocleUtil";

  export default {
    name: 'BpmTask',
    mixins: [ViewMixin],
    components: {
      // BpmSelectUserDialog,
      // BpmButtonCommonDialog
    },
    props: {
      processInstanceId: {
        type: String,
        default: null
      },
      showButton:{
        type: Boolean,
        required: false,
        default: true
      },
      refreshFlg: {
        type: Number,
        default: 0
      },
    },
    data () {
      return {
        currentUrl: null,//记录当前动作需要调用的url，回调使用
        url: {
          addUser: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/addUser/v1',
          removeUser: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/removeUser/v1',
          addPassRound: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/addPassRound/v1',
          removePassRound: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/removePassRound/v1',
          jump: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/jump/v1',
          list: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getTaskListByPage/v1',
          getActivityVoList: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getActivityVoList/v1',
          getActors: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getActors/v1',
          getCurrentTasks: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getCurrentTasks/v1',
          getLastHistTask: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getLastHistTask/v1',
          getFirstHistTask: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getFirstHistTask/v1',
        },
        activityTitle: '',
        activityList: [],//跳转、退回指定节点等流转节点选择；指派处理人节点数据
        activityType: null,//记录当前选择流程节点的动作的类型，选择流程节点后的回调使用
        button: null,//记录当前动作对应的button，回调使用
        queryParam: {
          processInstanceId: this.processInstanceId
        },//查询参数
        columns: [
          {
            title:this.$locale('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 50,
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$locale('m.bpm.entity.taskName'),
            align: 'center',
            width: 150,
            dataIndex: 'taskName'
          },
          {
            title: this.$locale('m.bpm.entity.taskTitle'),
            align: 'center',
            dataIndex: 'taskTitle'
          },
          /*{
            title: '优先级',
            align: 'center',
            dataIndex: 'priority',
            width: 80
          },*/
          {
            title: this.$locale('m.bpm.entity.assigneeName'),
            align: 'center',
            dataIndex: 'assigneeName',
            width: 150,
            customRender: function (t, r, index) {
              return t + (r.assigneeDeptName ? '/' + r.assigneeDeptName : '')
            }
          },
          {
            title: this.$locale('m.bpm.entity.taskType'),
            align: 'center',
            dataIndex: 'identityString',
            width: 80
          },
          {
            title: this.$locale('m.bpm.entity.fromUserName'),
            align: 'center',
            dataIndex: 'fromUserName',
            width: 150,
            customRender: function (t, r, index) {
              return t + (r.fromDeptName ? '/' + r.fromDeptName : '')
            }
          },
          {
            title: this.$locale('m.bpm.entity.sendTimeString'),
            align: 'center',
            dataIndex: 'sendTimeString',
            width: 130
          },
          {
            title: this.$locale('m.bpm.entity.endTimeString'),
            align: 'center',
            dataIndex: 'endTimeString',
            width: 130
          },
          {
            title: this.$locale('m.bpm.entity.dueDateString'),
            align: 'center',
            dataIndex: 'dueDateString',
            width: 130
          },
        ],
      }
    },
    methods: {
      $locale(params){
        return locleUtil.getLocale(params)
      },
      getTask (url) {
        return new Promise((resovle, reject) => {
          this.spinning++
          getAction(url, { processInstanceId: this.processInstanceId }).then((res) => {
            if (res.success) {
              resovle(res.result)
            } else {
              this.$message.warning(res.message)
            }
          }).finally(() => {
            this.spinning--
          })
        })
      },
      /*加签*/
      addUser () {
        this.getTask(this.url.getCurrentTasks).then((taskList) => {
          if (taskList.length == 0) {
            this.$message.warning(this.$locale('m.bpm.verify.tip_no_running_task'))
          } else if (taskList.length == 1) {
            this.doAddUser(taskList[0])
          } else if (taskList.length > 1) {
            this.activityList = taskList
            this.activityList.forEach(vo => {
              vo.text = vo.taskName || vo.taskDefinitionKey
            })
            this.$refs.activityDialog.visible = true
            this.activityTitle = this.$locale('m.bpm.verify.tip_select_current_task')
            this.activityType = 'addUser'
          }
        })
      },
      doAddUser (task) {
        const button = {
          mustIdea: '2',
          type: 'addUser',
          label: this.$locale('m.bpm.other.addAssignee'),
          attribute: {
            taskId: task.id,
            targetActivityId: task.taskDefinitionKey,
          }
        }
        this.currentUrl = this.url.addUser + '?taskId=' + button.attribute.taskId
        this.$refs.bpmSelectUserDialog.open(button)
      },
      /*减签*/
      removeUser () {
        this.getTask(this.url.getCurrentTasks).then((taskList) => {
          if (taskList.length == 0) {
            this.$message.warning(this.$locale('m.bpm.verify.tip_no_running_task'))
          } else if (taskList.length == 1) {
            this.doRemoveUser(taskList[0])
          } else if (taskList.length > 1) {
            this.activityList = taskList
            this.activityList.forEach(vo => {
              vo.text = vo.taskName || vo.taskDefinitionKey
            })
            this.$refs.activityDialog.visible = true
            this.activityTitle = this.$locale('m.bpm.verify.tip_select_current_task')
            this.activityType = 'removeUser'
          }
        })
      },
      doRemoveUser (task) {
        const button = {
          mustIdea: '2',
          type: 'removeUser',
          label: this.$locale('m.bpm.other.subAssignee'),
          attribute: {
            taskId: task.id,
            targetActivityId: task.taskDefinitionKey,
          }
        }
        this.currentUrl = this.url.removeUser + '?taskId=' + button.attribute.taskId
        this.$refs.bpmSelectUserDialog.open(button)
      },
      /*增加传阅人*/
      addPassRound () {
        this.getTask(this.url.getFirstHistTask).then((task) => {
          if (!task) {
            this.$message.warning(this.$locale('m.bpm.verify.tip_no_task'))
          } else {
            const button = {
              mustIdea: '2',
              type: 'addPassRound',
              label: this.$locale('m.bpm.other.addPassUser'),
              attribute: {
                taskId: task.id,
              }
            }
            this.currentUrl = this.url.addPassRound + '?taskId=' + button.attribute.taskId
            this.$refs.bpmSelectUserDialog.open(button)
          }
        })
      },
      /*减少传阅人*/
      removePassRound () {
        this.getTask(this.url.getFirstHistTask).then((task) => {
          if (!task) {
            this.$message.warning(this.$locale('m.bpm.verify.tip_no_task'))
          } else {
            const button = {
              mustIdea: '2',
              type: 'removePassRound',
              label: this.$locale('m.bpm.other.subPassUser'),
              attribute: {
                taskId: task.id,
              }
            }
            this.currentUrl = this.url.removePassRound + '?taskId=' + button.attribute.taskId
            this.$refs.bpmSelectUserDialog.open(button)
          }
        })
      },
      /*跳转*/
      jump () {
        this.getTask(this.url.getCurrentTasks).then((taskList) => {
          if (taskList.length == 0) {
            this.$message.warning(this.$locale('m.bpm.verify.tip_no_running_task'))
          } else if (taskList.length == 1) {
            this.doJump(taskList[0])
          } else if (taskList.length > 1) {
            this.activityList = taskList
            this.activityList.forEach(vo => {
              vo.text = vo.taskName || vo.taskDefinitionKey
            })
            this.$refs.activityDialog.visible = true
            this.activityTitle = this.$locale('m.bpm.verify.tip_select_current_task')
            this.activityType = 'jumpActivity'
          }
        })
      },
      doJump (task) {
        const button = {
          mustIdea: '2',
          type: 'jump',
          label: this.$locale('m.bpm.other.processJump'),
          attribute: {
            taskId: task.id,
          }
        }
        this.spinning++
        getAction(this.url.getActivityVoList, { taskId: task.id }).then((res) => {
          if (res.success) {
            this.activityList = res.result.map(vo => {
              return {
                text: vo.name,
                id: vo.id
              }
            })
            this.$refs.activityDialog.visible = true
            this.button = button
            this.activityTitle = this.$locale('m.bpm.verify.tip_select_target_node')
            this.activityType = 'jump'
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.spinning--
        })
      },
      /*跳转、退回指定节点等动作选择目标节点后的回调*/
      doActivityBack (activity) {
        if (this.activityType == 'jump') {
          this.button.attribute.targetActivityId = activity.id
          this.currentUrl = this.url.jump + '?taskId=' + this.button.attribute.taskId + '&targetActivityId=' + this.button.attribute.targetActivityId
          this.$refs.bpmSelectUserDialog.open(this.button)
        } else if (this.activityType == 'addUser') {
          this.doAddUser(activity)
        } else if (this.activityType == 'removeUser') {
          this.doRemoveUser(activity)
        } else if (this.activityType == 'jumpActivity') {
          this.doJump(activity)
        }
      },
      /*流程选人框的回调，执行按钮实际动作*/
      selectUserBack (data) {
        this.spinning++
        putAction(this.currentUrl, data).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.spinning--
        })
      },
    },
    watch: {
      refreshFlg (value) {
        if (value > 1) {
          this.queryParam.processInstanceId = this.processInstanceId
          this.loadData(1);
        }
      }
    }
  }
</script>
