import $util from "@/utils/index.js"
import $api from "@/api/index.js"
export default {
	state: {
		merchantAuth: 0, // 技-师列表是否显示商家
		old_attendant_name: '技师',
		configInfo: {
			id: 0,
			isIos: uni.getSystemInfoSync().system.includes('iOS'),
			navBarHeight: uni.getSystemInfoSync().statusBarHeight * 1 + 44,
			curSysHeight: '',
			tabbarHeight: '',
			methodObj: {
				1: 'call',
				2: 'miniProgram',
				3: 'web',
				4: 'navigateTo'
			},
			tabBar: [],
		},
		play_method: [{
			method: 'onPlay',
			msg: '开始播放',
			status: true,
		}, {
			method: 'onStop',
			msg: '结束播放',
			status: false,
		}, {
			method: 'onError',
			msg: '报错Error',
			status: false,
		}, {
			method: 'onEnded',
			msg: '自然结束播放',
			status: false,
		}],
		audioBg: {},
		audioType: 'countdown_voice',
		playBg: false,
		isHaveAudio: false
	},
	mutations: {
		//修改信息
		updateConfigItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
			if (key !== 'configInfo') return
			uni.setStorageSync('configInfo', val)
			let src = val[state.audioType]
			if (state.isHaveAudio) {
				state.audioBg.src = src || ''
				return
			}
			if (!val.countdown_voice && !val.service_start_recording && !val.service_end_recording) return
			state.audioBg = uni.createInnerAudioContext();
			state.isHaveAudio = true
			state.audioBg.src = src
			// #ifndef APP-PLUS
			state.audioBg.obeyMuteSwitch = false
			// #endif
			state.play_method.map(item => {
				state.audioBg[item.method](() => {
					console.log('bg=>', item.msg)
					state.playBg = item.status;
				})
			})
		}
	},
	actions: {
		// 获取基本配置
		async getConfigInfo({
			commit,
			state
		}, param) {
			let config = await $api.base.configInfo()
			if (!config.primaryColor) {
				config.primaryColor = '#A40035'
			}
			if (!config.subColor) {
				config.subColor = '#F1C06B'
			}
			if (!config.user_image) {
				config.user_image = 'https://lbqny.migugu.com/admin/anmo/mine/bg.png'
			}
			if (!config.coach_image) {
				config.coach_image = 'https://lbqny.migugu.com/admin/anmo/mine/bg.png'
			}
			if (!config.service_btn_color) {
				config.service_btn_color = '#282B34'
			}
			if (!config.service_font_color) {
				config.service_font_color = '#EBDDB1'
			}
			if (!config.user_font_color) {
				config.user_font_color = '#ffffff'
			}
			if (!config.coach_font_color) {
				config.coach_font_color = '#ffffff'
			}

			let base_config = $util.pick(state.configInfo, ['isIos', 'navBarHeight', 'curSysHeight',
				'tabbarHeight', 'methodObj', 'tabBar'
			])

			let tabBar = [{
				id: 1,
				name: '首页',
				default_img: '../static/tab/shouye_nor.png',
				selected_img: '../static/tab/shouye_sel.png'
			}, {
				id: 2,
				name: '技师',
				default_img: '../static/tab/jishi_nor.png',
				selected_img: '../static/tab/jishi_sel.png'
			}, {
				id: 4,
				name: '订单',
				default_img: '../static/tab/dingdan_nor.png',
				selected_img: '../static/tab/dingdan_sel.png'
			}, {
				id: 5,
				name: '我的',
				default_img: '../static/tab/wode_nor.png',
				selected_img: '../static/tab/wode_sel.png'
			}]


			let tInd = tabBar.findIndex(item => {
				return item.id == 2
			})
			tabBar[tInd].name = config.attendant_name
			
			// let authArr = ['map', 'dynamic', 'store'] 
			// let authArr = ['dynamic', 'store']
			let authArr = []  // tabbar 权限
			let authObj = {
				dynamic: {
					id: 3,
					name: '动态',
					default_img: '../static/tab/find_nor.png',
					selected_img: '../static/tab/find_sel.png',
					ind: config.attendant_name
				},
				store: {
					id: 6,
					name: '门店',
					default_img: '../static/tab/store_nor.png',
					selected_img: '../static/tab/store_sel.png',
					ind: '首页'
				},
				map: {
					id: 7,
					name: '地图找人',
					default_img: '../static/tab/jishi_nor.png',
					selected_img: '../static/tab/jishi_sel.png',
					ind: config.attendant_name
				}
			}

			let plugNum = 0
			let {
				plugAuth
			} = config
			for (let i in plugAuth) {
				plugAuth[i] = plugAuth[i] == 1 || plugAuth[i] ? false : true
				if (authArr.includes(i)) {
					if (plugAuth[i]) {
						plugNum++
					}
					let addInd = tabBar.findIndex(item => {
						return item.name == authObj[i].name
					})
					let spliceInd = tabBar.findIndex(item => {
						return item.name == authObj[i].ind
					})
					if (plugAuth[i] && addInd == -1) {
						tabBar.splice(spliceInd + 1, 0, authObj[i])
					}
					if (!plugAuth[i] && addInd !== -1) {
						tabBar.splice(addInd, 1)
					}
				}
			}
			config.plugAuth = plugAuth
			

			//if (plugNum > 1) {
			//tabBar.splice(tabBar.findIndex(item => {
			//return item.name == '订单'
			//}), 1)
			//}

			base_config.tabBar = tabBar
			let data = Object.assign({}, config, base_config)
			commit('updateConfigItem', {
				key: 'configInfo',
				val: data
			})

		},
		toPlayAudio({
			commit,
			state
		}, param) {
			state.audioType = param.key
			let src = state.configInfo[param.key]
			if (!src) return
			if (state.isHaveAudio) {
				state.audioBg.src = src
			} else {
				state.audioBg = uni.createInnerAudioContext();
				state.isHaveAudio = true
				state.audioBg.src = src
				// #ifndef APP-PLUS
				state.audioBg.obeyMuteSwitch = false
				// #endif
				state.play_method.map(item => {
					state.audioBg[item.method](() => {
						console.log('bg=>', item.msg)
						state.playBg = item.status;
					})
				})
			}

			if (state.playBg) {
				state.audioBg.stop()
			}
			state.audioBg.play()
		}
	}
}