import storage from '@/utils/storage';
import config from '@/config'

const state = {
	totalConfig: config, //全部配置
	areaConfig: {
		name: '文登',
		baseUrl: 'http://122.9.137.144:11098',
		// baseUrl: 'http://192.168.1.7:10200', //真机调试
		// baseUrl: 'http://localhost:10200',
		title: '威海文登市居家和社区管理系统',
		enableRememberPassword: true, //是否显示记住密码勾选框
		requireCheckinPhoto: false, //签到时，是否需要拍照
		addCheckinWatermark: false, //签到时，是否给照片加水印
		watermark_before_after_GZSG: false, //改造施工前后,照片加水印
		watermark_before_after_SMFW: false, //上门服务前后,照片加水印
	}, //当前选择的区域的配置
	selectedArea: uni.getStorage('selectedArea'), //当前选择的区域
}

const mutations = {
	SET_CONFIG(state, totalConfig) {
		state.totalConfig = totalConfig
	},
	SET_SELECTED_AREA(state, key) {
		state.selectedArea = key
	},
	SET_AREA_CONFIG(state, area) {
		state.areaConfig = area;
	},
}

const actions = {
	//设置全部配置到state-config
	setConfig({
		commit
	}, totalConfig) {
		commit('SET_CONFIG', totalConfig)
	},
	//设置当前区域到state-selectedArea，同时设置当前区域配置到state-areaConfig
	setSelectedArea({
		state,
		commit
	}, key) {
		commit('SET_SELECTED_AREA', state.totalConfig[key], key)
		commit('SET_AREA_CONFIG', state.totalConfig[key])
	},
	initAreaConfig({
		state,
		commit
	}, key) {
		commit('SET_AREA_CONFIG', state.totalConfig[key])
	},
	//切换区域
	selectArea({
		dispatch,
		state
	}, key, isSaveStorage = true) {
		if (state.totalConfig[key]) {
			dispatch('setSelectedArea', key)
			if (isSaveStorage) {
				storage.set('selectedArea', key) //保存到本地
			}
		} else {
			console.log('没有找到该区域配置');
		}
	}
}

export default {
	state,
	mutations,
	actions
}