<template>
  <div class="auditing-main">
    <cy-info-title>基础信息</cy-info-title>
    <basic-info :details-info="detailsInfo.dueDiligenceAllocation" />
    <cy-info-title type="collapse" name="审核内容">
      <legal-person v-if="detailsInfo.age > 55" ref="legalPerson" :details-info="detailsInfo" :disabled="disabled" />
      <audit-content ref="auditContent" :details-info="detailsInfo" :disabled="disabled" />
      <business-address ref="businessAddress" :details-info="detailsInfo" :disabled="disabled" />
    </cy-info-title>
    <cy-info-title type="collapse" name="企业经营情况">
      <business-situation ref="businessSituation" :details-info="detailsInfo" :disabled="disabled" />
    </cy-info-title>
    <audit-conclusion ref="auditConclusion" :details-info="detailsInfo" :disabled="disabled" @conclusionChange="conclusionChange" />
    <financial-situation v-if="!isBack" ref="financialSituation" :details-info="detailsInfo" :disabled="disabled" />
    <cy-info-title v-if="!isBack">
      综合评估
      <div v-if="!disabled" slot="right">
        <el-button type="primary" @click="oneClickCalculation">一键测算</el-button>
      </div>
    </cy-info-title>
    <assess v-if="!isBack" ref="assess" :details-info="assessData" :disabled="disabled" />
  </div>
</template>

<script>
import { getTwoReviewInfo, oneClickSecondEvaluationSubmit } from '@/api/creditManage/supplierManage'
import { validSpecialEmpty } from '@/utils/validate'
export default {
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    LegalPerson: () => import('./components/LegalPerson.vue'),
    AuditContent: () => import('./components/AuditContent'),
    BusinessAddress: () => import('./components/BusinessAddress'),
    BusinessSituation: () => import('./components/BusinessSituation'),
    AuditConclusion: () => import('./components/AuditConclusion'),
    FinancialSituation: () => import('./components/FinancialSituation'),
    Assess: () => import('./components/Assess')
  },
  props: {
    deatailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  inject: {
    disabled: {
      form: 'disabled',
      default: false
    }
  },
  data() {
    return {
      detailsInfo: {},
      assessData: {},
      isBack: false
    }
  },
  watch: {
    deatailsInfo: {
      handler(newV) {
        if (newV && Object.keys(newV)) {
          this.detailsInfo = newV
          this.assessData = newV
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.getTwoReview()
  },
  methods: {
    getTwoReview() {
      const { id } = this.$route.params
      const { type } = this.$route.query
      if (!type && !this.disabled) {
        getTwoReviewInfo({ id }).then(({ data }) => {
          this.detailsInfo = data
        })
      }
    },
    conclusionChange(val) {
      this.isBack = val
    },
    async getParams(type) {
      const { id } = this.$route.params
      const { taskId } = this.$route.query
      const { age, ageCompliance, cSupplierInfoHistoryId, cEnterpriseHistoryId, scope, businessAddress } = this.detailsInfo
      const scopeCompliance = this.$refs.auditContent.getParams()
      const { mapFilesCompliance, rentalFilesCompliance, waterFilesCompliance, officeFilesCompliance } = this.$refs.businessAddress.getParams()
      const businessSituation = this.$refs.businessSituation.getParams()
      var params = {}
      if (!this.isBack) {
        const financialSituation = await this.$refs.financialSituation.getParams(type)
        params = {
          ...params,
          ...financialSituation
        }
      }
      params = {
        ...params,
        ...businessSituation,
        cSupplierInfoId: id,
        cSupplierInfoHistoryId,
        cEnterpriseHistoryId,
        scope,
        businessAddress,
        scopeCompliance,
        mapFilesCompliance,
        rentalFilesCompliance,
        waterFilesCompliance,
        officeFilesCompliance,
        taskId,
        ageCompliance,
        age,
        id: this.detailsInfo.id
      }
      if (age > 55) {
        const { ageCompliance } = this.$refs.legalPerson.getParams()
        params.ageCompliance = ageCompliance
      }

      return params
    },
    async oneClickCalculation() {
      const data = await this.getParams('submit')
      const { scopeCompliance, mapFilesCompliance, rentalFilesCompliance, waterFilesCompliance, officeFilesCompliance, ageCompliance, age } = data
      if (!this.isBack) {
        if (age > 55 && validSpecialEmpty(ageCompliance)) {
          this.$message.warning('请选择法人年龄是否达标')
          return
        }
        if (validSpecialEmpty(scopeCompliance)) {
          this.$message.warning('请选择经营范围是否达标')
          return
        }
        if (validSpecialEmpty(mapFilesCompliance)) {
          this.$message.warning('请选择地点定位图是否达标')
          return
        }
        if (validSpecialEmpty(rentalFilesCompliance)) {
          this.$message.warning('请选择租房合同是否达标')
          return
        }
        if (validSpecialEmpty(waterFilesCompliance)) {
          this.$message.warning('请选择房租缴纳流水是否达标')
          return
        }
        if (validSpecialEmpty(officeFilesCompliance)) {
          this.$message.warning('请选择办公环境图是否达标')
          return
        }
      }
      oneClickSecondEvaluationSubmit({ Loading: true, ...data }).then(({ data }) => {
        this.assessData = data
      })
    },
    async getSubmit(type) {
      const data = await this.getParams(type)
      const auditConclusion = await this.$refs.auditConclusion.getParams(type)
      const { scopeCompliance, mapFilesCompliance, rentalFilesCompliance, waterFilesCompliance, officeFilesCompliance, ageCompliance, age } = data
      if (!this.isBack && type === 'submit') {
        if (age > 55 && validSpecialEmpty(ageCompliance)) {
          this.$message.warning('请选择法人年龄是否达标')
          return
        }
        if (validSpecialEmpty(scopeCompliance)) {
          this.$message.warning('请选择经营范围是否达标')
          return false
        }
        if (validSpecialEmpty(mapFilesCompliance)) {
          this.$message.warning('请选择地点定位图是否达标')
          return
        }
        if (validSpecialEmpty(rentalFilesCompliance)) {
          this.$message.warning('请选择租房合同是否达标')
          return
        }
        if (validSpecialEmpty(waterFilesCompliance)) {
          this.$message.warning('请选择房租缴纳流水是否达标')
          return
        }
        if (validSpecialEmpty(officeFilesCompliance)) {
          this.$message.warning('请选择办公环境图是否达标')
          return
        }
      }
      var params = {
        ...data,
        ...auditConclusion
      }
      if (!this.isBack) {
        const assess = await this.$refs.assess.getParams(type)
        params = {
          ...params,
          ...assess
        }
      }
      return params
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
