<template>
  <audit-form-top-banner
    :handel-save-form="filingflag ? saveForm : null"
    @handelSaveForm="saveForm"
  >
    <sd-detail-form
      ref="docform"
      form-id="iamDossierData"
      page-id="audit/archives/iamDossierData"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      :read-only="readOnly"
      @close="close(true)"
      @saved="saved"
    >
      <template v-slot="{ model, fields }">
        <table>
          <tr>
            <td style="border:none;padding-left:5px">
              <audit-advanced-group
                :expand="templateExpand"
                :expand-str="'templateExpand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="templateExpand">
          <tr>
            <!-- 文件名称 -->
            <sd-form-item-td name="docTitle" />
            <!-- 文件编号 -->
            <sd-form-item-td name="oanumber" />
          </tr>
          <tr>
            <!-- 备注 -->
            <sd-form-item-td name="remarks" :colspan="3">
              <a-textarea v-model="model.remarks" :rows="3" />
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 附件 -->
            <sd-form-item-td name="attachment" :colspan="3">
              <template v-slot:read-and-edit="{ editable }">
                <sd-attachment
                  v-model="model.attachment"
                  :group-id="JSON.parse(fields.attachment.value).value"
                  :read-only="!editable"
                ></sd-attachment>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 编制人员 -->
            <sd-form-item-td name="creatorName" />
            <!-- 编制日期 -->
            <sd-form-item-td name="creationTime" />
          </tr>
        </table>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import { getUserInfo } from '@/common/store-mixin'
import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditFormTopBanner from '../../components/audit-form-top-banner'
import auditAdvancedGroup from '../../components/audit-advanced-group.vue'
import auditAdvancedGroupMixins from '../../components/audit-advanced-group-mixins'
import components from './_import-components/audit-dossier-form-import'

export default {
  name: 'AuditDossierForm',
  metaInfo: {
    title: '案卷资料',
  },
  components: {
    ...components,
    auditAdvancedGroup,
    auditFormTopBanner,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      saveFlag: true,
      filingflag: true,
      templateExpand: true,
      readOnly: false,
    }
  },
  created() {
    const userInfo = getUserInfo()
    const userInfoName = this.$route.query.userInfoName
    const fileState = this.$route.query.fileState
    const fromFlag = this.$route.query.fromFlag
    if (fromFlag === 'projectList') {
      this.readOnly = false
      this.filingflag = true
    } else if (userInfo.name === userInfoName) {
      if (fileState === 'FILING') {
        this.readOnly = false
        this.filingflag = true
      } else {
        this.readOnly = true
        this.filingflag = false
      }
    } else {
      axios({
        url: 'api/xcoa-mobile/v1/iamarchivesmaintain/findHasPermission',
        method: 'post',
      }).then((res) => {
        if (res.data !== true || fileState !== 'FILING') {
          this.readOnly = true
          this.filingflag = false
        }
      })
    }
  },
  methods: {
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
    // 保存
    saveForm() {
      this.$refs.docform.validateFields().then(() => {
        const archivesId = this.$route.query.archivesId
        const parentId = this.$route.query.parentId
        this.$refs.docform.setFieldValue('archivesId', archivesId)
        this.$refs.docform.setFieldValue('parentId', parentId)
        this.$refs.docform.saveBtnClick()
      })
    },
    saved() {
      this.close(true)
    },
  },
}
</script>

<style module lang="scss">
@import '@/webflow/sd-flow-form.scss';
</style>
