import store from '@/store'
import CONSTANT from '@/constant'
import UTIL from '@/utils/util'

import { getList as getLevelList } from '@/api/level'
import { categoryListB as getCategoryList, usageListB as getUsageList } from '@/api/goods'
import { getFestList } from '@/api/common'
import { orderRelate as getOrderRelate } from '@/api/order'

/**
 * 获取等级列表
 * @parmas update为true时会重新触发请求，重置vuex的数据
 */
export async function getLevel(update) {
	let data = store.getters.getLevel
	if (!UTIL.isEmpty(data) && !update) {
		return data
	}
	await getLevelList().then(res => {
		data = res.data
		store.dispatch('setLevel', data)
	}).catch(() => {
		return false
	})

	return data
}

/**
 * 获取分类列表
 * @parmas update为true时会重新触发请求，重置vuex的数据
 */
export async function getCategory(update) {
	let data = store.getters.getCategory
	if (!UTIL.isEmpty(data) && !update) {
		return data
	}
	await getCategoryList().then(res => {
		data = res.data
		store.dispatch('setCategory', data)
	}).catch(() => {
		return false
	})

	return data
}

/**
 * 获取分类列表
 * @parmas update为true时会重新触发请求，重置vuex的数据
 */
export async function getUsage(update) {
	let data = store.getters.getUsage
	if (!UTIL.isEmpty(data) && !update) {
		return data
	}
	await getUsageList().then(res => {
		data = res.data
		store.dispatch('setUsage', data)
	}).catch(() => {
		return false
	})

	return data
}

/**
 * 获取节日列表
 * @parmas update为true时会重新触发请求，重置vuex的数据
 */
export async function getFest(update) {
	let data = store.getters.getFest
	if (!UTIL.isEmpty(data) && !update) {
		return data
	}
	await getFestList().then(res => {
		data = res.data
		store.dispatch('setFest', data)
	}).catch(() => {
		return false
	})

	return data
}

/**
 * 获取商城下单-相关信息 m
 * @parmas update为true时会重新触发请求，重置vuex的数据
 */
export async function getOrderShop(update) {
	let data = store.getters.getOrderShop
	if (!UTIL.isEmpty(data) && !update) {
		return data
	}
	await getOrderRelate().then(res => {
		data = res.data
		store.dispatch('setOrderShop', data)
	}).catch(() => {
		return false
	})

	return data
}
