<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="dealerEnabled">
        <a-switch
          v-model:checked="form.dealerEnabled"
          checked-children="开"
          un-checked-children="关"
        />
      </a-form-item>
      <a-form-item label="开放申请分销商" name="dealerApplyEnabled">
        <a-switch
          v-model:checked="form.dealerApplyEnabled"
          checked-children="开"
          un-checked-children="关"
        />
      </a-form-item>
      <a-form-item label="一级佣金比例" name="dealerLevel1Rate">
        <a-input-number
          v-model:value="form.dealerLevel1Rate"
          :min="0"
          :max="100"
          :precision="2"
          placeholder="请输入比例"
          style="width: 200px"
        />
        <span style="margin-left: 8px; color: #888">%</span>
      </a-form-item>
      <a-form-item label="二级佣金比例" name="dealerLevel2Rate">
        <a-input-number
          v-model:value="form.dealerLevel2Rate"
          :min="0"
          :max="100"
          :precision="2"
          placeholder="请输入比例"
          style="width: 200px"
        />
        <span style="margin-left: 8px; color: #888">%</span>
      </a-form-item>
      <a-form-item label="最低提现金额" name="dealerWithdrawMin">
        <a-input-number
          v-model:value="form.dealerWithdrawMin"
          :min="0"
          :precision="2"
          placeholder="请输入最低提现金额"
          style="width: 200px"
        />
        <span style="margin-left: 8px; color: #888">元</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 = 'dealer';

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

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

const { form, assignFields } = useFormData<ShopSetting>({
  dealerEnabled: false,
  dealerApplyEnabled: false,
  dealerLevel1Rate: 10,
  dealerLevel2Rate: 5,
  dealerWithdrawMin: 10
});

const rules = reactive({
  dealerLevel1Rate: [
    {
      required: true,
      message: '请输入一级佣金比例',
      type: 'number',
      trigger: 'blur'
    }
  ],
  dealerLevel2Rate: [
    {
      required: true,
      message: '请输入二级佣金比例',
      type: 'number',
      trigger: 'blur'
    }
  ],
  dealerWithdrawMin: [
    {
      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.dealerEnabled = toBool((values as any).dealerEnabled);
      form.dealerApplyEnabled = toBool((values as any).dealerApplyEnabled);
      // 数值字段转换
      if (values.dealerLevel1Rate !== undefined) {
        form.dealerLevel1Rate = Number(values.dealerLevel1Rate);
      }
      if (values.dealerLevel2Rate !== undefined) {
        form.dealerLevel2Rate = Number(values.dealerLevel2Rate);
      }
      if (values.dealerWithdrawMin !== undefined) {
        form.dealerWithdrawMin = Number(values.dealerWithdrawMin);
      }
    }
  } 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>
