<template>
  <a-card :bordered="false">
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 3, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="styleResponsive ? { md: 9, sm: 19, xs: 24 } : { flex: '1' }"
    >
      <a-form-item label="启用积分功能" name="pointsEnabled">
        <a-switch
          v-model:checked="form.pointsEnabled"
          checked-children="开"
          un-checked-children="关"
        />
      </a-form-item>
      <a-form-item label="启用签到积分" name="pointsSigninEnabled">
        <a-switch
          v-model:checked="form.pointsSigninEnabled"
          checked-children="开"
          un-checked-children="关"
        />
      </a-form-item>
      <a-form-item label="每日签到积分" name="pointsSigninAmount">
        <a-input-number
          v-model:value="form.pointsSigninAmount"
          :min="1"
          :max="99999"
          placeholder="请输入签到积分数"
          style="width: 200px"
        />
        <span style="margin-left: 8px; color: #888">积分</span>
      </a-form-item>
      <a-form-item label="消费积分比例" name="pointsOrderRate">
        <a-input-number
          v-model:value="form.pointsOrderRate"
          :min="1"
          :max="99999"
          placeholder="请输入比例"
          style="width: 200px"
        />
        <span style="margin-left: 8px; color: #888">每消费 N 元得 1 积分</span>
      </a-form-item>
      <a-form-item label="积分兑换比例" name="pointsExchangeRate">
        <a-input-number
          v-model:value="form.pointsExchangeRate"
          :min="1"
          :max="99999"
          placeholder="请输入比例"
          style="width: 200px"
        />
        <span style="margin-left: 8px; color: #888">N 积分兑换 1 元</span>
      </a-form-item>
      <a-form-item label="保存">
        <a-button type="primary" :loading="loading" @click="save">保存</a-button>
      </a-form-item>
    </a-form>
  </a-card>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import { useThemeStore } from '@/store/modules/theme';
import { storeToRefs } from 'pinia';
import { FormInstance } from 'ant-design-vue/es/form';
import useFormData from '@/utils/use-form-data';
import { batchSaveShopSetting, getShopSettingCategoryValues } from '@/api/shop/shopSetting';
import type { ShopSetting } from '@/api/shop/shopSetting/model';

const category = 'points';

const themeStore = useThemeStore();
const { styleResponsive } = storeToRefs(themeStore);

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

const { form, assignFields } = useFormData<ShopSetting>({
  pointsEnabled: true,
  pointsSigninEnabled: true,
  pointsSigninAmount: 10,
  pointsOrderRate: 10,
  pointsExchangeRate: 100
});

const rules = reactive({
  pointsSigninAmount: [
    {
      required: true,
      message: '请输入每日签到积分数',
      type: 'number',
      trigger: 'blur'
    }
  ],
  pointsOrderRate: [
    {
      required: true,
      message: '请输入消费积分比例',
      type: 'number',
      trigger: 'blur'
    }
  ],
  pointsExchangeRate: [
    {
      required: true,
      message: '请输入积分兑换比例',
      type: 'number',
      trigger: 'blur'
    }
  ]
});

const toBool = (val: any): boolean =>
  val === true || val === 'true' || val === '1' || val === 1;

const load = async () => {
  try {
    const values = await getShopSettingCategoryValues(category);
    if (values && Object.keys(values).length > 0) {
      assignFields(values);
      // 布尔值转换
      form.pointsEnabled = toBool((values as any).pointsEnabled);
      form.pointsSigninEnabled = toBool((values as any).pointsSigninEnabled);
      // 数值字段转换
      if (values.pointsSigninAmount !== undefined) {
        form.pointsSigninAmount = Number(values.pointsSigninAmount);
      }
      if (values.pointsOrderRate !== undefined) {
        form.pointsOrderRate = Number(values.pointsOrderRate);
      }
      if (values.pointsExchangeRate !== undefined) {
        form.pointsExchangeRate = Number(values.pointsExchangeRate);
      }
    }
  } catch (e: any) {
    // 未配置时忽略
  }
};

const save = () => {
  if (!formRef.value) return;
  formRef.value
    .validate()
    .then(() => {
      loading.value = true;
      batchSaveShopSetting(category, form as Record<string, unknown>)
        .then(() => {
          message.success('保存成功');
        })
        .catch((e) => {
          message.error(e.message);
        })
        .finally(() => {
          loading.value = false;
        });
    })
    .catch(() => {});
};

onMounted(load);
</script>
