import { mountComponent, useUnmountComponent } from "@/utils/mount-component";
import Settlement from "./settlement.vue";
import { BranchModel } from "@/model/myBranchList";
import { ref } from "vue";
import { Toast } from "vant";
import { useUpdateUserInfo } from "@/hooks";

const branchModel = new BranchModel();

export type UserInfo = {
	openid: string;
	nickName: string;
	headImage: string;
	achievement: number;
	achievementSettled: number;
};

export const showSettlement = (props: UserInfo) => {
	const { unmount } = mountComponent(Settlement, props);
	return {
		unmount,
	};
};

export const useSettlementIntegral = (props: { openid: string; settlePoints: number }) => {
	const { unmountComponent } = useUnmountComponent();
	const loading = ref(false);
	const settlementIntegral = async () => {
		loading.value = true;
		const data = await branchModel.integralSettlement(props);
		if (data.code === 200) {
			Toast.success("结算成功");
			unmountComponent();
			useUpdateUserInfo();
		} else {
			Toast.fail(data.msg);
		}
		loading.value = false;
	};
	return {
		loading,
		settlementIntegral,
	};
};
