//因为页面表格较多，每次操作一张表格时需要切换gridManager
var gridManager = null;

//定义生成表格的参数对象
var bomObj={
	parms:{},
	node:"",
	url:"",
	columns:[
		{ display: "物料", name: "materialName", width: 140, minWidth: 60, type: "string", align: "left" },
		{ display: "物料数量", name: "materialNumber", width: 120, minWidth: 60, type: "string", align: "left" },
		{ display: "产品", name: "productId", width: 120, minWidth: 60, type: "string", align: "left" }
	]
}
var technologyObj={
	parms:{},
	node:"technologyGrid",
	url:"slicedTechnologyList",
	gridManager:null,
	columns:[
		{ display: "工艺路线名称", name: "technologyName", width: 750, minWidth: 60, type: "string", align: "left" }
	]
}
var processObj={
	parms:{},
	node:"processGrid",
	url:"slicedTechnologyProcessList",
	gridManager:null,
	columns:[
		{ display: "工序内容", name: "processContent", width: 150, minWidth: 60, type: "string", align: "left", hidden: true  },
		{ display: "工序名称", name: "processName", width: 150, minWidth: 60, type: "string", align: "left" },
		{ display: "工序说明", name: "processRemark", width: 150, minWidth: 60, type: "string", align: "left" },
		{ display: "工序类别", name: "processTypeTextView", width: 150, minWidth: 60, type: "string", align: "left" },
		{ display: "工时", name: "workHours", width: 150, minWidth: 60, type: "string", align: "left" }
	]
}

var processBomObj={
	parms:{},
	node:"",
	url:"",
	columns:[
		{ display: "物料", name: "materialId", width: 140, minWidth: 60, type: "string", align: "left", hidden: true  },
		{ display: "SAP物料详细信息", name: "materialName", width: 140, minWidth: 60, type: "string", align: "left" },
		{ display: "使用的物料数量", name: "materialNumber", width: 120, minWidth: 60, type: "string", align: "left" },
		{ display: "物料计量单位", name: "productName", width: 120, minWidth: 60, type: "string", align: "left" }
	]
}

//创建表格（参数、列、插入的节点、请求地址）
function loadProofingApplyGrid(obj) {
	obj.gridManager =  UICtrl.grid("#"+obj.node, {
		columns: obj.columns,
		dataAction: "server",
		url: web_app.name +'/proofingApply/'+obj.url+'.ajax',
		parms:obj.parms,
		pageSize: 10,
		usePager: true,
		width: "100%",
		height: 400,
		heightDiff: -8,
		fixedCellHeight: true,
		selectRowButtonOnly: true
	});
	UICtrl.setSearchAreaToggle(obj.gridManager);
}


/**
 * 切换gridManager
 * 定义监听器、操作对应表格是自动切换
 */
function switchGridManager() {
	$("#technology").on("click", "*", function(event) {
		if(gridManager==technologyObj.gridManager)return;
		gridManager=technologyObj.gridManager;
	});
	$("#process").on("click", "*", function(event) {
		if(gridManager==processObj.gridManager)return;
		this.gridManager=processObj.gridManager;
	})
}


//展示工艺表格
function showTechnologyTable(productId){

	if(technologyObj.gridManager!=null){
		gridManager=technologyObj.gridManager;//手动切换gridManager
		reloadGrid({productId:productId});

		 //产品编号每次发生变动后删除残留的工序数据
		if(processObj.gridManager!=null){
			gridManager=processObj.gridManager;//手动切换gridManager
			reloadGrid({ technologyId:-1 });
		}
	}else {
		technologyObj.parms={productId:productId};
		loadProofingApplyGrid(technologyObj);
	}
}


$(document).ready(function() {
	switchGridManager();
	technologyClickRequestProcess();
});


/**
 * 刷新表格
 * @param parms 刷新时请求的参数
 */
function reloadGrid(parms) {
	if(parms!=null){
		gridManager.options.parms=parms
	}
	gridManager.loadData();
}

/**
 * 根据选定的technology列获取对应的process
 * 根据冒泡机制，永远都会先执行此点击事件，需要让平台内置事件先执行来获取行id
 */
function technologyClickRequestProcess(){
	$(document).on('click', '#technology .l-grid-row', function(event) {
		// 延迟执行新创建的点击事件逻辑，确保在当前点击事件逻辑之后执行
		setTimeout(function() {
			let result = DataUtil.getUpdateRow(gridManager); // 获取选中行
			processObj.parms = { technologyId: result.technologyId };
			if (processObj.gridManager != null) {
				gridManager = processObj.gridManager; // 手动切换 gridManager
				reloadGrid({ technologyId: result.technologyId });
			} else {
				loadProofingApplyGrid(processObj);
			}
		}, 0);
	});
}


