import { create } from 'zustand';
import { query, queryLogin } from '@/modules/query';
import { toast } from 'react-toastify';
interface PayState {
  getPayUrl: (data: { money: number; subject: string }) => Promise<any>;
  codeUrl: string;
  setCodeUrl: (url: string) => void;
  user: any;
  money: number;
  subject: string;
  setMoney: (money: number) => void;
  setSubject: (subject: string) => void;
  init: () => Promise<void>;
}

export const usePayStore = create<PayState>((set) => ({
  money: 12800,
  setMoney: (money) => set({ money }),
  subject: 'kevisual 会员',
  setSubject: (subject) => set({ subject }),
  getPayUrl: async (data) => {
    const res = await query.post({
      path: 'alipay',
      key: 'pay',
      data,
    });
    if (res.code === 200) {
      if (res.data?.form) {
        set({ codeUrl: res.data?.form });
      }
    } else {
      toast.error(res.message);
    }
    return res;
  },
  codeUrl: '',
  setCodeUrl: (url) => set({ codeUrl: url }),
  user: null,
  init: async () => {
    const res = await queryLogin.cacheStore.getCurrentUser();
    console.log(res);
    if (res) {
      set({ user: res });
    }
  },
}));
