<template>
  <span>
    <sd-webflow ref="flow" :removed-tabs="['sdRelatedDoc']">
      <template v-slot:form="{ model }">
        <table>
          <tr>
            <!-- 申请人 -->
            <sd-form-item-td name="creatorName" />
            <!-- 申请日期 -->
            <sd-form-item-td name="creationTime" />
          </tr>

          <tr>
            <!-- 所属项目编号 -->

            <sd-form-item-td name="projectCode">
              <span>{{ model.projectCode }}</span></sd-form-item-td
            >
            <!-- 所属项目名称 -->
            <sd-form-item-td name="projectName" @click="mxRelSelect" />
          </tr>
          <tr>
            <!-- 申请文件 -->
            <sd-form-item-td name="applyFile" :colspan="3" @click="wjRelSelect" />
          </tr>

          <tr>
            <!-- 借阅期限 -->
            <sd-form-item-td name="borrowTerm" @change="computedDays" />
            <!-- 实际借阅人 -->
            <sd-form-item-td name="actualBorrower" />
          </tr>
          <tr>
            <!-- 借阅原因 -->
            <sd-form-item-td name="borrowReason" :colspan="3">
              <a-textarea v-model="model.borrowReason" :rows="3" />
            </sd-form-item-td>
          </tr>
        </table>
      </template>
    </sd-webflow>
    <AuditSelectProject :visible="visibleY" @listMxSelected="listMxSelected"></AuditSelectProject>
    <AuditSelectDossier
      ref="selectdossier"
      :code="_code"
      :visible="visibleWJ"
      @listWjSelected="listWjSelected"
    ></AuditSelectDossier>
  </span>
</template>

<script>
import { Modal } from 'ant-design-vue'
import AuditSelectProject from './audit-select-project.vue'
import AuditSelectDossier from './audit-select-dossier.vue'
import components from './_import-components/audit-borrow-form-import'

export default {
  name: 'AuditBorrowForm',
  metaInfo: {
    title: '档案借阅',
  },
  components: {
    ...components,
    AuditSelectProject,
    AuditSelectDossier,
  },
  data() {
    return {
      visibleWJ: false,
      visibleY: false,
    }
  },
  computed: {
    _code() {
      const form = this.$refs.flow
      if (form !== undefined) {
        const projectCode = form.getFieldValue('projectCode')
        return projectCode
      } else {
        return ''
      }
    },
  },
  methods: {
    // 借阅日期不能小于当前日期
    computedDays() {
      const form = this.$refs.flow
      const borrowTerm = form.getFieldValue('borrowTerm')
      if (new Date() >= borrowTerm) {
        Modal.warning({
          title: '提示',
          content: '借阅日期不能小于当前日期！',
          onOk: () => {
            form.setFieldValue('borrowTerm', null)
          },
        })
      }
    },

    // 归档项目选择器列表
    mxRelSelect() {
      this.visibleY = true
    },

    listMxSelected(keys, keyinfos) {
      this.$refs.flow.setFieldValue('archivesId', keyinfos[0].id)
      this.$refs.flow.setFieldValue('projectCode', keyinfos[0].archivesCode)
      this.$refs.flow.setFieldValue('projectName', keyinfos[0].projectName)
    },

    // 归档文件选择器列表
    wjRelSelect() {
      const form = this.$refs.flow
      const projectCode = form.getFieldValue('projectCode')

      if (projectCode !== null && projectCode !== undefined) {
        this.visibleWJ = true
        setTimeout(() => {
          const code = this.$refs.selectdossier.$refs.dossierDataTable.expressions[1]
          code.stringValue = projectCode
          this.$refs.selectdossier.$refs.dossierDataTable.expressions[1] = code
        }, 1000)
      } else {
        Modal.warning({
          title: '提示',
          content: '请先选择所属项目',
          onOk: () => {
            form.setFieldValue('projectCode', null)
          },
        })
      }
    },

    listWjSelected(keys, keyinfos) {
      const docTitles = []
      const dossierIds = []
      keyinfos.forEach((element) => {
        docTitles.push(element.docTitle)
        dossierIds.push(element.id)
      })
      this.$refs.flow.setFieldValue('applyFile', docTitles.join(','))
      this.$refs.flow.setFieldValue('dossierId', dossierIds.join(','))
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
