<!-- 服务编辑弹窗 -->
<template>
  <ele-modal
    :width="500"
    :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="styleResponsive ? { md: 5, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="
        styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
      "
    >
      <a-form-item label="名称" name="name">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入服务承诺名称"
          v-model:value="form.name"
        />
      </a-form-item>
      <a-form-item label="描述" name="summary">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入描述内容"
          v-model:value="form.summary"
        />
      </a-form-item>
      <a-form-item label="排序" name="sortNumber">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="排序"
          v-model:value="form.sortNumber"
        />
      </a-form-item>
      <a-form-item label="默认勾选" name="isDefault">
        <a-radio-group v-model:value="form.isDefault">
          <a-radio :value="1">是</a-radio>
          <a-radio :value="0">否</a-radio>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { message } from 'ant-design-vue/es';
  import type { FormInstance, Rule } from 'ant-design-vue/es/form';
  import { storeToRefs } from 'pinia';
  import { useThemeStore } from '@/store/modules/theme';
  import useFormData from '@/utils/use-form-data';
  import { addService, updateService } from '@/api/goods/service/index';
  import type { Service } from '@/api/goods/service/model';

  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

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

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

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

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

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

  // 表单数据
  const { form, resetFields, assignFields } = useFormData<Service>({
    serviceId: undefined,
    name: '',
    summary: '',
    isDefault: 1,
    sortNumber: 100
  });

  // 表单验证规则
  const rules = reactive<Record<string, Rule[]>>({
    name: [
      {
        required: true,
        message: '请输入服务承诺名称',
        type: 'string',
        trigger: 'blur'
      }
    ],
    summary: [
      {
        required: true,
        message: '请输入服务承诺描述',
        type: 'string',
        trigger: 'blur'
      }
    ],
    isDefault: [
      {
        required: true,
        message: '请设置是否勾选',
        type: 'number',
        trigger: 'blur'
      }
    ],
    sortNumber: [
      {
        required: true,
        message: '请填写排序',
        type: 'number',
        trigger: 'blur'
      }
    ]
  });

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const saveOrUpdate = isUpdate.value ? updateService : addService;
        saveOrUpdate(form)
          .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) {
          assignFields(props.data);
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
        formRef.value?.clearValidate();
      }
    }
  );
</script>
