import { apiClient } from '@/api';
import { getWalmartConfig } from '@/api/card-walmart-order';
import {
  KamiApiCardInfoWalmartV1RedeemConfigGetResRedeemOrderScheduleStrategyEnum,
  type KamiApiCardInfoWalmartV1RedeemConfigSetReqRedeemOrderScheduleStrategyEnum,
  type KamiApiCardInfoWalmartV1RedeemConfigGetRes
} from '@/api/generated';
import {
  Button,
  Modal,
  Form,
  FormItem,
  Switch,
  type FormInstance,
  InputNumber,
  Select,
  Option
} from '@arco-design/web-vue';
import { defineComponent, onMounted, reactive, ref } from 'vue';

export default defineComponent({
  name: 'cardJdConfig',
  setup() {
    const state = reactive<{
      visible: boolean;
    }>({
      visible: false
    });
    const formData = ref<Required<KamiApiCardInfoWalmartV1RedeemConfigGetRes>>({
      isAllowDifferentAmount: true,
      isAllowDifferentSucceedCallback: true,
      isAllowDifferentFailCallback: true,
      redeemCardMinAmount: 0,
      isAllowCompensatedCallback: true,
      redeemCardRate: 100,
      isCardAllowRepeated: false,
      maxAccountRedeemCount: 20,
      redeemOrderScheduleStrategy:
        KamiApiCardInfoWalmartV1RedeemConfigGetResRedeemOrderScheduleStrategyEnum.Normal
    });
    const formRef = ref<FormInstance>();
    onMounted(() => {
      getWalmartConfig().then(res => {
        formData.value = res.data;
      });
    });
    return () => (
      <>
        <Button
          type='primary'
          status='normal'
          onClick={() => {
            state.visible = !state.visible;
            getWalmartConfig().then(res => {
              formData.value = res.data;
            });
          }}
        >
          设置
        </Button>
        <Modal
          title='充值设置'
          v-model:visible={state.visible}
          onBeforeOk={async (done: (closed: boolean) => void) => {
            if (await formRef.value.validate()) {
              done(false);
            }
            try {
              await apiClient.apiCardInfoWalmartConfigSetPost({
                kamiApiCardInfoWalmartV1RedeemConfigSetReq: {
                  ...formData.value,
                  redeemOrderScheduleStrategy: formData.value
                    .redeemOrderScheduleStrategy as unknown as KamiApiCardInfoWalmartV1RedeemConfigSetReqRedeemOrderScheduleStrategyEnum
                }
              });
              done(true);
            } catch {
              done(false);
            }
          }}
        >
          <Form
            ref={formRef}
            model={formData}
            labelColProps={{ span: 12 }}
            wrapperColProps={{ span: 12 }}
          >
            <FormItem field='redeemCardMinAmount' label='最小充值金额' required>
              <InputNumber v-model={formData.value.redeemCardMinAmount} />
            </FormItem>
            <FormItem
              field='isAllowDifferentAmount'
              label='是否兑换(金额异议)'
              required
            >
              <Switch v-model={formData.value.isAllowDifferentAmount} />
            </FormItem>
            <FormItem
              field='isAllowDifferentSucceedCallback'
              label='成功是否回调(金额异议)'
              required
            >
              <Switch
                v-model={formData.value.isAllowDifferentSucceedCallback}
              />
            </FormItem>
            <FormItem
              field='isAllowDifferentFailCallback'
              label='失败是否回调(金额异议)'
              required
            >
              <Switch v-model={formData.value.isAllowDifferentFailCallback} />
            </FormItem>
            <FormItem
              field='isAllowCompensatedCallback'
              label='补卡自动回调'
              required
            >
              <Switch v-model={formData.value.isAllowCompensatedCallback} />
            </FormItem>
            <FormItem field='isCardAllowRepeated' label='允许重复上传' required>
              <Switch v-model={formData.value.isCardAllowRepeated} />
            </FormItem>
            <FormItem
              field='maxAccountRedeemCount'
              label='账户最大充值次数'
              required
            >
              <InputNumber v-model={formData.value.maxAccountRedeemCount} />
            </FormItem>
            <FormItem field='redeemCardRate' label='充值速率(单/分钟)' required>
              <InputNumber v-model={formData.value.redeemCardRate} />
            </FormItem>
            <FormItem
              field='redeemOrderScheduleStrategy'
              label='充值策略'
              required
            >
              <Select
                v-model={formData.value.redeemOrderScheduleStrategy}
                required
              >
                <Option
                  label='ck轮训'
                  value={
                    KamiApiCardInfoWalmartV1RedeemConfigGetResRedeemOrderScheduleStrategyEnum.Normal
                  }
                ></Option>
                <Option
                  label='随机'
                  value={
                    KamiApiCardInfoWalmartV1RedeemConfigGetResRedeemOrderScheduleStrategyEnum.Random
                  }
                ></Option>
              </Select>
            </FormItem>
          </Form>
        </Modal>
      </>
    );
  }
});
