<template>
  <div>
    <div style="padding: 10px 0px 66px 0px;min-height: 100px">
      <!--标题-->
      <h4 style="text-align:center">{{title}}</h4>
      <el-card>
        <!--表单-->
        <fm-generate-form :data="jsonData" ref="generateForm" v-loading="loading"></fm-generate-form>
        <!--附件-->
        <UpLoadComponent ref="upLoadComponent"></UpLoadComponent>
        <!--审核意见-->
        <div v-if="type === 'audit'">
          <el-divider content-position="left"><span style="color: red">*</span> <i class="el-icon-document"></i> 审批意见</el-divider>
          <el-row><el-input type="textarea" :rows="5" maxlength="500" placeholder="请填写审核意见：" v-model="comments" show-word-limit></el-input></el-row>
        </div>
        <!--流程历史-->
        <FlowHisTableCcpm ref="flowHisTableCcpm"></FlowHisTableCcpm>
        <!--操作按钮-->
        <div class="FlowFormFooter">
          <el-button v-if="type === 'audit'" type="primary" :loading="loading" @click="agree" v-noMoreClick plain>同意</el-button>
          <el-button v-if="type === 'audit'" type="danger" :loading="loading" @click="reject" v-noMoreClick plain>驳回</el-button>
          <el-button :loading="loading" @click="close" v-noMoreClick plain>关闭</el-button>
        </div>
      </el-card>

    </div>
  </div>
</template>

<script>
  import CcpmService from '@/api/centerservice/ccpm/CcpmService'
  import UpLoadComponent from '@/views/common/UpLoadComponentCcpm'
  import FlowHisTableCcpm from '@/views/modules/flowable/task/FlowHisTableCcpm'
  export default {
    data () {
      return {
        jsonData: {},
        title: '',
        businessId: '',
        loading: false,
        type: '',
        processDefKey: '',
        comments: '' // 审核意见
      }
    },
    ccpmService: null,
    activated () {
      this.init()
    },
    created () {
      this.ccpmService = new CcpmService()
      this.init()
    },
    components: {
      UpLoadComponent,
      FlowHisTableCcpm
    },
    methods: {
      // 初始化
      init () {
        this.businessId = this.$route.query.id
        this.processDefKey = this.$route.query.processDefKey
        this.title = this.$route.query.title
        this.type = this.$route.query.type
        this.jsonData = []
        this.comments = ''
        this.$nextTick(async () => {
          this.loading = true
          this.$refs.upLoadComponent.clearUpload()
          this.$refs.flowHisTableCcpm.clearFlowHis()
          let res = await this.ccpmService.getByIdGenerate(this.businessId, this.processDefKey)
          if (res) {
            this.loading = false
            this.jsonData = JSON.parse(JSON.stringify(res.data))
            if (this.commonJS.isEmpty(this.jsonData.files)) {
              this.jsonData.files = []
            }
            let filesLabel = '附件'
            if (this.commonJS.isNotEmpty(this.jsonData.filesLabel)) {
              filesLabel = this.jsonData.filesLabel
            }
            this.$refs.upLoadComponent.newUpload('view', this.jsonData.files, null, null, filesLabel)
            this.$refs.flowHisTableCcpm.setFlowHis(this.jsonData.flowChart, this.jsonData.histoicFlow)
            this.$nextTick(() => {
              this.$refs.generateForm.refresh()
            })
          } else {
            this.loading = false
          }
        })
      },
      // 关闭
      close () {
        this.$refs.upLoadComponent.clearUpload()
        this.$refs.flowHisTableCcpm.clearFlowHis()
        this.jsonData = []
        this.comments = ''
        this.$store.dispatch('tagsView/delView', {fullPath: this.$route.fullPath})
        if (this.$route.query.backPath) {
          this.$router.push(this.$route.query.backPath)
        } else {
          this.$router.push('/sys/dashboard/workBench/Pending')
        }
      },
      // 同意
      agree () {
        if (this.commonJS.isEmpty(this.comments)) {
          this.$message.error('请填写审批意见')
        } else {
          this.loading = true
          this.ccpmService.reimAudit(this.businessId, 'yes', this.comments, this.processDefKey).then(({data}) => {
            this.loading = false
            if (data.success) {
              this.$message.success(data.message)
              this.close()
            } else {
              this.$message.error(data.message)
            }
          }).catch(() => {
            this.loading = false
          })
        }
      },
      // 驳回
      reject () {
        if (this.commonJS.isEmpty(this.comments)) {
          this.$message.error('请填写审批意见')
        } else {
          this.$confirm(`确定驳回此报销申请吗?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async () => {
            this.loading = true
            this.ccpmService.reimAudit(this.businessId, 'no', this.comments, this.processDefKey).then(({data}) => {
              this.loading = false
              if (data.success) {
                this.$message.success(data.message)
                this.close()
              } else {
                this.$message.error(data.message)
              }
            }).catch(() => {
              this.loading = false
            })
          })
        }
      }
    }
  }
</script>

