import http from '@/request/index.js';
export const state = {
	systemData: [], //系统数据
	roleData: [], //角色数据
	questionData: [], // 问题类型
	processData: [], //处理人员数据
	feedBackList: [], //已反馈数据查询
	queryListStas: {

	}, //查询某个数据的处理情况
	queryData: {} //查询数据详情
};
export const mutations = {
	setSystemData(state, data) {
		state.systemData = data
	},
	setRoleData(state, data) {
		state.roleData = data
	},
	setQuestionData(state, data) {
		state.questionData = data
	},
	setProcessData(state, data) {
		state.processData = data
	},
	setFeedBackList(state, data) {
		state.feedBackList = data
	},
	setQueryListStas(state, data) {
		state.queryListStas = data
	},
	setQueryData(state, data) {
		state.queryData = data
	}

}
export const actions = {
	getSystemData({
		commit
	}) {
		getSelectata('sys', commit, 'setSystemData')
	},
	getRoleData({
		commit
	}) {
		getSelectata('role', commit, 'setRoleData')
	},
	getQuestionData({
		commit
	}) {
		getSelectata('prbType', commit, 'setQuestionData')
	},
	getQueryData({
		commit,
		rootState
	}) {
		return new Promise((resolve, reject) => {
			let storeOpenID = uni.getStorageSync('openID');
			let openID = rootState.userInfo.openID || storeOpenID;
			http('query.details', {
				rcdId: rootState.queryListStas.id,
				wechatId: openID
			}, '加载中').then(res => {
				if (res.code === 0) {
					commit('setQueryData', res.data);
					uni.navigateTo({
						url: '/pages/details/details',
						success: function(res) {}
					});
				}
			}).catch(e => {
				reject(e)
			})
		})
	},
	getProcessData({
		commit
	}) {
		return new Promise((resolve, reject) => {
			http('feedback.processData', {}).then(res => {
				if (res.code === 0) {
					let data = createData(res.data);
					commit('setProcessData', data);
				}
			}).catch(e => {
				reject(e)
			})
		})
	},
	getFeedBackList({
		commit,
		rootState
	}, {
		current,
		size,
		that
	}) {
		return new Promise((resolve, reject) => {
			let storeOpenID = uni.getStorageSync('openID');
			let openID = rootState.userInfo.openID || storeOpenID;
			http('feedback.getFedbackList', {
				wechatId: openID,
				current,
				size
			}, '加载中').then(res => {
				if (res.code === 0) {
					let data = res.data.records || [];
					that.$refs.paging.complete(data);
					// commit('setFeedBackList', data);
				}
			}).catch(e => {
				reject(e)
				that.$refs.paging.complete(false);
			})
		})
	},
};

function getSelectata(type, commit, setData) {
	return new Promise((resolve, reject) => {
		http('feedback.system', {
			listType: type,
		}, '加载中').then(res => {
			if (res.code === 0) {
				let data = createData(res.data);
				commit(setData, data);
			}
		}).catch(e => {
			reject(e)
		})
	})
}

function createData(data) {
	let newData = [];
	data.length > 0 && data.forEach((item) => {
		newData.push({
			text: item.listVal || item.userName,
			value: item.listVal || item.userId,
			userid: item.userId || "",
		})
	})
	return newData;
}