import {
	defineStore
} from 'pinia';

export const useStore = defineStore('counter', {
	state: () => ({
		user: {},
		yytjInfo: {},
		config: false,
		groupInfo: {}, // 团检信息
		combo_compare: [null, null, null], // 套餐对比
		save: {
			hospital: 1,
			doctor: 1,
		},
		checkup_type: {
			id: ""
		}, // 预约类型
		api_map: {},
		count: 0,
		loading: 0,
		buyInfo: {}, // 购买信息接口配置 预约页面使用 别的地方最好不要用
	}),
	actions: {
		getCheckupTypeId() {
			return JSON.parse(uni.getStorageSync('CHECKUP_TYPE') || '{}') || this.checkup_type;
		},
		setCheckupTypeId(data) {
			this.checkup_type = data;
			uni.setStorageSync('CHECKUP_TYPE', JSON.stringify(data));
		},
		setBuyInfo(info) {
			this.buyInfo = info;
			console.log(info);
			uni.setStorageSync('BUY_INFO', JSON.stringify(info));
		},
		getBuyInfo() {
			console.log(uni.getStorageSync('BUY_INFO'));
			return JSON.parse(uni.getStorageSync('BUY_INFO') || "{}") || this.buyInfo;
		},
		setYytjInfo(yytjInfo) {
			this.yytjInfo = yytjInfo;
			uni.setStorageSync('YYTJ_INFO', JSON.stringify(yytjInfo));
		},
		getYytjInfo() {
			console.log(uni.getStorageSync('YYTJ_INFO'));
			return JSON.parse(uni.getStorageSync('YYTJ_INFO') || '{}') || this.yytjInfo;
		},
		setUser(user) {
			this.user = user;
			uni.setStorageSync('USER', JSON.stringify(user));
		},
		getUser() {
			return JSON.parse(uni.getStorageSync('USER')) || this.user;
		},
		setGroupInfo(info) {
			this.groupInfo = info;
			uni.setStorageSync('GROUP_INFO', JSON.stringify(info));
		},
		getGroupInfo() {
			return JSON.parse(uni.getStorageSync('GROUP_INFO')) || this.groupInfo;
		},
		resetSaveInfo() {
			this.save = {
				hospital: 1,
				doctor: 1,
			}
			uni.setStorageSync('SAVE_INFO', JSON.stringify(this.save))
		},
		saveInfo(data) {
			for (let i in data) {
				this.save[i] = data[i]
			}
			uni.setStorageSync('SAVE_INFO', JSON.stringify(this.save))
		},
		loadingStart() {
			this.loading++
		},
		loadingDone() {
			this.loading--
			if (this.loading < 0) this.loading = 0
		}
	},
});