//数据表格配置

import tool from "@/utils/tool"

export default {
	successCode: 200,
	currentPage: 1,													//表格开始页码
	pageSize: 10,													//表格每一页条数
	pageSizes: [10, 20, 30, 40, 50],								//表格可设置的一页条数
	paginationLayout: "total, sizes, prev, pager, next, jumper",	//表格分页布局，可设置"total, sizes, prev, pager, next, jumper"
	lazyProps: function (props) {									//表格懒加载配置选项
		let obj = {};
		for (const key in props) {
			obj[key] = props[key].key;
		}
		return Object.keys(obj).length && obj || { hasChildren: "hasChildren", children: "children" }
	},
	parseData: function (res) {
		return {
			data: res.content || res.records || (res.code == "fail" && [] || res.data) || res,				    //分析无分页的数据字段结构
			total: isNUllOrUndefined(res.totalElements) || isNUllOrUndefined(res.total) || "0",		    	//分析总数字段结构
			msg: res.message || "success",  																//分析描述字段结构
			code: res.code || 200		    																//分析状态字段结构
		}
	},
	lazyData: function (array, props) {								//表格懒加载数据分析
		return array.map(arr => {
			for (const key in props) {
				arr[props[key].key] = props[key].value(arr)
			}

			return arr
		})
	},
	request: {							//请求规定字段
		page: {
			key: "page",
			pageStart: function (page) {
				return page - 1
			}
		},
		current: {
			key: "current",
			pageStart: function (page) {
				return page
			}
		},
		//规定一页条数字段
		pageSize: "size"
	},
	/**
	 * 自定义列保存处理
	 * @tableName scTable组件的props->tableName
	 * @column 用户配置好的列
	 */
	columnSettingSave: function (tableName, column) {
		return new Promise((resolve) => {
			setTimeout(() => {
				//这里为了演示使用了session和setTimeout演示，开发时应用数据请求
				tool.session.set(tableName, column)
				resolve(true)
			}, 1000)
		})
	},
	/**
	 * 获取自定义列
	 * @tableName scTable组件的props->tableName
	 * @column 组件接受到的props->column
	 */
	columnSettingGet: function (tableName, column) {
		return new Promise((resolve) => {
			//这里为了演示使用了session和setTimeout演示，开发时应用数据请求
			const userColumn = tool.session.get(tableName)
			if (userColumn) {
				resolve(userColumn)
			} else {
				resolve(column)
			}
		})
	},
	/**
	 * 重置自定义列
	 * @tableName scTable组件的props->tableName
	 * @column 组件接受到的props->column
	 */
	columnSettingReset: function (tableName, column) {
		return new Promise((resolve) => {
			//这里为了演示使用了session和setTimeout演示，开发时应用数据请求
			setTimeout(() => {
				tool.session.remove(tableName)
				resolve(column)
			}, 1000)
		})
	}
}

function isNUllOrUndefined(column) {
	if (column === null || column == undefined) return undefined
	return column + ""
}
