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

<script>
import moment from 'moment'
import CpmService from '../../cpm-service'
import components from './_import-components/cpm-work-paper-form-import'

export default {
  name: 'CpmWorkPaperForm',
  metaInfo: {
    title: '工作底稿',
  },
  components,
  data() {
    return { 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)
    }
    // 审批环节审核日期赋默认值
    const flowStatus = this.$refs.webflow.getFieldValue('FLOW_STATUS') // 流程状态
    const vtime = this.$refs.webflow.getFieldValue('VERIFY_TIME')
    if (flowStatus === 1 && !vtime) {
      // 审批环节审核日期为空时赋当前日期默认值
      this.$refs.webflow.setFieldValue('VERIFY_TIME', moment(new Date()))
    }
  },
  methods: {
    // 初始化项目信息
    initData(projectId, flag, flagz) {
      const users = [] // 审核人员（项目组长）
      const org = [] // 被检查单位
      if (projectId) {
        // 获取项目信息
        CpmService.getProjectData(projectId, flagz).then((res) => {
          if (res.data && res.data[0]) {
            // 项目ID
         //   if (flag) this.$refs.webflow.setFieldValue('PROJECT_ID', projectId)
            // 项目编号
           if (flag) this.$refs.webflow.setFieldValue('PROJECT_CODE', res.data[0].currentNumber)
            // 被检查单位
            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 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)
              }
            }
            // 审核人员（项目组长）
            const submitternames = res.data[0].submitterNames
            const submitterids = res.data[0].submitterIds
            const submitteraccounts = res.data[0].submitterAccounts
            const snames = submitternames?.split(',')
            const sids = submitterids?.split(',')
            const saccounts = submitteraccounts?.split(',')
            if (flag) {
              if (snames && snames[0]) {
                const vids = []
                const vaccounts = []
                const vnames = []
                snames.forEach((n, index) => {
                  users.push({
                    type: 'User',
                    name: n,
                    text: n,
                    code: saccounts[index],
                    id: sids ? sids[index] : '',
                    attrNames: [],
                  })
                  vids.push(sids ? sids[index] : '')
                  vaccounts.push(saccounts[index])
                  vnames.push(n)
                })
                this.$refs.webflow.setFieldValue('VERIFY', users)
                this.$refs.webflow.setFieldValue('VERIFY_ID', vids.join(','))
                this.$refs.webflow.setFieldValue('VERIFY_ACCOUNT', vaccounts.join(','))
                this.$refs.webflow.setFieldValue('VERIFY_NAME', vnames.join(','))
              }
            }
          }
        })
      }
    },
    initDaF(array) {
      if (array) {
        array.attrFD.keepHiddenCell = false
        return array
      }
    },
    validForm() {
      this.$refs.webflow.setFieldValue('JBXX', 'true')
      this.$refs.webflow.setFieldValue('JCZL', 'true')
      this.$refs.webflow.setFieldValue('WTFX', 'true')
      return new Promise((resolve, reject) => {
        resolve(true)
      })
    },
  },
}
</script>

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