<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveFormShow ? saveForm : null"
      :handel-publish="handelPublishShow ? handelPublish : null"
      :hiden-or-show="hidenOR0Show"
      @handelPublish="handelPublish"
      @handelSaveForm="saveForm"
    >
      <sd-detail-form
        ref="docform"
        page-id="audit/work/intoMeeting/iamAuditIntoMetting"
        form-id="iamAuditIntoMetting"
        :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
        :class="$style.form"
        @close="close(true)"
        @saved="saved"
        @sdFormReady="sdFormReady"
      >
        <template v-slot="{ model, fields }">
          <table>
            <tr :class="$style.firstTr">
              <td class="ant-form-item-label ant-form-item-label"></td>
              <td></td>
              <td class="ant-form-item-label ant-form-item-label"></td>
              <td></td>
            </tr>
            <tr>
              <!-- 文件标题 -->
              <sd-form-item-td name="fieldName" :colspan="3" />
            </tr>
            <tr>
              <!-- 被审计单位names -->
              <sd-form-item-td name="auditedUnit" />
              <!-- 文件编号 -->
              <sd-form-item-td name="fieldCode"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 文件描述 -->
              <sd-form-item-td name="fieldDesc" :colspan="3"> </sd-form-item-td>
            </tr>
            <tr>
              <!-- 附件 -->
              <sd-form-item-td name="attachment" :colspan="3" />
            </tr>
            <tr>
              <!-- 编制人员 -->
              <sd-form-item-td name="creatorName" />
              <!-- 编制日期 -->
              <sd-form-item-td name="creationTime" />
            </tr>
            <sd-form-item-td :hidden="true" name="projectId" />
          </table>
        </template>
      </sd-detail-form>
    </audit-form-top-banner>
  </span>
</template>

<script>
import { Message } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditFormTopBanner from '../../../../components/audit-form-top-banner'
import components from './_import-components/iam-audit-into-meeting-form-import'

export default {
  name: 'IamAuditIntoMeetingForm',
  metaInfo: {
    title: '进场会',
  },
  components: {
    ...components,
    auditFormTopBanner,
  },

  data() {
    return {
      projectId: this.$root.$route.query.projectId,
      mode: this.$root.$route.query.mode,
      hidenOR0Show: true,
      saveFormShow: true,
      handelPublishShow: true,
    }
  },
  mounted() {
    // 假如新建的话
    if (!this.$route.query.record) {
      setTimeout(() => {
        this.setAuditUnit(this.projectId)
      }, 1000)
    }
  },
  created() {},
  methods: {
    // 页面加载完成后
    sdFormReady() {
      const ini = setInterval(() => {
        if (this.$refs.docform) {
          try {
            const endType = this.$refs.docform.getFieldValue('endType')
            if (endType === '1') {
              this.hidenOR0Show = false
            }
            clearInterval(ini)
          } catch {}
        }
      }, 100)
      if (this.mode === 'VIEW') {
        this.saveFormShow = false
        this.handelPublishShow = false
      }
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    saved() {
      const endType = this.$refs.docform.getFieldValue('endType')
      let content = '保存成功！'
      if (endType === 1) {
        content = '发布成功！'
      }

      Message.success({ content: content }, 1).then(() => {
        this.close(true)
      })
    },
    // 保存
    saveForm() {
      var endType = this.$refs.docform.getFieldValue('endType')
      if (endType !== '1') {
        this.$refs.docform.setFieldValue('endType', 0)
      }
      this.$refs.docform.setFieldValue('projectId', this.projectId)
      var selectVal = this.$refs.docform.getFieldValue('auditedUnit')
      if (selectVal) {
        var names = []
        var ids = []
        for (var i = 0; i < selectVal.length; i++) {
          names.push(selectVal[i].text || selectVal[i].name)
          ids.push(selectVal[i].id || selectVal[i].code)
        }
        this.$refs.docform.setFieldsValue({
          auditedUnitNames: names.join(','),
          auditedUnitIds: ids.join(','),
        })
      }

      this.$refs.docform.saveBtnClick()
    },
    // 发布
    handelPublish() {
      this.$refs.docform.setFieldValue('endType', 1)
      this.$refs.docform.setFieldValue('projectId', this.projectId)
      var selectVal = this.$refs.docform.getFieldValue('auditedUnit')
      if (selectVal) {
        var names = []
        var ids = []
        for (var i = 0; i < selectVal.length; i++) {
          names.push(selectVal[i].text || selectVal[i].name)
          ids.push(selectVal[i].id || selectVal[i].code)
        }
        this.$refs.docform.setFieldsValue({
          auditedUnitNames: names.join(','),
          auditedUnitIds: ids.join(','),
        })
      }
      this.$refs.docform.saveBtnClick()
    },
    // 获取被审计单位
    setAuditUnit(projectId) {
      const params = {
        projectId: projectId,
      }
      if (projectId) {
        axios({
          url: 'api/xcoa-mobile/v1/iam-audit-intometting/findAuditUnit',
          method: 'post',
          params,
        }).then((res) => {
          this.$refs.docform.setFieldValue('auditedUnit', res.data)
        })
      }
    },
    // 获取编号
    setCode(projectId) {
      const fieldCode = this.$refs.docform.getFieldValue('fieldCode')
      if (fieldCode && fieldCode !== '') {
      } else {
        const params = {
          projectId: projectId,
        }
        if (projectId) {
          axios({
            url: 'api/xcoa-mobile/v1/iam-audit-intometting/getFieldCode',
            method: 'get',
            params,
          }).then((res) => {
            this.$refs.docform.setFieldValue('fieldCode', res.data)
          })
        }
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';
.first-tr {
  td {
    background: #fff !important;
    border: none !important;
  }
}
</style>
