/**
 * 事业部项目表动态list.js
 */
shr.defineClass("shr.custom.BusDepProjectDynamicList", shr.custom.CustomDynamicList, {
	isSum: false,
	initalizeDOM: function () {
		//新增编辑列
		this.editColumns.push("principal.name");
		this.editColumns.push("bdp.notprincipalpropor");
		this.editColumns.push("bdp.bonusAmount");
		this.editColumns.push("bdp.principalpropor");
		this.editColumns.push("bdp.totalbonushalf");
		this.editColumns.push("bdp.totalbonushalf");
		this.editColumns.push("bdp.totalbonusreal");
		this.editColumns.push("bdp.directmanprojbonus");
		this.editColumns.push("bdp.contramanprojbonus");
		this.editColumns.push("bdp.condmanprojectbonus");
		this.editColumns.push("bdp.stramanprojectbonus");
		this.editColumns.push("bdp.bonusadjustment");
		this.editColumns.push("bdp.bonusadjustmentdesc");
		this.editColumns.push("bdp.initialallocationratio1");
		this.editColumns.push("bdp.initialallocationratio2");
		this.editColumns.push("bdp.initialallocationratio3");
		this.editColumns.push("proPrin.name");

		//设置金额字段显示精度
		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.BusDepProjectDynamicList.superClass.initalizeDOM.call(this);
	},
	// 根据动态列表配置 取业务主键
	getBillIdFieldName: function () {
		return 'bdp.id';
	},
	//导出选中
	getExportFieldName: function () {
		return 'bdp.id';
	},
	/**
	 * @Override
	 */
	queryGrid: function () {
		//设置按钮是否可见
		this.setButtonVisible();
		shr.custom.BusDepProjectDynamicList.superClass.queryGrid.call(this);
	},
	/**
	 * 设置按钮是否可见
	 */
	setButtonVisible: function () {
		var bonusCycleId = shr.getUrlParam('parentId');
		var options = {
			action: 'getStateByBonusCycleId',
			uipk: this.uipk,
			type: 'POST',
			async: false,
			param: {
				bonusCycleId: bonusCycleId
			},
			success: function (res) {
				if (1 == res.value) {
					$("#saveDynGrid").hide();
					$("#import").hide();
					$("#syncDepProject").hide();
					$("#noticePrincipal").hide();
					$("#batchEnable").parent().parent().hide();
					$("#opReminderContent").hide();
				} else {
					$("#saveDynGrid").show();
					$("#import").show();
					$("#syncDepProject").show();
					$("#noticePrincipal").show();
					$("#batchEnable").parent().parent().show();
					$("#opReminderContent").show();
				}
			}
		};
		shr.remoteCall(options);
	},
	/**
	 * 	单元格点击事件触发
	 * @param {*} rowid 
	 * @param {*} colIndex 
	 * @param {*} cellcontent 
	 * @param {*} e 
	 * @returns 
	 */
	onCellSelect: function (rowid, colIndex, cellcontent, e) {
		shr.custom.BusDepProjectDynamicList.superClass.onCellSelect.call(this);
		if (colIndex != 0) {
			var columnName = $("#grid").getColumnName(colIndex);
			//周期锁定状态
			var bonuscycleState = $("#grid").jqGrid("getCell", rowid, "bonuscycle.state");
			if (1 == bonuscycleState) {
				//锁定
				$("#grid").jqGrid("setGridParam", { cellEdit: false })
			} else {
				//事业部状态
				var state = $("#grid").jqGrid("getCell", rowid, "bdp.state");
				if ((0 != state && 3 != state) && "principal.name" == columnName) {
					//启用 和提交状态，负责人和奖金分包金额不能编辑
					$("#grid").jqGrid("setGridParam", { cellEdit: false })
				} else if ((4 == state || 5 == state) && "bdp.bonusamount" == columnName) {
					//奖金分包金额
					$("#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 {
					//事业部状态
					var state = rowData["bdp.state"];
					if (0 != state.value && 3 != state.value) {
						//启用状态,提交状态
						for (var j = 0; j < tds.length; j++) {
							var describedby = $(tds[j]).attr("aria-describedby")
							if ("grid_principal.name" == describedby) {
								//负责人 || 奖金分包金额
								$(tds[j]).removeClass('cellEditItem')
							} else if ((4 == state.value || 5 == state.value) && "grid_bdp.bonusamount" == describedby) {
								//奖金分包金额
								$(tds[j]).removeClass('cellEditItem')
							} else {
								$(tds[j]).addClass('cellEditItem')
							}
						}
					}
				}
			}
		}
	},
	/**
	* 描述: 维护奖金分配人员明细
	*/
	optionPersonnelDetailAction: function () {
		var self = this;
		//自定义参数
		var parentId = shr.getUrlParam('parentId');
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				self.reloadPage({
					uipk: "BonusCycleBusDepProject.wizard",
					//uipk: "com.kingdee.eas.custom.projectbonus.app.PersonnelDetail.dynamicList",
					parentId: parentId
				});
			}, function () {
				return;
			});
		} else {
			self.reloadPage({
				uipk: "BonusCycleBusDepProject.wizard",
				parentId: parentId,
				permItemId: shr.getCurrentPagePermItemId()
			});
		}
	},
	/**
	 * 描述: 导入action
	 */
	importAction: function () {
		var self = this;
		//自定义参数
		var parentId = shr.getUrlParam('parentId');
		var customParam = { "parentId": parentId }
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				self.doImportData('import', customParam);
			}, function () {
				return;
			});
		} else {
			self.doImportData('import', customParam);
		}
	},
	/**
	 * 同步事业部项目
	 */
	syncDepProjectAction: function () {
		var self = this;
		var parentId = shr.getUrlParam('parentId');
		var options = {
			action: 'syncDepProject',
			uipk: this.uipk,
			type: 'POST',
			param: {
				parentId: parentId
			},
			success: function (res) {
				//重新查询数据
				self.queryGrid();
				shr.showInfo({
					message: res,
					hideAfter: 3
				});
			},
		};
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				shr.remoteCall(options);
			}, function () {
				return;
			});
		} else {
			shr.remoteCall(options);
		}
	},
	/**
	 * 获得自定义过滤条件，供子类覆写使用
	 */
	getCustomFilterItems: function () {
		var parentId = shr.getUrlParam('parentId');
		return "bonusCycle.id = '" + parentId + "' and busdep.id is not null"
	},
	/**
	* 组装保存时传至服务端的数据
	*/
	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');
			// //校验输入内容

			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;
	},
	/**
	 * 批量撤回
	 */
	batchWithdrawAction: function () {
		this.defaultBatchActionHandle("batchWithdraw");
	},
	/**
	 * 解锁
	 */
	unlockAction: function () {
		var self = this;
		var parentId = shr.getUrlParam('parentId');
		var options = {
			method: "unlock",
			async: false,
			param: { parentId: parentId },
			success: function (res) {
				shr.showSuccess({
					message: "解锁成功!",
					hideAfter: 3
				});
				self.queryGrid();
			}
		};
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				shr.remoteCall(options);
			}, function () {
				return;
			});
		} else {
			shr.remoteCall(options);
		}
	},
	/**
	 * 锁定
	 */
	lockAction: function () {
		var self = this;
		var parentId = shr.getUrlParam('parentId');
		var options = {
			method: "lock",
			async: false,
			param: { parentId: parentId },
			success: function (res) {
				shr.showSuccess({
					message: "锁定成功!",
					hideAfter: 3
				});
				self.queryGrid();
			}
		};
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				shr.remoteCall(options);
			}, function () {
				return;
			});
		} else {
			shr.remoteCall(options);
		}
	},
	/**
	 * 催办
	 */
	noticePrincipalAction: function () {
		var self = this;
		var parentId = shr.getUrlParam('parentId');
		var options = {
			method: "noticePrincipal",
			async: false,
			param: { parentId: parentId },
			success: function (res) {
				shr.showSuccess({
					message: res,
					hideAfter: 3
				});
				//_self.queryGrid();
			}
		};
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				shr.remoteCall(options);
			}, function () {
				return;
			});
		} else {
			shr.remoteCall(options);
		}
	},
	/**
	 * 维护提醒内容
	 */
	opReminderContentAction: function () {
		var self = this;
		var parentId = shr.getUrlParam('parentId');
		var option = {
			uipk: "com.kingdee.eas.custom.projectbonus.app.BonusCycle_opReminderContent.form",
			billId: parentId,
			method: "edit"
		};
		self.beforeSave();
		if (self.checkEditCell()) {
			shr.showConfirm("您有编辑数据未保存，是否忽略？", function () {
				self.reloadPage(option);
			}, function () {
				return;
			});
		} else {
			self.reloadPage(option);
		}
	},
});

