<template>
  <div>
    <!-- 勘误 -->
    <sd-detail-modal
      ref="oaDetail"
      :record-id="fwId"
      page-id="audit/km/knowledge/kmKnowledge/kmKnowledge"
      :modal-props="{ width: 1000 }"
      @saved="fwEditSaved"
      @actionBtnClick="actionBtnClick"
    >
      <template v-slot="{ model, fields }">
        <table>
          <tr>
            <sd-form-item-td name="title" :colspan="3" />
          </tr>
          <tr>
            <!-- 所属分类 -->
            <sd-form-item-td name="category" :colspan="3">
              <template v-slot:read-and-edit="{ editable }">
                <km-tree-select
                  v-model="model.category"
                  :editable="editable"
                  tree-data-url="api/xcoa-mobile/v1/km-knowledge-category/knowledge-category-tree?pageId=kmKnowledge"
                ></km-tree-select>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 标签 -->
            <sd-form-item-td name="label" :colspan="3">
              <template v-slot:read-and-edit="{ editable }">
                <km-tag-picker v-model="model.label" :editable="editable" />
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 业务领域 -->
            <sd-form-item-td name="businessArea" :colspan="3">
              <template v-slot:read-and-edit="{ editable }">
                <km-business-area-picker v-model="model.businessArea" :read-only="!editable" />
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 摘要 -->
            <sd-form-item-td name="abstractMessage" :colspan="3">
              <a-textarea
                v-model="model.abstractMessage"
                :rows="4"
                placeholder="请输入摘要"
                allow-clear
              />
            </sd-form-item-td>
          </tr>

          <tr>
            <!-- 文档附件 -->
            <sd-form-item-td name="applyAttachment" :colspan="3" />
          </tr>
          <tr>
            <!-- 封面上传 -->
            <sd-form-item-td name="cover">
              <template v-slot:read-and-edit="{ editable }">
                <oa-upload-img
                  v-model="model.cover"
                  :read-only="!editable"
                  :group-id="JSON.parse(fields.coverAttachment.value).value"
                ></oa-upload-img>
              </template>
            </sd-form-item-td>
            <!-- 默认集成该父类权限 -->
            <sd-form-item-td name="authFollowParent" />
          </tr>
          <tr v-if="model.authFollowParent === '0'">
            <!-- 是否允许评价 -->
            <sd-form-item-td name="authComment" />
            <!-- 附件是否允许下载 -->
            <sd-form-item-td name="authDownload" />
          </tr>

          <tr v-if="model.authFollowParent === '0'">
            <!-- 发布用户 -->
            <sd-form-item-td name="applyPerson" />
            <!-- 发布部门 -->
            <sd-form-item-td name="applyDept" />
          </tr>
          <tr>
            <!-- 作者 -->
            <sd-form-item-td name="creatorName" />
            <!-- 所属部门 -->
            <sd-form-item-td name="createDeptName" />
          </tr>
        </table>
      </template>
    </sd-detail-modal>
  </div>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
import components from './_import-components/km-knowledage-editmodal-import'

export default {
  name: 'KmKnowledageEditmodal',
  metaInfo: {
    title: 'OaEditForm',
  },
  components,
  props: {
    fwId: {
      type: Number,
      default: null,
    },
    showEditMoldal: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      docId: null,
    }
  },
  watch: {
    showEditMoldal: function(newValue) {
      this.docId = undefined
      this.$refs.oaDetail.show()
    },
  },
  mounted() {},
  methods: {
    // 勘误保存
    fwEditSaved() {
      Modal.success({
        title: '勘误成功！',
        onOk: () => {
          // 获取详情
          this.$emit('queryKnowledgeDetails')
        },
      })
    },

    actionBtnClick(evt, btn) {
      if (btn.buttonId === 'save') {
        evt.waitUntil(
          new Promise((resolve, reject) => {
            const params = this.$refs.oaDetail.getFieldsValue()
            if (params.authFollowParent === '0') {
              if (params.applyDept.length === 0 && params.applyPerson.length === 0) {
                Message.warn('发布人员和发布部门不能同时为空')
                evt.preventDefault()
                resolve()
              } else {
                resolve()
              }
            } else {
              resolve()
            }
          })
        )
      }
    },
  },
}
</script>

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