import { State } from '@/constant'

export default {
  props: {
    workflow: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: []
    }
  },
  computed: {
    more () {
      return this.list.length > 1
    },
    btnMsg () {
      return this.list.some(({ pass }) => !pass) ? '驳回' : '通过'
    },
    reviewCol () {
      return this.more
        ? [
          { label: '审核', align: 'center', render: this.renderStatus },
          { type: 'invoke', render: [
            { label: '查看', on: this.onView }
          ] }
        ]
        : [{ type: 'invoke', width: 160, render: [
          { label: '查看', on: this.onView },
          { label: '通过', on: this.onResolve },
          { label: '驳回', on: this.onReject }
        ] }]
    }
  },
  methods: {
    getList () {
      return Promise.resolve({ data: this.list })
    },
    renderStatus (item, h) {
      const { status } = item
      if (status === State.SUBMITTED) {
        return h('el-checkbox', {
          model: {
            value: item.pass,
            callback (val) {
              item.pass = val
            }
          }
        }, '通过')
      }
      return h('el-tag', {
        staticClass: 'o-tag u-readonly',
        props: {
          type: status === State.RESOLVED ? 'success' : 'danger',
          size: 'medium',
          'disable-transitions': true
        }
      }, status === State.RESOLVED ? '通过' : '驳回')
    },
    onResolve () {
      this.$emit('review', { type: this.type, agreeIds: [this.list[0].id], rejectIds: [] })
    },
    onReject () {
      this.$emit('review', { type: this.type, agreeIds: [], rejectIds: [this.list[0].id] })
    },
    onNext () {
      const agreeIds = []
      const rejectIds = []
      this.list.filter(({ status }) => status === State.SUBMITTED).forEach(({ id, pass }) => {
        (pass ? agreeIds : rejectIds).push(id)
      })
      this.$emit('review', { type: this.type, agreeIds, rejectIds })
    }
  }
}
