<template>
  <div class="report-detail">
    <div style="margin-top: 16px;">
      <check-info v-model="checkInfo" />
      <a-form-model ref="form" :model="auditObj" :rules="rules">
        <audit-edit :auditObj.sync="auditObj" />
        <a-row>
          <a-col :span="24" class="bg-gray">
            <a-button @click="cancel">取消</a-button>
            <a-button type="primary" @click="submit">提交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
  </div>
</template>
<script>
import { isEmptyParams } from '@/views/utils/common'
import moment from 'moment'
import CheckInfo from '@/views/report/check/components/checkInfo'
import AuditList from '@/views/audit/components/auditInfo'
import AuditEdit from '@/views/audit/components/auditEdit'
export default {
  name: 'Audit',
  components: {
    CheckInfo,
    AuditList,
    AuditEdit,
  },
  data() {
    return {
      checkInfo: {
        id: '',
        projId: '',
        checkYear: null,
        // 工作进展
        workProgress: '',
        // 其他（请做说明）
        otherResults: '',
        // 项目批准经费
        applyMoney: 0,
        // 已使用经费
        usingMoney: 0,
        // 现结余经费
        surplusMoney: 0,
        // 经费具体使用情况说明
        moneyInstructions: '',
        checkState: null,
        projName: '',
        projNo: '',
        appPersonName: '',
        appUnitName: '',
        startDate: '',
        endDate: '',
        // 成果
        results: [],
        auditList: [{ result: '', unit: '', time: '' }],
      },
      auditObj: {
        id: this.value,
        auditObjectId: this.objId,
        auditResult: null,
        auditType: 3,
        auditContent: '',
      },
      rules: {
        auditContent: {
          required: true,
          message: '请填写审核意见',
          trigger: 'blur',
        },
        auditResult: {
          required: true,
          message: '请选择审核结果',
          trigger: 'change',
        },
      },
    }
  },
  props: {
    value: {
      type: String,
      default: () => {
        return null
      },
    },
    objId: {
      type: String,
      default() {
        return null
      },
    },
  },
  created() {
    this.getCheckInfoById()
  },
  methods: {
    moment,
    getCheckInfoById() {
      let pars = { id: this.objId }
      if (this.objId != null) {
        this.$api.checkReport
          .getCheckInfoById(pars)
          .then(({ data = {} }) => {
            if (data) {
              this.checkInfo = data
              this.checkInfo.startDate = moment(
                this.checkInfo.startDate
              ).format('YYYY年MM月DD日')
              this.checkInfo.endDate = moment(this.checkInfo.endDate).format(
                'YYYY年MM月DD日'
              )
            }
          })
          .catch(() => {
            this.loading = false
          })
      }
    },
    cancel() {
      this.$emit('closeWindow', 'auditcel')
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let pars = isEmptyParams(this.auditObj)
          let par = { ...pars }
          this.$api.auditManager
            .checkAudit(par)
            .then(({ data = {} }) => {
              if (data) {
                this.$message.success('审核成功！')
                this.$emit('closeWindow', 'audit')
              }
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
.report-detail {
  height: 70vh;
  overflow: auto;
  border-left: 1px solid #f0f0f0;
  border-top: 1px solid #f0f0f0;
}
</style>
