<template>
  <audit-form-top-banner
    :handel-publish="$route.query.mode !== 'VIEW' && handelPublish"
    :handel-save-form="$route.query.mode !== 'VIEW' && handleSave"
    @handelSaveForm="handleSave"
    @handelPublish="handelPublish"
  >
    <sd-detail-form
      ref="docform"
      page-id="audit/work/leavemeeting/iamLeaveMeeting"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      :read-only="$route.query.mode === 'VIEW'"
      @close="close(true)"
      @saved="saved"
    >
      <template v-slot="{ model }">
        <table>
          <tr>
            <td style="padding-left:5px;border:none">
              <audit-advanced-group
                :expand="basicExpand"
                :expand-str="'basicExpand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="basicExpand">
          <tr>
            <!-- 文件标题 -->
            <sd-form-item-td name="docTitle" :colspan="3"> </sd-form-item-td>
          </tr>
          <tr>
            <!-- 被审计单位 -->
            <sd-form-item-td name="auditUnitSelect" />
            <!-- 文件编号 -->
            <sd-form-item-td name="fileNumber" />
          </tr>
          <tr>
            <!-- 文件描述 -->
            <sd-form-item-td name="fileDesc" :colspan="3" />
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3" />
          </tr>
          <tr>
            <!-- 编制人员 -->
            <sd-form-item-td name="authName" />
            <!-- 编制日期 -->
            <sd-form-item-td name="authTime" />
          </tr>
          <tr v-show="false">
            <!-- 被审计单位名称 -->
            <sd-form-item-td name="auditUnitName" />
            <!-- 被审计单位ID -->
            <sd-form-item-td name="auditUnitId" />
          </tr>
          <tr v-show="false">
            <!-- 项目ID -->
            <sd-form-item-td name="projectId" />
            <!-- 状态 -->
            <sd-form-item-td name="reservestring1" />
          </tr>
        </table>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

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

export default {
  name: 'IamAuditLeavemeetingForm',
  metaInfo: {
    title: '离场会',
  },
  components: {
    ...components,
    auditFormTopBanner,
    auditAdvancedGroup,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      basicExpand: true,
      publishFlag: true,
      saveFlag: true,
      labelArray: [],
    }
  },
  mounted() {
    setTimeout(() => {
      // this.setFileNumber()
      this.setAuditUnitSelect(this.$root.$route.query.projectId)
    }, 1000)
  },
  created() {
    if (this.$route.query.type) {
      if (this.$route.query.type === '1') {
        this.publishFlag = false
      } else if (this.$route.query.type === '0') {
        this.publishFlag = false
        this.saveFlag = false
      }
    }

    axios({
      url:
        `api/xcoa-mobile/v1/iamauditproject/getProjectInfoById?id=` +
        this.$root.$route.query.projectId,
      method: 'get',
    }).then((res) => {
      if (res.data) {
        const itemStatus = res.data.itemStatus
        if (['05', '06', '07'].includes(itemStatus)) {
          this.saveFlag = false
          this.publishFlag = false
        }
      } else {
        this.saveFlag = false
        this.publishFlag = false
      }
    })
  },
  methods: {
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    // 保存
    saveForm() {
      var selectVal = this.$refs.docform.getFieldValue('auditUnitSelect')
      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({
          auditUnitName: names.join(','),
          auditUnitId: ids.join(','),
        })
      }
      if (this.$refs.docform.getFieldValue('projectId') === undefined) {
        const projectId = this.$root.$route.query.projectId
        this.$refs.docform.setFieldValue('projectId', parseInt(projectId))
      }
      this.$refs.docform.saveBtnClick()
    },
    handleSave() {
      // 发布，保存
      if (this.$refs.docform.getFieldValue('reservestring1') !== '已结束') {
        this.$refs.docform.setFieldValue('reservestring1', '起草中')
      }
      this.saveForm()
    },
    handelPublish() {
      // 发布，保存
      this.$refs.docform.setFieldValue('reservestring1', '已结束')
      this.saveForm()
    },
    saved() {
      const reservestring1 = this.$refs.docform.getFieldValue('reservestring1')
      let content = '保存成功！'
      if (reservestring1 === '已结束') {
        content = '发布成功！'
      }

      Message.success({ content: content }, 1).then(() => {
        this.close(true)
      })
    },
    setFileNumber() {
      if (this.$refs.docform.getFieldValue('fileNumber') === undefined) {
        const params = {
          projectId: parseInt(this.$root.$route.query.projectId),
        }
        axios({
          url: 'api/xcoa-mobile/v1/iamleavemeeting/findFileNumber',
          method: 'post',
          params,
        }).then((res) => {
          this.$refs.docform.setFieldValue('fileNumber', res.data)
        })
      }
    },
    // 获取被审计单位
    setAuditUnitSelect(projectId) {
      const params = {
        projectId: projectId,
      }
      // 有record说明是已经保存过的文件，不需要重新赋值被审计单位
      if (projectId && !this.$route.query.record) {
        axios({
          url: 'api/xcoa-mobile/v1/iam-audit-intometting/findAuditUnit',
          method: 'post',
          params,
        }).then((res) => {
          this.$refs.docform.setFieldValue('auditUnitSelect', res.data)
        })
      }
    },
  },
}
</script>

<style module lang="scss">
@import '@/webflow/sd-flow-form.scss';
.ant-card-body {
  padding: 12px;

  :global(.buttons_sd-detail-form_common) {
    :global(.ant-form-item-control-wrapper) {
      padding-left: 24%;
      padding-top: 4%;
    }
  }
  :global(.title_sd-detail-form_common) {
    font-size: 1.5em;
    text-align: center;
  }

  .tr_border {
    box-sizing: border-box;
    border-right: 1px solid #e8e8e8;
    border-left: 1px solid #e8e8e8;
  }
}
</style>
