<!-- 分类编辑弹窗 -->
<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">
            <CategorySelect
              :data="categoryList"
              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"
              @pressEnter="save"
            />
          </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>
      </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 CategorySelect from './category-select.vue';
  import {
    addArticleCategory,
    updateArticleCategory
  } from '@/api/cms/category';
  import type { ArticleCategory } from '@/api/cms/category/model';

  const useForm = Form.useForm;

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

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

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

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

  // 表单数据
  const form = reactive<ArticleCategory>({
    // 文章分类id
    categoryId: 0,
    // 文章分类
    title: '',
    // 上级分类
    parentId: 0,
    // 封面图
    avatar: '',
    // 用户ID
    userId: '',
    // 所属门店ID
    shopId: '',
    // 排序
    sortNumber: 100,
    // 备注
    comments: '',
    // 状态
    status: ''
  });

  // 表单验证规则
  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 categoryData = {
          ...form,
          parentId: form.parentId || 0
        };
        const saveOrUpdate = isUpdate.value
          ? updateArticleCategory
          : addArticleCategory;
        saveOrUpdate(categoryData)
          .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.categoryId;
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
