<!-- 文档编辑弹窗 -->
<template>
  <ele-modal
    :width="400"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '修改文档' : '添加文档'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ md: { span: 24 }, sm: { span: 24 } }"
      :wrapper-col="{ md: { span: 24 }, sm: { span: 24 } }"
      layout="vertical"
    >
      <a-form-item label="上级目录" name="parentId">
        <docs-select
          :data="docs"
          placeholder="请选择上级目录"
          v-model:value="form.parentId"
        />
      </a-form-item>
      <a-form-item label="文档标题" name="title">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入文档标题"
          v-model:value="form.title"
          @pressEnter="save"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import DocsSelect from './docs-select.vue';
  import { addDocs, updateDocs } from '@/api/cms/docs';
  import type { Docs } from '@/api/cms/docs/model';
  import { FormInstance } from 'ant-design-vue/es/form';

  const useForm = Form.useForm;

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 分类
    docs?: Docs[];
    // 修改回显的数据
    data?: Docs | null;
  }>();

  // 是否是修改
  const isUpdate = ref(false);
  // 提交状态
  const loading = ref(false);
  const formRef = ref<FormInstance | null>(null);

  // 表单数据
  const form = reactive<Docs>({
    // 文档id
    docsId: 0,
    // 文档标题
    title: '',
    bookId: undefined,
    // 上级文档
    parentId: 0,
    // 用户ID
    userId: '',
    // 所属门店ID
    shopId: '',
    // 排序
    sortNumber: 100,
    // 备注
    comments: '',
    // 内容
    content: ''
  });

  // 表单验证规则
  const rules = reactive({
    title: [
      {
        required: true,
        message: '请输入文档标题',
        type: 'string',
        trigger: 'blur'
      }
    ],
    sortNumber: [
      {
        required: true,
        message: '请输入排序号',
        type: 'number',
        trigger: 'blur'
      }
    ]
  });

  const { resetFields, validate, validateInfos } = useForm(form, rules);

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const docsData = {
          ...form,
          parentId: form.parentId || 0
        };
        const saveOrUpdate = isUpdate.value ? updateDocs : addDocs;
        saveOrUpdate(docsData)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if(props.data){
          assignObject(form, props.data);
          if (props.data?.isUpdate) {
            isUpdate.value = true;
          } else {
            form.title = '';
            form.parentId = form.docsId;
            form.docsId = undefined;
            form.content = undefined;
            isUpdate.value = false;
          }
        }
      }
    }
  );
</script>
