<template>
  <sd-webflow ref="webflow">
    <template v-slot:form="{ model, FlowData }">
      <a-spin tip="加载中..." :spinning="spinning">
        <sd-form-by-builder ref="builderform" :form-data="FlowData.processFormData" />
      </a-spin>
    </template>
  </sd-webflow>
</template>

<script>
import LawService from '../../law-service'
import components from './_import-components/case-library-form-import'

export default {
  name: 'CaseLibraryForm',
  metaInfo: {
    title: '案件管理-案例库',
  },
  components,
  data() {
    return {
      spinning: false,
      showflag: false,
    }
  },
  created() {
    const ini = setInterval(() => {
      if (
        this.$refs.webflow &&
        (this.$refs.webflow.$refs.form || this.$refs.webflow.$refs.sdForm)
      ) {
        clearInterval(ini)
        this.initData()
        this.showflag = true
      }
    }, 100)
  },
  methods: {
    initData() {
      if (!this.$route.query.extParams) return
      this.spinning = true
      const query = JSON.parse(this.$route.query.extParams)
      const finishID = query.finishId ? query.finishId : ''
      const caseID = query.caseId ? query.caseId : ''
      // finishID  结案
      // caseId 案件
      LawService.getBeanByFormId(finishID, 'onl:6d9b334e035840fba7843dbdccfd6862').then((res) => {
        const obj = res.data.pageFormData.pageFieldInfos
        obj.forEach((item) => {
          if (!this.$refs.webflow.getFieldValue(item.name)) {
            this.$refs.webflow.setFieldValue(item.name, item.value)
          }
        })

        this.showflag = true
      })
      LawService.getBeanByFormId(caseID, 'onl:d0bb8202233c48f0b5148776754461e1').then((res) => {
        const obj = res.data.pageFormData.pageFieldInfos
        obj.forEach((item) => {
          if (!this.$refs.webflow.getFieldValue(item.name)) {
            this.$refs.webflow.setFieldValue(item.name, item.value)
          }
          // 原告
          if (item.name === 'LAW_CASE_PLAINTIFF') {
            this.$refs.webflow.setFieldValue(
              'LAW_CASE_PLAINTIFF_LIBRARY',
              JSON.parse(item.value).map((item) => {
                item.MAIN_ID = null
                item.id = null
                return item
              })
            )
          }

          // 被告
          if (item.name === 'LAW_CASE_DEFENDANT') {
            this.$refs.webflow.setFieldValue(
              'LAW_CASE_DEFENDANT_LIBRARY',
              JSON.parse(item.value).map((item) => {
                item.MAIN_ID = null
                item.id = null
                return item
              })
            )
          }

          // 第三人  LAW_CASE_THREE
          if (item.name === 'LAW_CASE_THREE') {
            this.$refs.webflow.setFieldValue(
              'LAW_CASE_THREE_LIBRARY',
              JSON.parse(item.value).map((item) => {
                item.MAIN_ID = null
                item.id = null
                return item
              })
            )
          }
        })

        this.showflag = true
        this.spinning = false
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
