<template>
  <audit-form-top-banner
    :handel-save-form="saveForm"
    :form-data="readOnly ? 'VIEW' : ''"
    @handelSaveForm="saveForm"
  >
    <sd-detail-form
      ref="docform"
      :page-id="this.$route.query.pageId ? this.$route.query.pageId : null"
      :record-id="this.id ? this.id : null"
      :read-only="readOnly"
      :class="[$style.form]"
      @close="close(true)"
      @saved="saved"
    >
      <template v-slot="{ formData, model, fields }">
        <table :class="$style.detailForm">
          <tr>
            <td colspan="4" style="border:none">
              <sd-form-by-builder
                v-show="showflag"
                :form-data="formData.pageFormData"
              ></sd-form-by-builder>
            </td>
          </tr>
        </table>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import { message } from '@/common/one-ui'
import auditFormTopBanner from '@product/iam/components/audit-form-top-banner'
import CpmService from '../../cpm-service'
import components from './_import-components/cpm-confirm-detail-form-import'

export default {
  name: 'CpmConfirmDetailForm',
  metaInfo: {
    title: '问题清单确认',
  },
  components: {
    ...components,
    auditFormTopBanner,
  },
  data() {
    return {
      initialled: false,
      id: this.$route.query.id ? this.$route.query.id : '',
      flag: true,
      readOnly: false,
      showflag: false,
    }
  },
  mounted() {
    if (this.$route.query.projectId) {
      const ep = this.$route.query.projectId
      const groupId = this.$route.query.groupId
      setTimeout(() => {
        if (groupId !== '1') {
          this.initData(groupId, true, false)
        } else {
          this.initData(ep, true, true)
        }
      }, 2000)
    } else {
      setTimeout(() => {
        const projectId = this.$refs.docform.getFieldValue('PROJECT_ID')
        const subprojectId = this.$refs.docform.getFieldValue('SUB_PROJECT_ID')
        if (subprojectId !== null && subprojectId !== '1') {
          this.initData(subprojectId, false, false)
        } else {
          this.initData(projectId, false, true)
        }
      }, 2000)
    }
    this.setReadOnly()
  },
  methods: {
    saved(t) {
      const idobj = t.pageFormData.pageFieldInfos.find((i) => i.name === 'id').value
      this.id = idobj
      const id = this.$route.params.id ? this.$route.params.id : this.$route.query.id
      if (id === null || id === undefined || id === '') {
        let path = this.$route.fullPath
        path = path.replace('?id=&pageId', '?id=' + this.id + '&pageId')
        window.location.href = '#' + path
      }
      message.success({ content: '保存成功！' }, 1).then(() => {})
    },
    // 保存
    saveForm() {
      this.$refs.docform.saveBtnClick()
    },
    close(flag) {
      window.close()
    },
    // 初始化项目信息
    initData(projectId, flag, flagz) {
      const org = []
      if (projectId) {
        // 获取项目信息
        CpmService.getProjectData(projectId, flagz).then((res) => {
          const id = this.$refs.docform.getFieldValue('id')
          if (!id) {
            const configCode = this.$refs.docform.getFieldValue('currentNumber')
            this.$refs.docform.setFieldValue('FIND_CODE', res.data[0].currentNumber + configCode)
          }

          const title = res.data[0].title
          if (flag) this.$refs.docform.setFieldValue('PROJECT_NAME', title)
          if (flag) this.$refs.docform.setFieldValue('PROJECT_ID', this.$route.query.projectId)
          if (flag) this.$refs.docform.setFieldValue('SUB_PROJECT_ID', this.$route.query.groupId)
          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.docform.setFieldValue('CHECKED_DEPT_FW', orgfw)
          if (flag) {
            if (names.length > 1) {
              this.$refs.docform.setFieldValue('CHECKED_DEPT', org)
            } else {
              names.forEach((n, index) => {
                org.push({
                  type: 'GROUP',
                  name: n,
                  code: codes[index],
                })
              })
              this.$refs.docform.setFieldValue('CHECKED_DEPT', org)
            }
          }
        })
      }
      this.showflag = true
    },
    setReadOnly() {
      const ini = setInterval(() => {
        if (
          this.$refs.docform &&
          (this.$refs.docform.$refs.form || this.$refs.docform.$refs.sdForm)
        ) {
          clearInterval(ini)
          // 工作底稿生成单据不可编辑
          const findfrom = this.$refs.docform.getFieldValue('FIND_FROM') // 问题来源
          if (findfrom === '2' || findfrom === '1') {
            this.readOnly = true
          }
        }
      }, 100)
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';
.detailForm {
  width: 100% !important;
  margin: 0px;
  :global(.sd-form-table) {
    width: 100% !important;
  }
}
</style>
