const db = uniCloud.database();
import {
	getJsonTree,
	groupBy
} from "@/js_sdk/util/jsonData";

//一次性把字典数据查询并存储
 export function saveDictItemStore() {
	db.collection('ngTools_DictItem').field(
			"dictID,itemValue as value , itemText as text ,itemColor,description,sortOrder"
		).orderBy('dictID,sortOrder')
		.get().then((res) => {
			console.log(res)

			if (res.result.data) {
				const selectData = groupBy(res.result.data, 'dictID');
				// #ifdef APP-PLUS
				uni.setStorageSync("dictItem", selectData);
				// #endif	
				// #ifdef H5
				localStorage.setItem("dictItem", JSON.stringify(selectData));
				// #endif	
				// #ifdef MP-WEIXIN
				wx.setStorageSync("dictItem", selectData);
				// #endif
				console.log(res.result.data)
				console.log(selectData)

			}
		}).catch((err) => {
			uni.showModal({
				content: err.message || '请求服务失败',
				showCancel: false
			})
			console.log(3333)
		}).finally(() => {
			console.log(4444)
			uni.hideLoading()
		})
}

//一次性把字典数据查询并存储


export function saveDepartStore() {
	db.collection('ngTools_depart').field(
			"_id,parent_id,depart_name,level,sort,manager_uid,status"
		).orderBy('parent_id')
		.get().then((res) => {
			if (res.result.data) {
				const departData = getJsonTree(res.result.data);
				console.log(departData)
				// #ifdef APP-PLUS
				uni.setStorageSync("departData", departData);
				// #endif	
				// #ifdef H5							
				localStorage.setItem("departData", JSON.stringify(departData));
				// #endif	
				// #ifdef MP-WEIXIN
				wx.setStorageSync("departData", departData);
				// #endif
				console.log(res.result.data)
				console.log(departData)

			}
		}).catch((err) => {
			uni.showModal({
				content: err.message || '请求服务失败',
				showCancel: false
			})
			console.log(3333)
		}).finally(() => {
			console.log(4444)
			uni.hideLoading()
		})
}

export function getDictItemStore(dictID) {
	let data = [];
	// #ifdef MP-WEIXIN
	data = (wx.getStorageSync("dictItem"));
	// #endif
	// #ifdef APP-PLUS
	data = uni.getStorageSync("dictItem");
	// #endif	
	// #ifdef H5
	data = JSON.parse(localStorage.getItem("dictItem"));
	// #endif	
	return data[dictID];
	// console.log(this.selectData);
	// console.log(data);
	// if (this.selectData.length > 0) {
	
	// }
}

export function getDepartStore() {
	let data = [];
	// #ifdef MP-WEIXIN
	data = (wx.getStorageSync("departData"));
	// #endif
	// #ifdef APP-PLUS
	data = uni.getStorageSync("departData");
	// #endif	
	// #ifdef H5
	data = JSON.parse(localStorage.getItem("departData"));
	// #endif	
	return data;

}

 
 