<template>
  <span>
    <audit-form-top-banner
      :handel-save-form="saveForm"
      :handel-publish="handelPublish"
      :form-data="mode"
      :hiden-or-show="hidenOR0Show"
      @handelSaveForm="saveForm"
      @handelPublish="handelPublish"
    >
      <sd-detail-form
        ref="docform"
        page-id="audit/preparation/material/iamAuditMaterial"
        :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 style="display:none">
              <!-- projectId -->
              <sd-form-item-td name="projectId" :colspan="3" />
            </tr>
            <tr>
              <!-- 资料名称 -->
              <sd-form-item-td name="materialTitle" :colspan="3" />
            </tr>
            <tr>
              <!-- 被审计单位 -->
              <sd-form-item-td name="auditedUnit"> </sd-form-item-td>
              <!-- 资料编号 -->
              <sd-form-item-td name="materialCode">
                <template v-slot:read-and-edit="{ editable }">
                  {{ model.materialCode }}
                </template>
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 资料描述 -->
              <sd-form-item-td name="materialDesc" :colspan="3">
                <template v-slot:read-and-edit="{ editable }">
                  <a-textarea
                    v-if="editable"
                    v-model="model.materialDesc"
                    :auto-size="{ minRows: 3, maxRows: 3 }"
                  ></a-textarea>
                  <span v-else>{{ model.materialDesc }}</span>
                </template>
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 附件 -->
              <sd-form-item-td name="attachment" :colspan="3">
                <template v-slot:read-and-edit="{ editable }">
                  <XmAttachmentCus
                    v-model="model.attachment"
                    :group-id="JSON.parse(fields.attachment.value).value"
                    :read-only="!editable"
                  ></XmAttachmentCus>
                </template>
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 材料提供部门 -->
              <!-- <sd-form-item-td name="provisionDept" /> -->
              <sd-form-item-td name="provisionDept">
                <sd-group-picker
                  v-model="model.provisionDept"
                  :hierarchical="false"
                  :single="true"
                  @change="onChange"
                />
              </sd-form-item-td>
              <!-- 材料提供人 -->
              <!-- <sd-form-item-td name="provisionUser" /> -->
              <sd-form-item-td name="provisionUser">
                <sd-user-picker
                  v-model="model.provisionUser"
                  :disabled-keys="
                    model.provisionUser && model.provisionUser[0]
                      ? [model.provisionUser[0].code]
                      : []
                  "
                  :hierarchical="false"
                  :root-node="rootNode"
                  :single="true"
                />
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 备注 -->
              <sd-form-item-td name="remarks" :colspan="3">
                <template v-slot:read-and-edit="{ editable }">
                  <a-textarea
                    v-if="editable"
                    v-model="model.remarks"
                    :auto-size="{ minRows: 3, maxRows: 3 }"
                  ></a-textarea>
                  <span v-else>{{ model.remarks }}</span>
                </template>
              </sd-form-item-td>
            </tr>
            <tr>
              <!-- 编制人员 -->
              <sd-form-item-td name="creatorName" />
              <!-- 编制日期 -->
              <sd-form-item-td name="creationTime" />
            </tr>
            <tr>
              <!-- 资料类型 -->
              <sd-form-item-td name="materialType" :colspan="3" component="a-select" />
            </tr>
          </table>
        </template>
      </sd-detail-form>
    </audit-form-top-banner>
  </span>
</template>

<script>
import { Message } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import iamAuditpreparationService from '../iam-audit-preparation-service'
import auditFormTopBanner from '../../../../components/audit-form-top-banner.vue'
import components from './_import-components/iam-audit-material-form-import'
import XmAttachmentCus from '@product/iam/core/attachment/xm-attachment-cus'

export default {
  name: 'IamAuditMaterialForm',
  metaInfo: {
    title: '审计资料',
  },
  components: {
    ...components,
    auditFormTopBanner,
    XmAttachmentCus,
  },
  data() {
    return {
      rootNode: undefined,
      projectId: this.$route.query.projectId, // 项目ID
      projectInfo: null, // 项目信息
      mode: null,
      hidenOR0Show: true,
      publish: 0, // 为1 时是发布
      hasOperatePermissions: true,
    }
  },
  created() {
    const hasOperatePermissions = JSON.parse(this.$route.query?.hasOperatePermissions)
    this.hasOperatePermissions = hasOperatePermissions
  },
  mounted() {
    setTimeout(() => {
      // 获取编号
      // iamAuditpreparationService.getMaterialCode(this.projectId).then((res) => {
      //   this.$refs.docform.setFieldValue('materialCode', res.data)
      // })
      // 设置审计单位默认值
      if (!this.$route.query.record) {
        iamAuditpreparationService.getAuditedUnitById(this.projectId).then((res) => {
          this.$refs.docform.setFieldValue('auditedUnit', res.data)
          // 设置projectId
          this.$refs.docform.setFieldValue('projectId', parseFloat(this.projectId))
        })
      }
    }, 500)
  },
  methods: {
    onChange() {
      debugger
      const id = this.$refs.flow.getFieldValue('provisionDept')[0].code
      const name = this.$refs.flow.getFieldValue('provisionDept')[0].name
      this.rootNode = { code: id, name: name }
    },
    handleBtnClick(btn, item) {},
    setTitle(evt) {
      this.$refs.flow.setFieldValue('title', this.$refs.flow.getFieldValue('meansTitle'))
    },
    handelPublish() {
      // 发布，保存
      this.$refs.docform.setFieldValue('docStatus', '已结束')
      this.saveForm()
      this.publish = 1
    },
    // 保存
    saveForm() {
      this.$refs.docform.saveBtnClick()
    },
    // 页面加载完成后
    sdFormReady() {
      if (!this.hasOperatePermissions) {
        this.$refs.docform.formData.mode = 'VIEW'
      }
      if (this.$route.query?.mode) this.mode = this.$route.query.mode
      else this.mode = this.$refs.docform.formData.mode
      const ini = setInterval(() => {
        if (this.$refs.docform && this.$refs.docform.getFieldValue('docStatus')) {
          const docStatus = this.$refs.docform.getFieldValue('docStatus')
          if (docStatus === '已结束') {
            this.hidenOR0Show = false
          }
          clearInterval(ini)
        }
      }, 10)
    },
    saved() {
      if (this.publish === 1) {
        Message.success('发布成功', 1).then((res) => {
          this.close(true)
        })
      } else {
        Message.success('保存成功', 1).then((res) => {
          this.close(true)
        })
      }
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
  },
}
</script>

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