<template>
  <div v-loading="loading">
    <IndexPage ref="indexPageRefs" :dataType="1">
      <template #actions="{ row }">
        <m-button type="primary" text @click="onDetails(row)">查看申请单</m-button>
        <m-button v-if="row.status === 0" text type="danger" @click="onUrgent(row)">加急</m-button>
      </template>
    </IndexPage>
    <ApprovalDetails ref="approvalDetailsRefs" @close="onClose"></ApprovalDetails>
  </div>
</template>

<script>
import IndexPage from '../components/IndexPage.vue'
import ApprovalDetails from '../components/ApprovalDetails.vue'
import {
  getApprovalUrge,
  getApprovalList
} from '@/api/approval'
export default {
  name: 'myOrder',
  components: {
    IndexPage,
    ApprovalDetails
  },
  data () {
    return {
      loading: false
    }
  },
  watch: {
    '$route.query.id' (val) {
      if (val) {
        this.onGetDetails(val)
      }
    }
  },
  methods: {
    onClose () {
      this.$router.push(this.$route.path)
    },
    async onGetDetails (workFlowInstanceId) {
      this.loading = true
      try {
        const { data } = await getApprovalList({
          dataType: 1,
          entity: {
            workFlowInstanceId
          }
        })
        if (!data.records.length) {
          return
        }
        // 唤起审核详情
        this.onDetails(data.records[0])
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    },
    onDetails (item) {
      this.$refs.approvalDetailsRefs.open(item)
    },
    async onUrgent ({ workFlowInstanceId }) {
      try {
        await getApprovalUrge({ workFlowInstanceId })
        this.$message.success('加急成功')
        this.$refs.indexPageRefs.onInit()
      } catch (error) {
        this.$message.error(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
