<template>
  <div v-loading="loading" class="video-due-main">
    <basic-info />
    <cy-info-title type="collapse" name="企业基础情况">
      <company-basic-info />
    </cy-info-title>
    <cy-info-title type="collapse" name="企业负责人情况">
      <div v-for="(item, index) in datailsInfo.value && datailsInfo.value.creditSituations" :key="index">
        <company-person :ref="`companyPersonRef${index}`" :details-info="item" />
      </div>
    </cy-info-title>
    <cy-info-title type="collapse" name="企业商誉">
      <corporate-goodwill ref="corporateGoodwillRef" />
    </cy-info-title>
    <cy-info-title type="collapse" name="企业经营与财务">
      <operate-finance />
    </cy-info-title>
    <cy-info-title>
      综合评估
      <div v-if="!disabled" slot="right">
        <el-button type="primary" @click="oneClickEvaluation">一键评估</el-button>
      </div>
    </cy-info-title>
    <assess ref="assess" :details-info="assessData" />
  </div>
</template>

<script>
import { getVideoDueInfo, oneClickVideoEvaluation } from '@/api/creditManage/supplierManage'
import { busEmit } from '@/utils/eventBus'
export default {
  components: {
    BasicInfo: () => import('./components/BasicInfo'),
    CompanyBasicInfo: () => import('./components/CompanyBasicInfo/index'),
    CompanyPerson: () => import('./components/CompanyPerson/index'),
    CorporateGoodwill: () => import('./components/CorporateGoodwill/index'),
    OperateFinance: () => import('./components/OperateFinance/index'),
    Assess: () => import('./components/Assess')
  },
  provide() {
    return {
      datailsInfo: this.datailsInfo,
      disabled: this.disabled,
      submitType: () => this.submitType
    }
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    detailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      datailsInfo: {
        value: ''
      },
      assessData: {},
      loading: false,
      submitType: ''
    }
  },
  watch: {
    detailsInfo: {
      handler(newV) {
        this.datailsInfo.value = newV
        this.assessData = newV?.videoDueDiligenceComprehensiveEvaluation
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.getDatails()
  },
  methods: {
    getDatails() {
      const { type } = this.$route.query
      if (type === 'see') return
      this.loading = true
      const { id } = this.$route.params
      getVideoDueInfo({ supplierId: id }).then(({ data }) => {
        this.datailsInfo.value = data
        this.loading = false
      })
    },
    async getParams(type) {
      // 股东
      const shareholderParmas = await busEmit('shareholderParmas')
      // 年龄
      const ageParmas = await busEmit('ageParmas')
      // 注册资本
      const registeredCapitalParmas = await busEmit('registeredCapitalParmas')
      // 注册地址
      const registeredAddressParmas = await busEmit('registeredAddressParmas')
      // 经营地址
      const businessAddressParams = await busEmit('businessAddressParams')
      // 行业
      const industryParams = await busEmit('industryParams')
      // 企业性质
      const natureBusinessParams = await busEmit('natureBusinessParams')
      // 企业变更
      const changeInfoParams = await busEmit('changeInfoParams')
      // 相关企业
      const relatedEnterprisesParams = await busEmit('relatedEnterprisesParams')

      // 企业负责人情况
      const videoDueDiligenceShareholdersSituations = []
      this.datailsInfo.value.creditSituations.forEach(async(item, index) => {
        const itemObj = await this.$refs[`companyPersonRef${index}`][0].getParams(type)
        await videoDueDiligenceShareholdersSituations.push(itemObj)
      })

      // 企业商誉
      const corporateGoodwillParams = await this.$refs.corporateGoodwillRef.getParams(type)

      // 企业经营与财务
      const financialSituationParams = await busEmit('financialSituationParams')

      // 企业运营情况
      const operateSituationParams = await busEmit('operateSituationParams')
      const { id } = this.$route.params
      const { taskId } = this.$route.query

      const params = {
        ...this.datailsInfo.value,
        ...shareholderParmas,
        ...ageParmas,
        ...registeredCapitalParmas,
        ...registeredAddressParmas,
        ...businessAddressParams,
        ...industryParams,
        ...natureBusinessParams,
        ...changeInfoParams,
        ...relatedEnterprisesParams,
        videoDueDiligenceShareholdersSituations,
        ...corporateGoodwillParams,
        videoDueDiligenceFinance: {
          ...financialSituationParams
        },
        ...operateSituationParams,
        cSupplierInfoId: id,
        taskId,
        id: this.datailsInfo.value?.id
      }
      delete params.creditSituations
      delete params.corporateGoodwill
      return params
    },
    async oneClickEvaluation() {
      this.submitType = 'submit'
      const params = await this.getParams('submit')
      oneClickVideoEvaluation({ Loading: true, ...params }).then(({ data }) => {
        const { videoDueDiligenceComprehensiveEvaluation } = data
        this.assessData = videoDueDiligenceComprehensiveEvaluation
      })
    },
    async submit(type) {
      this.submitType = type
      const params = await this.getParams(type)
      const assess = await this.$refs.assess.getParams(type)
      return {
        Loading: true,
        ...params,
        ...assess
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.video-due-main {

}
</style>
