import selectQueryAction from '../control/selectQueryView.vue';
export default {
	execute: function (bill, param) {
		if (param) {
			GMS.$hideContainer.addComponent(selectQueryAction, {}, async function (c) {
				debugger
				const getExtrCondition = async (filterFieldObj) => {
					let extrConditions = []
					for (const key in filterFieldObj) {
						let extrConditionObj = { name: filterFieldObj[key] }
						let value = bill.getMasterData().getValue(key)
						extrConditionObj.values = await getExtrConditionObjValue(value),
						extrConditionObj.type = getExtrConditionObjType(value)
						console.log('type', await getExtrConditionObjType(value))
						extrConditions.push(extrConditionObj)
					}
					return extrConditions
				}
				const getExtrConditionObjType = (val) => {
					const type = typeof val
					const reg = new RegExp(/^[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}$/)
					if (Object.prototype.toString.call(val) === '[object Object]') {
						const value = val.code
						const type = typeof value
						return type.toUpperCase()
					} else if (Object.prototype.toString.call(val) === '[object Date]') {
						return 'DATE'
					} else if (type === 'number' || type === 'boolean') {
						return type.toUpperCase()
					} else if (reg.test(val)) {
						return 'UUID'
					} else {
						return type.toUpperCase()
					}
				}

				const getExtrConditionObjValue = async (val) => {
					if (Object.prototype.toString.call(val) === '[object Object]') {
						if (val._type) {
  						const tableName = val._type.split('.').length > 0 ? val._type.split('.')[1] : val._type
							const postData = {
								pagination: false,
								tableName: tableName,
								searchKey: "",
								queryDataStructure: "ALL",
								queryChildrenType: "ALL_CHILDREN_WITH_SELF",
								code: val.code,
								authType: "ACCESS",
								versionDate: "",
								expression: "",
							}
							const {data} = await GMS.$http.post(`/baseData/data/list`, postData)
							console.log('res.data', data)
							const node = data.rows[0]
							const value = node.objectcode? node.objectcode: val.code
							return value
						} else {
							return val.objectcode? val.objectcode: val.code
						}

					} else {
						return val
					}
				}

				c.bill = bill
				const actionParam = param.param
				c.field = actionParam.field
				c.baseDataFields = actionParam.field.baseDataFields
				c.templateName = actionParam.templateName
				c.tableName = actionParam.tableName
				const res = await getExtrCondition(actionParam.field.filterField)
				c.extrCondition = res
				c.isDrawer = true
				c.drawerShow = true
				console.log('extrCondition', c.extrCondition)
				let isSub = actionParam.tableName.split('.').length
				c.isSub = isSub === 1
			})
		} else {
			GAMS.Common.messagePrompt('缺少参数');
		}
	},

}