/**
 * 分配人员明细列表_已提交_专员list.js
 */
shr.defineClass("shr.custom.DetailSubmitedDynamicList", shr.custom.CustomDynamicList, {
	isSum: false,
	initalizeDOM: function () {
		//新增编辑列
		this.editColumns.push("bdpapd.performancegradeq1");
		this.editColumns.push("bdpapd.performancegradeq2");
		this.editColumns.push("bdpapd.performancecoeffq1");
		this.editColumns.push("bdpapd.performancecoeffq2");
		this.editColumns.push("bdpapd.initialproportion");
		this.editColumns.push("bdpapd.adjustmentdesc");
		this.editColumns.push("bdpapd.opinionamount");

		//设置金额字段显示精度
		this.decimalPrecisionFields.push({ name: "bdpapd.opinionamount", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdpapd.indivbonusamount", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdpapd.approvalamount", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdp.bonusamount", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdp.totalbonushalf", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdp.totalbonusreal", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdp.directmanprojbonus", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdp.contramanprojbonus", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdp.condmanprojectbonus", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdp.stramanprojectbonus", precision: 6 });
		this.decimalPrecisionFields.push({ name: "bdp.bonusadjustment", precision: 6 });
		shr.custom.DetailSubmitedDynamicList.superClass.initalizeDOM.call(this);
		this.setF7Attribute();
	},
	/*
	* 设置F7组件属性
	*/
	setF7Attribute: function () {
		var self = this;
		$('#busDep').shrPromptBox("option", {
			//确定按钮设置值,之后执行方法
			afterOK: function (datas, e) {
				//查询表格数据
				self.queryGrid();
			},
			//open方法里调用
			verifyBeforeOpenCallback: function (e, value) {
				//var parentId = shr.getUrlParam('parentId');
				var parentId = self.getParentUrlParam('parentId')
				$("#busDep").shrPromptBox("setFilter", "id in ( SELECT CFBusDepID FROM CT_PB_BONUSCYCLEBUSDEPPROJECT where FParentID ='"
					+ parentId + "'  and CFState = '2')");
			},
		});
		//计算_事业部
		$('#calculateBusDep').shrPromptBox("option", {
			//确定按钮设置值,之后执行方法
			afterOK: function (datas, e) {
				//计算
				self.doCalculate();
			},
		});
		//提交_事业部
		$('#submitBusDep').shrPromptBox("option", {
			//确定按钮设置值,之后执行方法
			afterOK: function (datas, e) {
				//提交
				self.doSubmit();
			},
		});
		//撤回_事业部
		$('#withdrawBusDep').shrPromptBox("option", {
			//确定按钮设置值,之后执行方法
			afterOK: function (datas, e) {
				//撤回
				self.doWithdraw();
			},
		});
	},
	/**
	 * 返回
	 */
	backAction: function (callback) {
		var self = this;
		var callback = function () {
			var url = "/shr/dynamic.do?"
			var parentId = self.getParentUrlParam('parentId')
			var params = {
				uipk: "com.kingdee.eas.custom.projectbonus.app.BonusCycle.dynamicList",
				inFrame: true,
				parentId: parentId,
				permItemId: shr.getCurrentPagePermItemId()
			}
			url = url + $.param(params);
			window.parent.location.href = url;
			//shr.urlLocate(url, params);
		};
		shr.custom.DetailSubmitedDynamicList.superClass.backAction.call(this, callback);
	},
	// 根据动态列表配置 取业务主键
	getBillIdFieldName: function () {
		return 'bdpapd.id';
	},
	//导出选中
	getExportFieldName: function () {
		return 'bdpapd.id';
	},
	/**
	 * @Override
	 */
	queryGrid: function () {
		//设置按钮是否可见
		this.setButtonVisible();
		shr.custom.DetailSubmitedDynamicList.superClass.queryGrid.call(this);
	},
	/**
	 * 设置按钮是否可见
	 */
	setButtonVisible: function () {
		//var bonusCycleId = shr.getUrlParam('parentId');
		var self = this;
		var bonusCycleId = self.getParentUrlParam('parentId')
		shr.callHandler({
			handler: "com.kingdee.eas.custom.projectbonus.web.handler.BusDepProjectDynamicListHandler",
			action: 'getStateByBonusCycleId',
			param: {
				bonusCycleId: bonusCycleId
			},
			async: false,  //异步
			type: "POST",
			success: function (res) {
				if (1 == res.value) {
					$("#saveDynGrid").hide();
					$("#import").hide();
				} else {
					$("#saveDynGrid").show();
					$("#import").show();
				}
			}
		});
	},
	/**
	 * 	单元格点击事件触发
	 * @param {*} rowid 
	 * @param {*} colIndex 
	 * @param {*} cellcontent 
	 * @param {*} e 
	 * @returns 
	 */
	onCellSelect: function (rowid, colIndex, cellcontent, e) {
		shr.custom.DetailSubmitedDynamicList.superClass.onCellSelect.call(this);
		if (colIndex != 0) {
			//周期锁定状态
			var bonuscycleState = $("#grid").jqGrid("getCell", rowid, "bonuscycle.state");
			if (1 == bonuscycleState) {
				//锁定
				$("#grid").jqGrid("setGridParam", { cellEdit: false })
			} else {
				//非启用状态不能编辑
				$("#grid").jqGrid("setGridParam", { cellEdit: true })
			}
		}
	},
	/**
	 * 表格加载完成后操作，供子类复写
	 */
	afterLoadComplete: function (ret) {
		//当状态非启用 编辑列单元格背景色显示白色
		if (ret.records > 0) {
			for (let i = 0; i < ret.rows.length; i++) {
				var rowData = ret.rows[i];
				var fid = rowData["_row_index"];
				var tr = $('tr[id="' + fid + '"]')
				var tds = tr.find('.cellEditItem');
				//周期锁定状态
				var bonuscycleState = rowData["bonuscycle.state"];
				if (1 == bonuscycleState.value) {
					//锁定状态
					//启用状态,提交状态
					for (var j = 0; j < tds.length; j++) {
						$(tds[j]).removeClass('cellEditItem')
					}
				} else {
					for (var j = 0; j < tds.length; j++) {
						$(tds[j]).addClass('cellEditItem')
					}
				}
			}
		}
	},
	/**
	* 描述: 导入action
	*/
	importAction: function () {
		//自定义参数
		//var bonusCycleId = shr.getUrlParam('parentId');
		var self = this;
		var bonusCycleId = self.getParentUrlParam('parentId')
		var busDepIds = $("#busDep_el").val();
		var customParam = { "bonusCycleId": bonusCycleId, "busDepIds": busDepIds }
		shr.custom.DetailSubmitedDynamicList.superClass.importAction.call(this, customParam);
	},
	/**
	 * 获得自定义过滤条件，供子类覆写使用
	 */
	getCustomFilterItems: function () {
		//ar parentId = shr.getUrlParam('parentId');
		var self = this;
		var parentId = self.getParentUrlParam('parentId')
		var busDepIds = $("#busDep_el").val();
		var filter = "bonusCycle.id = '" + parentId + "' and bDPAPD.id is not null";
		if (busDepIds) {
			busDepIds = busDepIds.replace(/,/g, "','");
			return filter += " and busDep.id in ('" + busDepIds + "')";
		}
		return filter;
	},
	/**
	* 组装保存时传至服务端的数据
	*/
	assembleSaveData: function (action) {
		var _self = this;
		//组装需要修改的数据
		var data = [];
		var meta;
		var id, column, value, cur, element, personCode, datatype;
		var tds = $("#grid td[class*='dirty-cell']");
		var $table = $("#grid")[0];
		for (var i = 0; i < tds.length; i++) {
			//获取父节点，以获取对应id
			meta = {};
			var cur = $(tds[i]);
			//获取输入的内容，而不是转以后的字符实体，同时对单引号处理
			fid = cur.parent().attr('id');
			element = cur.attr('aria-describedby').replace('grid_', '');
			value = $($table).getCell(fid, element);
			//value = cur.text().replace(/\'/g, "''").trim();
			fid = cur.parent().attr('id');
			element = cur.attr('aria-describedby').replace('grid_', '');
			datatype = _self.getColumnType(element);
			if ("promptbox" == datatype) {
				column = element.split('.')[0];
				value = value["id"];
			} else {
				column = element.split('.')[1];
			}
			//列表需要显示id列
			id = $("#" + _self.getEscapeChars(fid) + " td[aria-describedby='grid_" + _self.getBillIdFieldName() + "']").attr('title');
			//校验
			_self.checkData(column, tds[i], value);
			//校验输入内容
			if (!_self.checkNumValid(value, datatype)) {
				$(tds[i]).focus();
				$(tds[i]).css({
					"border": "solid 2px red"
				});
				throw new Error("请输入正确的格式");
			}
			meta.fid = id;
			meta.column = column;
			meta.value = value;
			meta.type = datatype;
			data.push(meta);
		}
		return data;
	},
	/**
	 * 
	 */
	checkData: function (column, td, value) {
		var _self = this;
		if ("initialproportion" == column && (1 < value || 0 > value)) {
			$(td).focus();
			$(td).css({
				"border": "solid 2px red"
			});
			throw new Error("个人初始比例只能在[0,1]之间!");
		}
	},
	/**
	* 计算
	*/
	calculateAction: function (option) {
		var self = this;
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				//设置过滤
				var fliter = this.getBusdepFilter();
				$("#calculateBusDep").shrPromptBox("setFilter", "id in ( " + fliter + ")");
				//打开
				$("#calculateBusDep").shrPromptBox("open");
			}, function () {
				return;
			});
		} else {
			//设置过滤
			var fliter = this.getBusdepFilter();
			$("#calculateBusDep").shrPromptBox("setFilter", "id in ( " + fliter + ")");
			//打开
			$("#calculateBusDep").shrPromptBox("open");
		}
	},
	/**
	* 真正的计算方法
	 */
	doCalculate: function () {
		//事业部ids
		var busDepIds = $("#calculateBusDep_el").val();
		if (busDepIds) {
			this.doRemoteWithBatchAction({
				method: "calculate",
				billId: busDepIds
			});
		} else {
			shr.showError({ message: "请先选中事业部", hideAfter: 3 });
		}
	},
	/**
	 * 提交至负责人
	*/
	submitCalculateAction: function (option) {
		var self = this;
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				var fliter = this.getBusdepFilter();
				$("#submitBusDep").shrPromptBox("setFilter", "id in ( " + fliter + ")");
				//打开
				$("#submitBusDep").shrPromptBox("open");
			}, function () {
				return;
			});
		} else {
			var fliter = this.getBusdepFilter();
			$("#submitBusDep").shrPromptBox("setFilter", "id in ( " + fliter + ")");
			//打开
			$("#submitBusDep").shrPromptBox("open");
		}
	},
	/**
	 * 真正的提交方法
	 */
	doSubmit: function () {
		//事业部ids
		var busDepIds = $("#submitBusDep_el").val();
		if (busDepIds) {
			this.doRemoteWithBatchAction({
				method: "submitCalculate",
				billId: busDepIds
			});
		} else {
			shr.showError({ message: "请先选中事业部", hideAfter: 3 });
		}
	},
	/**
	 * 撤回
	*/
	withdrawAction: function (option) {
		var self = this;
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				var fliter = this.getBusdepFilter();
				$("#withdrawBusDep").shrPromptBox("setFilter", "id in ( " + fliter + ")");
				//打开
				$("#withdrawBusDep").shrPromptBox("open");
			}, function () {
				return;
			});
		} else {
			var fliter = this.getBusdepFilter();
			$("#withdrawBusDep").shrPromptBox("setFilter", "id in ( " + fliter + ")");
			//打开
			$("#withdrawBusDep").shrPromptBox("open");
		}
	},
	/**
	 * 真正的撤回方法
	 */
	doWithdraw: function () {
		var self = this;
		//事业部ids
		var busDepIds = $("#withdrawBusDep_el").val();
		if (busDepIds) {
			this.doRemoteWithBatchAction({
				method: "withdraw",
				billId: busDepIds
			});
		} else {
			shr.showError({ message: "请先选中事业部", hideAfter: 3 });
		}
	},
	/**
	 * 获取事业部过滤条件
	 */
	getBusdepFilter: function (type) {
		var self = this;
		var parentId = self.getParentUrlParam('parentId')
		var fliter = "";
		if (!parentId) {
			shr.showError({ message: "parentId不能为空!", hideAfter: 3 });
			return fliter;
		}
		//设置过滤
		var fliter = "SELECT FID FROM CT_PB_BONUSCYCLEBUSDEPPROJECT where FParentID ='" + parentId + "' and CFState = '2'";
		return fliter;
	},
	/**
	 * 批量提示行数据，方便子类覆写
	 */
	getBatchTipsRowDatas: function (ids, options) {
		var _self = this;
		var rowData;
		var option = {
			action: 'getBusDepProjectById',
			uipk: this.uipk,
			type: 'POST',
			async: false,
			param: {
				busDepIds: ids.toString()
			},
			success: function (res) {
				rowData = res;
			},
		};
		shr.remoteCall(option);
		return rowData;
	},
	/**
	 * 如果重写过getBatchTipsRowDatas方法，则需要增加对应的列配置属性
	 */
	getBatchTipsTableModel: function () {
		var tableModel = $("#grid").jqGrid("getGridConfig").colModel;
		for (let i = 0; i < tableModel.length; i++) {
			var name = tableModel[i].name;
			if ("busdep.name" == name || "secondcenter.name" == name || "bdp.type" == name || "project.name" == name) {
				tableModel[i].isBatchTipsColumn = true;
			}
		}
		return tableModel;
	},
});

