// 定义字段映射
const FieldKeysMap = {
	"STK_TransferDirect": "FID,FSrcStockLocId.FF102029.FName,FSrcStockId.FName,FDestStockId.FName,FMaterialId.FNumber,FBOXNO_LT,FQty,FENCHASENUM_LT,FAuxPropId.FF101501.FDataValue,FLot.FNumber,FNoteEntry,FBillNo,FStockerId.FName,FDate,FDestStockId.FNumber"
};

export function getSearchParam(condition) {
	let FilterString = [{
			"FieldName": "FDocumentStatus",
			"Compare": "105",
			"Value": "A",
			"Left": "(",
			"Right": "",
			"Logic": 1
		},
		{
			"FieldName": "FDocumentStatus",
			"Compare": "105",
			"Value": "D",
			"Left": "",
			"Right": ")",
			"Logic": 0
		},
		{
			"FieldName": "FSrcStockId.FName",
			"Compare": "17",
			"Value": "面料",
			"Left": "",
			"Right": "",
			"Logic": 0
		},
		{
			"FieldName": "FBillNo",
			"Compare": "17",
			"Value": condition.billNo,
			"Left": "",
			"Right": "",
			"Logic": 0
		},
		{
			"FieldName": "FTransferDirect",
			"Compare": "29",
			"Value": "GENERAL",
			"Left": "",
			"Right": "",
			"Logic": 0
		},
		{
			"FieldName": "FScanType.FDataValue",
			"Compare": "67",
			"Value": "调拨入库扫描",
			"Left": "(",
			"Right": "",
			"Logic": 1
		},
		{
			"FieldName": "FScanType.FDataValue",
			"Compare": "67",
			"Value": "调拨出库扫描",
			"Left": "",
			"Right": ")",
			"Logic": 0
		},
		{
			"FieldName": "FDate",
			"Compare": "39",
			"Value": condition.range[0],
			"Left": "(",
			"Right": "",
			"Logic": "0"
		},
		{
			"FieldName": "FDate",
			"Compare": "16",
			"Value": condition.range[1],
			"Left": "",
			"Right": ")",
			"Logic": "0"
		}
	]
	let FieldKeys =
		"FID,FSrcStockLocId.FF102029.FName,FSrcStockId.FName,FDestStockId.FName,FMaterialId.FNumber,FBOXNO_LT,FQty,FENCHASENUM_LT,FAuxPropId.FF101501.FDataValue,FLot.FNumber,FNoteEntry,FBillNo,FStockerId.FName,FDate,FDestStockId.FNumber"
	console.log(condition.FBillType)
	
	

	console.log(FieldKeys)

	return {
		"parameters": [{
			"FormId": condition.FormId,
			"FieldKeys": FieldKeys,
			"FilterString": FilterString
		}]
	}
}
export function getBillDetailListParam(condition) {
	console.log(condition)
	
	return {
		"parameters": [{
			"FormId": condition.FormId,
			"FieldKeys": FieldKeysMap[condition.FormId],
			"FilterString": [{
				"Left": "",
				"FieldName": "FID",
				"Compare": "338",
				"Value": condition.selectedFID,
				"Right": "",
				"Logic": 0
			}],
		}]
	}

}
// 定义一个函数来创建库存查询请求数据
export function createInventoryQueryRequest(value) {
	return {
		parameters: [{
			FormId: "STK_TransferDirect",
			FieldKeys: "fID",
			FilterString: [{
					Left: "",
					FieldName: "FSrcStockLocId.FF102029.FName",
					Compare: "67",
					Value: value,
					Right: "",
					Logic: 0
				},
				// {
				// 	Left: "",
				// 	FieldName: "FBaseQty",
				// 	Compare: "21",
				// 	Value: "0",
				// 	Right: "",
				// 	Logic: 0
				// }
			]
		}]
	};
}

function getActualQtyKey(FormId) {
	const keyMap = {
		'LT_PRD_PMCWorkBench': 'FActualQty',
		'SP_PickMtrl': 'FActualQty',
		'STK_MisDelivery': 'FQty',
		'PUR_MRB': 'FRMREALQTY'
	};
	return keyMap[FormId] || 'FQty';
}

function createEntityObject(item, actualQtyKey) {
	return {
		"FEntryID": item["FEntity.FEntryID"],
		"FInventoryQty": item["FInventoryQty"],
		"FAuxPropId": {
			"FAUXPROPID__FF101501": {
				"FNumber": item["FAuxPropId.FF101501.FDATAVALUE"]
			}
		},
		"FStockLocId": {
			"FSTOCKLOCID__FF102029": {
				"Fname": item["FStockLocId.FF102029.fname"]
			}
		},
		"FLot": {
			"FNumber": item["Flot.FNumber"]
		},
		"FENCHASENUM_LT": item["FENCHASENUM.LT"],
		[actualQtyKey]: item[actualQtyKey] // 使用动态键名
	};
}

export function getSaveParam(FormId, data, indexList) {
	const targetFID = data;
	const actualQtyKey = getActualQtyKey(FormId);

	const FEntityObjects = indexList
		.filter(item => item.FID === targetFID)
		.map(item => createEntityObject(item, actualQtyKey));

	const needUpdateFields = [
		"FInventoryQty",
		"FAuxPropId",
		"FStockLocId",
		"FIsScanFabric",
		"FLot",
		"FENCHASENUM_LT",
		actualQtyKey // 直接添加到数组中
	];

	return {
		"parameters": [
			FormId,
			{
				"NeedUpDateFields": needUpdateFields,
				"NeedReturnFields": ["FID"],
				"IsDeleteEntry": "true",
				"SubSystemId": "",
				"IsVerifyBaseDataField": "false",
				"IsEntryBatchFill": "true",
				"ValidateFlag": "true",
				"NumberSearch": "true",
				"IsAutoAdjustField": "false",
				"InterationFlags": "",
				"IgnoreInterationFlag": "",
				"IsControlPrecision": "false",
				"ValidateRepeatJson": "false",
				"Model": {
					"FID": targetFID,
					"FIsScanFabric": true,
					"FEntity": FEntityObjects
				}
			}
		]
	};
}
export function getSubmitParam(FormId, data) {
	const params = {
		"parameters": [
			FormId,
			{
				"CreateOrgId": 0,
				"Numbers": [],
				"Ids": data.toString(),
				"SelectedPostId": 0,
				"UseOrgId": 0,
				"NetworkCtrl": "",
				"IgnoreInterationFlag": ""
			}
		]
	};
	console.log(params);
	return params;
}
export function getAuditParam(FormId, data) {
	const params = {
		"parameters": [
			FormId,
			{
				"CreateOrgId": 0,
				"Numbers": [],
				"Ids": data.toString(),
				"InterationFlags": "",
				"UseOrgId": 0,
				"NetworkCtrl": "",
				"IsVerifyProcInst": "true",
				"IgnoreInterationFlag": "",
				"UseBatControlTimes": "false"
			}
		]
	}
	console.log(params)
	return params
}