<template>
  <div>
    <Navbar title="审批详情" />
    <div class="content">
      <Title :title="title" />
      <Preview :dataList="applicationForm" />
      <Title title="其他附件" />
      <Preview :dataList="otherMaterials" />
      <Title title="审批意见" />
      <u--textarea
        v-model="approvalOpinion"
        placeholder="请输入内容"
        count
        height="105"
        @blur="handleBlur"
        :disabled="isDetail"
      />
      <Title title="审批记录" />
      <Steps :data="approvalRecordList" />
      <TabbarBtn
        v-if="!isDetail"
        :showBtn="opt.finalCheck != 0 ? true : false"
        @reject="reject"
        @handlePermit="handlePermit"
        @handleEnd="handleEnd"
      />
    </div>
  </div>
</template>

<script>
import Steps from 'pages/component/Steps'
import TabbarBtn from './component/TabbarBtn'
import { selectFile, submitPersonApproval, approvalHistory } from '@/api/project'
import { filterInput } from '@/utils/regular'

export default {
  components: { Steps, TabbarBtn },
  data() {
    return {
      isLoading: false,
      title: '开工申请单',
      opt: {},
      isEnd: false,
      isDetail: false,
      // 开工申请单
      applicationForm: [],
      // 其他附件
      otherMaterials: [],
      // 审批意见
      approvalOpinion: '',
      // 审批记录
      approvalRecordList: []
    }
  },
  onLoad(opt) {
    console.log('🚀 ~ onLoad ~ opt:', opt)
    this.opt = JSON.parse(opt.params)
    console.log('🚀 ~ onLoad ~ this.opt:', this.opt)
    this.isDetail = this.opt.isDetail
    if (opt.isEnd) {
      this.title = '完工申请单'
      this.isEnd = true
    }
    this.getApplicationForm()
    this.getOtherMaterials()
    this.getApprovalRecord()
  },
  methods: {
    handleBlur() {
      console.log('审批意见', this.approvalOpinion)
      this.approvalOpinion = filterInput(this.approvalOpinion)
    },
    // 驳回
    reject(val) {
      console.log('驳回原因-->父元素:', val)
      const params = {
        proId: this.opt.proId,
        supId: this.opt.supId,
        supUuid: this.opt.supUuid,
        taskId: this.opt.taskId,
        procInstId: this.opt.procInsId,
        finalCheck: this.opt.finalCheck,
        reason: this.approvalOpinion,
        agree: '2',
        rejectReason: val
      }
      if (this.isLoading) {
        uni.showToast({
          title: '请勿重复提交',
          icon: 'none'
        })
        return
      }
      this.isLoading = true
      console.log('🚀 ~ reject ~ params:', params)
      submitPersonApproval(params)
        .then(res => {
          console.log('驳回', res)
          if (res.code === 200) {
            uni.showToast({
              title: '驳回成功',
              icon: 'none'
            })
            setTimeout(() => {
              uni.navigateBack()
              this.isLoading = false
            }, 800)
          }
        })
        .catch(err => {
          console.log('🚀 ~ err:', err)
          setTimeout(() => {
            uni.navigateBack()
            this.isLoading = false
          }, 300)
        })
    },
    // 通过
    handlePermit() {
      if (this.isLoading) {
        uni.showToast({
          title: '请勿重复提交',
          icon: 'none'
        })
        return
      }
      this.isLoading = true
      const params = {
        proId: this.opt.proId,
        supId: this.opt.supId,
        supUuid: this.opt.supUuid,
        taskId: this.opt.taskId,
        procInstId: this.opt.procInsId,
        finalCheck: this.opt.finalCheck,
        reason: this.approvalOpinion,
        agree: '1'
      }
      console.log('🚀 ~ handlePermit ~ params:', params)
      submitPersonApproval(params)
        .then(res => {
          console.log('通过', res)
          if (res.code === 200) {
            uni.showToast({
              title: '审批成功',
              icon: 'none'
            })
            setTimeout(() => {
              uni.navigateBack()
              this.isLoading = false
            }, 800)
          }
        })
        .catch(err => {
          console.log('🚀 ~ err:', err)
          setTimeout(() => {
            uni.navigateBack()
            this.isLoading = false
          }, 300)
        })
    },
    // 终审
    handleEnd() {
      if (this.isLoading) {
        uni.showToast({
          title: '请勿重复提交',
          icon: 'none'
        })
        return
      }
      this.isLoading = true
      const params = {
        proId: this.opt.proId,
        supId: this.opt.supId,
        supUuid: this.opt.supUuid,
        taskId: this.opt.taskId,
        procInstId: this.opt.procInsId,
        finalCheck: this.opt.finalCheck,
        reason: this.approvalOpinion,
        agree: '3'
      }
      console.log('🚀 ~ handleEnd ~ params:', params)
      submitPersonApproval(params)
        .then(res => {
          console.log('通过', res)
          if (res.code === 200) {
            uni.showToast({
              title: '终审成功',
              icon: 'none'
            })
            setTimeout(() => {
              uni.navigateBack()
              this.isLoading = false
            }, 800)
          }
        })
        .catch(err => {
          console.log('🚀 ~ err:', err)
          setTimeout(() => {
            uni.navigateBack()
            this.isLoading = false
          }, 300)
        })
    },
    // 获取其他材料
    getOtherMaterials() {
      const params = {
        proId: this.proId,
        consUuid: this.consUuid,
        uuid: this.opt.uuid,
        classification: this.isEnd ? '2' : '1',
        fromType: '1',
        informationType: this.isEnd ? '12' : '7'
      }
      console.log('🚀 ~ getEnterpriseQualification ~ params:', params)
      selectFile(params).then(res => {
        console.log('其他材料', res)
        if (res.data && res.data.length > 0) {
          this.otherMaterials = res.data
        } else {
          this.otherMaterials = []
        }
      })
    },
    // 获取开工申请单
    getApplicationForm() {
      const params = {
        proId: this.proId,
        consUuid: this.consUuid,
        uuid: this.opt.uuid,
        classification: this.isEnd ? '2' : '1',
        fromType: '1',
        informationType: this.isEnd ? '11' : '6'
      }
      console.log('🚀 ~ getEnterpriseQualification ~ params:', params)
      selectFile(params).then(res => {
        console.log('开工申请单', res)
        if (res.data && res.data.length > 0) {
          this.applicationForm = res.data
        } else {
          this.applicationForm = []
        }
      })
    },
    // 获取审批记录
    async getApprovalRecord() {
      const params = {
        taskId: this.opt.taskId
      }
      console.log('🚀 ~ getApprovalRecord ~ params:', params)
      const res = await approvalHistory(params)
      console.log('审批记录', res)
      if (res.code === 200) {
        this.approvalRecordList = res.data
        console.log('🚀 ~ getApprovalRecord ~ this.ApprovalRecordList:', this.ApprovalRecordList)
      }
    }
  }
}
</script>

<style lang="scss">
.content {
  padding: 0 20px;
}
</style>
