import $api from "@/api/index.js"
import $util from "@/utils/index.js"
import $store from "@/store/index.js"
export default {
	state: {
		pageActive: false,
		activeIndex: 0,
		tabList: [{
			title: '全部',
			sort: 'top desc',
		}, {
			title: '价格',
			sort: 'price',
			sign: 0,
			is_sign: 1,
		}, {
			title: '销量',
			sort: 'total_sale',
			sign: 0,
			is_sign: 1,
		}, {
			title: '好评度',
			sort: 'star',
			sign: 0,
			is_sign: 1,
		}],
		param: {
			page: 1,
			sort: ''
		},
		list: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		banner: [],
		service_cate: [],
		service_all_cate: [],
		recommend_list: [],
		recommend_style: 1
	},
	mutations: {
		async updateServiceItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		async getServiceIndex({
			commit,
			state
		}, param) {
			let d = await $api.service.index(param);
			let {
				banner = [],
					service_cate = [],
					recommend_list = [],
					recommend_style = 1,
					coach_apply_show = 0
			} = d
			let all_cate = $util.deepCopy(service_cate)
			commit('updateServiceItem', {
				key: 'service_all_cate',
				val: all_cate
			})
			let cateLen = all_cate.length
			if (coach_apply_show) {
				service_cate.splice(cateLen > 4 ? 4 : cateLen - 1, 0, {
					id: 0,
					title: $store.state.config.configInfo.attendant_name + '入驻',
					cover: 'https://lbqny.migugu.com/admin/anmo/technician/default_technician.png',
					url: '/technician/pages/apply'
				})
			}
			let num = ((coach_apply_show && cateLen > 28) || (!coach_apply_show && cateLen > 29)) ? 29 :
				coach_apply_show ? cateLen + 1 : cateLen
			console.log(coach_apply_show, num, "======num")
			service_cate = service_cate.slice(0, num)
			if ((cateLen > (coach_apply_show ? 28 : 29))) {
				service_cate.push({
					id: 0,
					title: '全部分类',
					cover: '',
					icon: 'iconfenleiyingyongtongzhi2',
					url: '/user/pages/service/cate'
				})
			}
			commit('updateServiceItem', {
				key: 'banner',
				val: banner
			})
			commit('updateServiceItem', {
				key: 'service_cate',
				val: service_cate
			})
			commit('updateServiceItem', {
				key: 'recommend_list',
				val: recommend_list
			})
			commit('updateServiceItem', {
				key: 'recommend_style',
				val: recommend_style
			})
		},
		async getServiceList({
			commit,
			state
		}, param) {
			let d = await $api.service.serviceList(param)
			let oldList = state.list;
			let newList = d;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList;
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		}
	},
}