<template>
  <a-card :bordered="false">
    <a-form
      ref="formRef"
      :model="form"
      :label-col="styleResponsive ? { md: 3, sm: 5, xs: 24 } : { flex: '120px' }"
      :wrapper-col="styleResponsive ? { md: 9, sm: 19, xs: 24 } : { flex: '1' }"
    >
      <a-form-item label="订单通知" name="notifyOrderEnabled">
        <a-switch v-model:checked="form.notifyOrderEnabled" />
        <span class="ele-text-secondary" style="margin-left: 8px; font-size: 12px;">用户下单后发送通知</span>
      </a-form-item>
      <a-form-item label="支付通知" name="notifyPayEnabled">
        <a-switch v-model:checked="form.notifyPayEnabled" />
        <span class="ele-text-secondary" style="margin-left: 8px; font-size: 12px;">用户支付成功后发送通知</span>
      </a-form-item>
      <a-form-item label="退款通知" name="notifyRefundEnabled">
        <a-switch v-model:checked="form.notifyRefundEnabled" />
        <span class="ele-text-secondary" style="margin-left: 8px; font-size: 12px;">退款申请/完成后发送通知</span>
      </a-form-item>
      <a-form-item label="发货通知" name="notifyShipEnabled">
        <a-switch v-model:checked="form.notifyShipEnabled" />
        <span class="ele-text-secondary" style="margin-left: 8px; font-size: 12px;">商家发货后发送通知</span>
      </a-form-item>
      <a-form-item label="签收通知" name="notifySignEnabled">
        <a-switch v-model:checked="form.notifySignEnabled" />
        <span class="ele-text-secondary" style="margin-left: 8px; font-size: 12px;">用户确认收货后发送通知</span>
      </a-form-item>
      <a-form-item :wrapper-col="styleResponsive ? { md: { offset: 3, span: 9 } } : { flex: '1' }">
        <a-button type="primary" :loading="loading" @click="save">保存</a-button>
      </a-form-item>
    </a-form>
  </a-card>
</template>

<script lang="ts" setup>
import { 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 = 'notify';
const themeStore = useThemeStore();
const { styleResponsive } = storeToRefs(themeStore);
const loading = ref(false);
const formRef = ref<FormInstance | null>(null);

const { form, assignFields } = useFormData<ShopSetting>({
  notifyOrderEnabled: true,
  notifyPayEnabled: true,
  notifyRefundEnabled: true,
  notifyShipEnabled: true,
  notifySignEnabled: true,
});

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

const load = async () => {
  try {
    const values = await getShopSettingCategoryValues(category);
    if (values && Object.keys(values).length > 0) {
      assignFields(values);
      form.notifyOrderEnabled = toBool((values as any).notifyOrderEnabled);
      form.notifyPayEnabled = toBool((values as any).notifyPayEnabled);
      form.notifyRefundEnabled = toBool((values as any).notifyRefundEnabled);
      form.notifyShipEnabled = toBool((values as any).notifyShipEnabled);
      form.notifySignEnabled = toBool((values as any).notifySignEnabled);
    }
  } catch (e) {
    // 未配置时忽略
  }
};

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

onMounted(load);
</script>
