<template>
  <div>
    <a-spin :spinning="spinning != 0">
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="addReader" type="primary" icon="user-add"> {{$locale('m.bpm.other.addReadUser')}}</a-button>
        <a-button @click="removeReader" type="default" icon="user-delete"> {{$locale('m.bpm.other.subReadUser')}}</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>
  </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: 'BpmTaskReader',
    mixins: [ViewMixin],
    components: {
      // BpmSelectUserDialog,
    },
    props: {
      processInstanceId: {
        type: String,
        default: null
      }
    },
    data () {
      return {
        currentUrl: null,//记录当前动作需要调用的url，回调使用
        url: {
          addReader: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/addReader/v1',
          removeReader: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/removeReader/v1',
          list: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getTaskReaderListByPage/v1',
          getActors: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getActors/v1',
          getFirstHistTask: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getFirstHistTask/v1',
        },
        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.readerName'),
            align: 'center',
            dataIndex: 'assigneeName',
            width: 180,
            customRender: function (t, r, index) {
              return t + (r.assigneeDeptName ? '/' + r.assigneeDeptName : '')
            }
          },
          {
            title: this.$locale('m.bpm.entity.sendTimeString'),
            align: 'center',
            dataIndex: 'sendTimeString',
            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--
          })
        })
      },
      /*增加读者*/
      addReader () {
        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: 'addReader',
              label: this.$locale('m.bpm.other.addReadUser'),
              attribute: {
                taskId: task.id,
              }
            }
            this.currentUrl = this.url.addReader + '?taskId=' + button.attribute.taskId
            this.$refs.bpmSelectUserDialog.open(button)
          }
        })
      },
      /*减少读者*/
      removeReader () {
        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: 'removeReader',
              label: this.$locale('m.bpm.other.subReadUser'),
              attribute: {
                taskId: task.id,
              }
            }
            this.currentUrl = this.url.removeReader + '?taskId=' + button.attribute.taskId
            this.$refs.bpmSelectUserDialog.open(button)
          }
        })
      },
      /*流程选人框的回调，执行按钮实际动作*/
      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--
        })
      },
    }
  }
</script>
