<template>
  <sd-webflow ref="webflow" :class="$style.webflow" :validate-form="validForm">
    <template v-slot:form="{ model, FlowData }">
      <sd-form-by-builder
        ref="builderform"
        :form-data="initDaF(FlowData.processFormData)"
        :keep-hidden-cell="false"
      />
    </template>
  </sd-webflow>
</template>

<script>
import CpmService from '../../cpm-service'
import components from './_import-components/cpm-confirm-form-import'

export default {
  name: 'CpmConfirmForm',
  metaInfo: {
    title: 'CpmConfirmForm',
  },
  components,
  data() {
    return {
      projectId: '',
      flag: true,
    }
  },
  mounted() {
    if (this.$route.query.extParams) {
      const ep = JSON.parse(this.$route.query.extParams)
      if (ep.SUB_PROJECT_ID !== '1') {
        this.initData(ep.SUB_PROJECT_ID, true, false)
      } else {
        this.initData(ep.PROJECT_ID, true, true)
      }
    } else {
      setTimeout(() => {
        const projectId = this.$refs.webflow.getFieldValue('PROJECT_ID')
        const subprojectId = this.$refs.webflow.getFieldValue('SUB_PROJECT_ID')
        if (subprojectId !== null && subprojectId !== '1') {
          this.initData(subprojectId, false, false)
        } else {
          this.initData(projectId, false, true)
        }
      }, 2000)
    }
    this.showSelect()
  },
  methods: {
    validForm() {
      this.$refs.webflow.setFieldValue('datagroup1', 'true')
      this.$refs.webflow.setFieldValue('datagroup2', 'true')
      return new Promise((resolve, reject) => {
        resolve(true)
      })
    },
    initDaF(array) {
      if (array) {
        array.attrFD.keepHiddenCell = false
      }
      return array
    },
    showSelect() {
      const stepProps = this.$refs.webflow.FlowData.attrs.stepProps
      if (stepProps) {
        const showSel = this.$refs.webflow.FlowData.attrs.stepProps.showSel
        // 被检查单位环节隐藏选择、删除按钮
        if (showSel === 'false') {
          document.getElementsByClassName('anticon-plus-circle')[0].parentNode.style.display =
            'none'
          document.getElementsByClassName('anticon-minus-circle')[0].parentNode.style.display =
            'none'
        }
      } else {
        // 仅在被检查单位环节显示确认、取消按钮
        document.getElementsByClassName('ant-btn-link')[0].parentNode.style.display = 'none'
        document.getElementsByClassName('ant-btn-link')[1].parentNode.style.display = 'none'
      }
    },
    // 初始化项目信息
    initData(projectId, flag, flagz) {
      const org = []
      if (projectId) {
        // 获取项目信息
        CpmService.getProjectData(projectId, flagz).then((res) => {
          const title = res.data[0].title
          const startDeptName = res.data[0].startDeptName
          if (flag) this.$refs.webflow.setFieldValue('title', title + '问题确认单')
          if (flag) this.$refs.webflow.setFieldValue('START_DEPT_NAME', startDeptName)
          if (flag) {
            this.$refs.webflow.FlowData.processFormData.processFormPropertyValues.find((item) => {
              return item.name === 'START_DEPT_NAME'
            }).readonly = true
          }

          const checkdeptnames = res.data[0].checkedDeptNames
          let checkdeptcodes = []
          if (res.data[0].checkedDeptCodes) {
            checkdeptcodes = res.data[0].checkedDeptCodes
          } else {
            checkdeptcodes = res.data[0].checkedDeptIds
          }
          const code = res.data[0].currentNumber
          const names = checkdeptnames.split(',')
          const codes = checkdeptcodes.split(',')
          const orgfw = []
          names.forEach((n, index) => {
            orgfw.push({
              type: 'Group',
              name: n,
              code: codes[index],
            })
          })
          this.$refs.webflow.setFieldValue('CHECKED_DEPT_FW', orgfw)
          if (flag) {
            if (names.length > 1) {
              this.$refs.webflow.setFieldValue('CHECKED_DEPT', org)
            } else {
              names.forEach((n, index) => {
                org.push({
                  type: 'Group',
                  name: n,
                  code: codes[index],
                })
              })
              this.$refs.webflow.setFieldValue('CHECKED_DEPT', org)
            }
          }
        })
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.webflow {
  :global(.ant-space-horizontal) {
    :global(.ant-space-item):nth-child(4) {
      display: none;
    }
  }
}
</style>
