<!-- 文档编辑弹窗 -->
<template>
  <ele-modal
    :width="620"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '修改文档' : '添加文档'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 7 }, sm: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 24 } }"
    >
      <a-row :gutter="16">
        <a-col :md="12" :sm="24" :xs="24">
          <a-form-item label="上级目录" v-bind="validateInfos.parentId">
            <docs-select
              :data="docs"
              placeholder="请选择上级目录"
              v-model:value="form.parentId"
            />
          </a-form-item>
          <a-form-item label="文档标题" v-bind="validateInfos.title">
            <a-input
              allow-clear
              :maxlength="20"
              placeholder="请输入文档标题"
              v-model:value="form.title"
            />
          </a-form-item>
          <a-form-item label="排序号" v-bind="validateInfos.sortNumber">
            <a-input-number
              :min="0"
              :max="99999"
              class="ele-fluid"
              placeholder="请输入排序号"
              v-model:value="form.sortNumber"
            />
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24" :xs="24">
          <a-form-item label="备注">
            <a-textarea
              :rows="4"
              :maxlength="200"
              placeholder="请输入备注"
              v-model:value="form.comments"
            />
          </a-form-item>
        </a-col>
        <div style="margin-bottom: 22px">
          <a-divider />
        </div>
      </a-row>
    </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';

  const useForm = Form.useForm;

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

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

  // 是否是修改
  const isUpdate = ref(false);

  // 提交状态
  const loading = ref(false);

  // 表单数据
  const form = reactive<Docs>({
    // 文档id
    docsId: 0,
    // 文档标题
    title: '',
    // 上级文档
    parentId: 0,
    // 封面图
    avatar: '',
    // 用户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 = () => {
    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);
          isUpdate.value = true;
        } else {
          form.parentId = props.docsId;
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
