<template>
  <div class="pa-3 white">
    <m-search class="mb-3" :items="searchItems" v-model="searchValues" @search="onSearch"></m-search>
    <m-table
      v-loading="loading"
      :items="items"
      :headers="headers"
      :page-size="pageInfo.size"
      :page-current="pageInfo.current"
      :total="total"
      @page-change="onPageChange"
    >
      <template #status="{ row }">
        <el-tag size="small" :type="statusList[row.status].color">{{ statusList[row.status].text }}</el-tag>
      </template>
      <template #actions="{ row }">
        <m-button type="primary" text @click="onProcess(row)">审批进度</m-button>
        <slot name="actions" :row="row"></slot>
      </template>
    </m-table>
    <ApprovalProgress ref="approvalProgressRefs"></ApprovalProgress>
  </div>
</template>

<script>
import {
  getApprovalList
} from '@/api/approval'
import ApprovalProgress from './ApprovalProgress'
export default {
  name: 'IndexPage',
  components: {
    ApprovalProgress
  },
  props: {
    dataType: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      statusList: [
        { text: '处理中', color: 'default' },
        { text: '审批通过', color: 'success' },
        { text: '审批拒绝', color: 'danger' },
        { text: '异常结束', color: 'warning' }
      ],
      searchItems: [
        {
          label: '名称',
          prop: 'title',
          type: 'input',
          options: {
            placeholder: '请输入名称'
          }
        }
      ],
      searchValues: {
        title: null
      },
      headers: [
        { label: '审批功能名称', prop: 'title' },
        { label: '发起人', prop: 'createUserName' },
        { label: '发起时间', prop: 'createDate' },
        { label: '审核状态', prop: 'status', align: 'center' },
        { label: '工作流版本号', prop: 'version' },
        { label: '操作', prop: 'actions', fixed: 'right', width: 300 }
      ],
      items: [],
      total: 0,
      pageInfo: {
        current: 1,
        size: 10
      },
      loading: false
    }
  },
  created () {
    this.onInit()
  },
  methods: {
    async onInit () {
      this.loading = true
      try {
        const { data } = await getApprovalList({
          dataType: this.dataType,
          page: {
            ...this.pageInfo,
            orders: [
              { column: 'create_date', asc: false }
            ]
          },
          entity: {
            ...this.searchValues
          }
        })
        this.items = data.records
        this.total = data.total || 0
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    },
    onProcess ({ workFlowInstanceId }) {
      this.$refs.approvalProgressRefs.open(workFlowInstanceId)
    },
    async onDetails (item) {
      this.$refs.approvalDetailsRefs.open(item)
    },
    onSearch () {
      this.pageInfo.current = 1
      this.onInit()
    },
    onPageChange (index) {
      this.pageInfo.current = index
      this.onInit()
    }
  }
}
</script>

<style lang="scss" scoped>
  /* 自定义样式 */
</style>
