<!-- 规格编辑弹窗 -->
<template>
  <ele-modal
    :width="520"
    :visible="visible"
    :maskClosable="false"
    :title="isUpdate ? '编辑规格' : '添加规格'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ flex: '80px' }"
      :wrapper-col="{ flex: '1' }"
    >
      <a-form-item label="规格名称" name="specName">
        <a-input
          allow-clear
          placeholder="请输入规格名称，如：颜色、尺码、材质"
          v-model:value="form.specName"
        />
      </a-form-item>
      <a-form-item label="状态" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio :value="0">显示</a-radio>
          <a-radio :value="1">隐藏</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="排序号" name="sortNumber">
        <a-input-number
          :min="0"
          :max="9999"
          class="ele-fluid"
          placeholder="数值越小越靠前"
          v-model:value="form.sortNumber"
        />
      </a-form-item>
      <a-form-item label="备注" name="comments">
        <a-textarea
          :rows="3"
          :maxlength="200"
          placeholder="可选备注说明"
          v-model:value="form.comments"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import { addShopSpec, updateShopSpec } from '@/api/shop/shopSpec';
  import { ShopSpec } from '@/api/shop/shopSpec/model';
  import { FormInstance } from 'ant-design-vue/es/form';

  const isUpdate = ref(false);

  const props = defineProps<{
    visible: boolean;
    data?: ShopSpec | null;
  }>();

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

  const loading = ref(false);
  const formRef = ref<FormInstance | null>(null);

  const form = reactive<ShopSpec>({
    specId: undefined,
    specName: undefined,
    comments: undefined,
    status: 0,
    sortNumber: 100
  });

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

  // 表单验证规则
  const rules: Record<string, any> = {
    specName: [
      {
        required: true,
        type: 'string',
        message: '请填写规格名称',
        trigger: 'blur'
      }
    ]
  };

  const save = () => {
    if (!formRef.value) return;
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const saveOrUpdate = isUpdate.value ? updateShopSpec : addShopSpec;
        saveOrUpdate({ ...form })
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          assignObject(form, props.data);
          isUpdate.value = true;
        } else {
          // 重置为默认值
          Object.assign(form, {
            specId: undefined,
            specName: undefined,
            comments: undefined,
            status: 0,
            sortNumber: 100
          });
          isUpdate.value = false;
        }
      } else {
        formRef.value?.resetFields();
      }
    },
    { immediate: true }
  );
</script>
