<!doctype html>
<html>
	<head>
		<meta charset="utf-8"><meta http-equiv="Expires" content="0"><meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Cache-control" content="no-cache"><meta http-equiv="Cache" content="no-cache">
		<title>协议对比</title>
		
		<script type="text/javascript">
			var pageVue = null;
			function loadJsCss(callback) {
				var jscss_urls = [];
				window.top.initJsCss(document, jscss_urls, callback);
			};
			
			function initVue() {
				new ListVue({
					el: "#vbody",
					data: {
						form_dataname: "agm_agreement",
						table_dataname: "agm_agreement_detail",
						
						title: "协议对比",
						notdisabled: true,
						approval:false,
						
						dataRequest: [
							{
								 isClientMode: false,
								 name: "agm_template",
								 //url: "root/front/getOneDictionary",
								 //paramsobj: { code: "OrgType", },
								 filter: " type='agreement'",
								 isnotoption: false, //true:不是选项
								 code:"id",//是下拉选项时设置
								 label:"name",//是下拉选项时设置
							},
							{
								 isClientMode: false,
								 name: "agm_category",
								 //url: "root/front/getOneDictionary",
								 //paramsobj: { code: "OrgType", },
								 isnotoption: false, //true:不是选项
								 code:"id",//是下拉选项时设置
								 label:"name",//是下拉选项时设置
							},
						],
						dataRequestObj: {},
						tree_agm_category: [],
						//tabaleFieldsName: "policyReportSummary_",
						
						newRowDefault: {
							//id: uuid_short(),
							code: createCode("XY"),
							versionno: "1",
						},
						
						tabAll: {
							"产品销售服务协议": {label: "产品销售服务协议", name: "dj"},
							"佣金": {label: "佣金", name: "yj"},
							"自营备案": {label: "自营", name: "zy"}
						},
						
						activeName: "",
						
						formFieldsAll1: [
							{isshow: "T", field: "creator_name", name: "创建人名称", type: "span", notvalunit: true},
							{isshow: "T", field: "create_time", name: "创建日期", type: "span", notvalunit: true},
							
							{isshow: "T", field: "code", name: "协议号", type: "span", notvalunit: true},
							{isshow: "T", field: "versionno", name: "协议版本号", type: "span", notvalunit: true},
							//{isshow: "T", field: "category_id", name: "协议类型-ID", notvalunit: true},
							{isshow: "T", field: "category_name", name: "协议类型", type: "span", notvalunit: true},
							//{isshow: "T", field: "template_id", name: "模板-ID", notvalunit: true},
							{isshow: "T", field: "template_name", name: "协议模板", type: "span", notvalunit: true},
							//{isshow: "T", field: "state_code", name: "协议状态-编码", notvalunit: true},
							{isshow: "T", field: "state_name", name: "协议状态", type: "span", notvalunit: true},
							//{isshow: "T", field: "platform_id", name: "平台ID", notvalunit: true},
							{isshow: "T", field: "platform_name", name: "平台名称", notvalunit: true},
							//{isshow: "T", field: "department_id", name: "部门ID", notvalunit: true},
							
							//{isshow: "T", field: "applicant_id", name: "申请人ID", notvalunit: true},
							//{isshow: "T", field: "applicant_name", name: "申请人姓名", notvalunit: true},
							{isshow: "T", field: "date_from", name: "有效期-从", type: "date", notvalunit: true},
							{isshow: "T", field: "date_to", name: "有效期-到", type: "date", notvalunit: true},
							{isshow: "T", field: "department_name", name: "部门名称", notvalunit: true},
							{isshow: "T", field: "remark", name: "备注", notvalunit: true},
							
							//{isshow: "T", field: "creator_id", name: "创建人id", notvalunit: true},
							
							//{isshow: "T", field: "update_time", name: "更新日期", notvalunit: true},
							
						],
						
						formFieldsAll2: [
							{isshow: "T", field: "province", name: "省份", type: "select", options: dataRoot.database.province, props:{value: "field2", label: "label", checkStrictly: true}, notvalunit: true},
							//{isshow: "T", field: "customer_id", name: "客户ID", notvalunit: true},
							{isshow: "T", field: "customer_name", name: "客户名称", type: "span", colspan: 2, notvalunit: true},
							{isshow: "T", field: "contact", name: "联系人", notvalunit: true},
							{isshow: "T", field: "legal_representative", name: "法人代表", notvalunit: true},
							{isshow: "T", field: "phone", name: "电话", notvalunit: true},
							{isshow: "T", field: "fax", name: "传真", notvalunit: true},
							{isshow: "T", field: "bank_name", name: "开户银行", notvalunit: true},
							{isshow: "T", field: "bank_account", name: "开户行账号", notvalunit: true},
							{isshow: "T", field: "zip_code", name: "邮编", notvalunit: true},
							{isshow: "T", field: "address", name: "地址", colspan: 2, notvalunit: true},
							{isshow: "T", field: "days_billing", name: "账期(天)", notvalunit: true},
							{isshow: "T", field: "days_shipment", name: "运输期(天)", notvalunit: true},
							{isshow: "T", field: "amt_credit_limit", name: "信用额度", notvalunit: true},
							{isshow: "T", field: "pay_method", name: "付款方式", notvalunit: true},
						],
						
						formAttr1: {
							istitle: true,
							title: "基本信息",
							columnnumber: 3,
							labelwidth: "120px",
							labelposition: "right",//"left", //"right",
							size: "medium",
							border: "0px solid #c6c6c6"
						},
						formFields1: [
							// {field: "deliveryname", name: "供应商", isshow: "T", required: true, type: "popup", colspan: 2},
							// //{field: "recordstatus", name: "协议状态", isshow: "T", disabled:true, formatter:"orderState"},
							// {field: "applicant", name: "申请人", isshow: "T", disabled:true, required: true},
							// {field: "begindate", name: "开始时间", isshow: "T", type: "date", required: true},
							// {field: "closedate", name: "结束时间", isshow: "T", type: "date", required: true},
							// {field: "recordcode", name: "协议编号", isshow: "T", disabled: true},
							// //{field: "field16", name: "备注说明", isshow: "T", type: "textarea"},
							
							// {isshow: "T", field: "category_name", name: "协议类型", width: "150"},
							
							
							// {isshow: "T", field: "code", name: "协议编码", type: "span", disabled: true, notvalunit: true},
							// {isshow: "T", field: "applicant_name", name: "申请人", type: "span", disabled: true, notvalunit: true},
							
							// {isshow: "T", field: "template_id", name: "协议模板", type: "select", disabled: true, isrefresh: true, options: [], props:{value: "id", label: "name"}, notvalunit: true},
							// //{isshow: "T", field: "platform_id", name: "平台ID", width: "100"},
							// {isshow: "T", field: "platform_name", name: "平台名称", type: "popup", colspan: 2, notvalunit: true},
							// //{isshow: "T", field: "department_id", name: "部门ID", width: "100"},
							// {isshow: "T", field: "department_name", name: "部门名称", notvalunit: true},
							// //{isshow: "T", field: "applicant_id", name: "申请人ID", width: "100"},
							// {isshow: "T", field: "date_from", name: "有效期-从", type: "date", notvalunit: true, colspan: 1},
							// {isshow: "T", field: "date_to", name: "有效期-到", type: "date", notvalunit: true, colspan: 1},
							// {isshow: "T", field: "id", name: "", type: "null", colspan: 1},
							// {isshow: "T", field: "remark", name: "备注", type: "textarea", colspan: 3, notvalunit: true},
						],
						
						formAttr2: {
							istitle: true,
							title: "乙方信息",
							columnnumber: 3,
							labelwidth: "120px",
							labelposition: "right",//"left", //"right",
							size: "medium",
							border: "0px solid #c6c6c6"
						},
						formFields2: [
							// {field: "customercode", name: "商业编号", isshow: "T", disabled:true, required: true, type:"span"},
							// {field: "province", name: "商业省份", isshow: "T", disabled: true},
							// {field: "customername", name: "商业名称", isshow: "T", required: true, type: "popup", colspan: 2},
							// {field: "customeraddress", name: "商业地址", isshow: "T", disabled:true, colspan: 2},
							
							// {isshow: "T", field: "province", name: "省份", type: "select", options: dataRoot.database.province, props:{value: "field2", label: "label", checkStrictly: true}, notvalunit: true},
							// //{isshow: "T", field: "customer_id", name: "客户ID", width: "100"},
							// {isshow: "T", field: "customer_name", name: "客户名称", type: "popup", notvalunit: true},
							// {isshow: "T", field: "contact", name: "联系人", notvalunit: true},
							// {isshow: "T", field: "legal_representative", name: "法人代表", notvalunit: true},
							// {isshow: "T", field: "phone", name: "电话", notvalunit: true},
							// {isshow: "T", field: "fax", name: "传真", notvalunit: true},
							// {isshow: "T", field: "bank_name", name: "开户银行", notvalunit: true},
							// {isshow: "T", field: "bank_account", name: "开户行账号", notvalunit: true},
							// {isshow: "T", field: "zip_code", name: "邮编", notvalunit: true},
							// {isshow: "T", field: "address", name: "地址", align: "left", notvalunit: true},
							// {isshow: "T", field: "days_billing", name: "账期(天)", align: "right", notvalunit: true},
							// {isshow: "T", field: "days_shipment", name: "运输期(天)", align: "right", notvalunit: true},
							// {isshow: "T", field: "amt_credit_limit", name: "信用额度", align: "right", notvalunit: true},
							// {isshow: "T", field: "pay_method", name: "付款方式", type: "select", isrefresh: true, options: [{label: "账期",value: "账期"}, {label: "电汇",value: "电汇"}, {label: "现金",value: "现金"}], notvalunit: true},
							
						],
						
						formAttr_t: {
							istitle: false,
							title: "协议模板选择",
							columnnumber: 1,
							labelwidth: "70px",
							labelposition: "left",
							size: "medium",
							//border: "20px solid #c6c6c600",
							borderleft: "0px solid #c6c6c600",
						},
						formFields_t: [
							{isshow: "T", field: "template_id", name: "协议模板", type: "select", isrefresh: true, options: [], props:{value: "id", label: "name"}, notvalunit: true},
							{isshow: "T", field: "customer_name", name: "客户名称", type: "popup", notvalunit: true},
						],
						formData_t: {},
						agreement_fields: [],//协议模板头字段
						agm_agreement_obj: {},//协议模板头字段obj
						agreement_detail_fields: [],//协议模板明细字段
						agm_agreement_detail_obj: {},//协议模板明细字段obj
						
						record_template_ids: [],//备案模板id
						record_template_obj: {},//备案模板id及对应的字段
						tablefields_list: [], //协议各明细列表对应的字段
						tablefields_obj: {}, //协议各明细对象对应的字段
						
						template_tabsname: "A",
						
						select_record_list: [],//生成协议的备案
						
						urlImg2: "./img/wushuju.png",
						isEditTableData:true,
						isWithinEditTableData:false,
						formData1: {},
						formData1Old: {id: "111"},
						
						isRefresh: true,
						tableDataObj: {},
						tableDataOldObj: {},
						tableData2: [],
						tableHeight2: 280,
						editTableButton: {
							edit: {
								isshow: false,
								txt:"编辑"
							},
							del: {
								isshow: true,
								txt:"删除"
							},
							save: {
								isshow: false,
								txt:"保存"
							},
						},
						
						isCommit: false,
						reasonvisible: false,
						passOrRefuse: false,
						textarea2: "",
						
						popupParames: {},
						oldObj: {},
						tableFieldsAll: [
							{isshow: "T", field: "code", name: "备案号", width: "150"},
							{isshow: "T", field: "versionno", name: "备案版本号", width: "151"},
							{isshow: "T", field: "area", name: "区域省份", width: "152"},
							//{isshow: "T", field: "customer_id", name: "客户-id", width: "153"},
							{isshow: "T", field: "customer_code", name: "客户-编码", width: "154"},
							{isshow: "T", field: "customer_name", name: "客户-名称", width: "155"},
							//{isshow: "T", field: "terminal_id", name: "终端id", width: "156"},
							{isshow: "T", field: "terminal_name", name: "终端名称", width: "157"},
							//{isshow: "T", field: "product_id", name: "产品-id", width: "158"},
							{isshow: "T", field: "product_code", name: "产品-编码", width: "159"},
							{isshow: "T", field: "product_name", name: "产品-名称", width: "160"},
							{isshow: "T", field: "spec", name: "è§„æ ¼", width: "161"},
							{isshow: "T", field: "pack", name: "包装", width: "162"},
							{isshow: "T", field: "type_code", name: "类型-编码", width: "163"},
							{isshow: "T", field: "type_name", name: "类型-名称", width: "164"},
							{isshow: "T", field: "state_code", name: "状态-编码", width: "165"},
							{isshow: "T", field: "state_name", name: "状态-名称", width: "166"},
							{isshow: "T", field: "price_raw", name: "原始价格/政策价格", width: "167"},
							{isshow: "T", field: "rate", name: "扣率", width: "168"},
							{isshow: "T", field: "price", name: "价格/补偿单价", width: "169"},
							{isshow: "T", field: "price_bidding", name: "中标价", width: "170"},
							{isshow: "T", field: "price_invoice", name: "开票价", width: "171"},
							{isshow: "T", field: "price_rebate", name: "折扣价", width: "172"},
							{isshow: "T", field: "date_from", name: "生效时间", width: "173"},
							{isshow: "T", field: "date_to", name: "失效时间", width: "174"},
							{isshow: "T", field: "remark", name: "备注", width: "175"},
							//{isshow: "T", field: "creator_id", name: "创建人-id", width: "176"},
							//{isshow: "T", field: "creator_name", name: "创建人-姓名", width: "177"},
							//{isshow: "T", field: "create_time", name: "创建时间", width: "178"},
							//{isshow: "T", field: "update_time", name: "æ›´æ–°æ—¶é—´", width: "179"}
						],
						
						tableFieldsOther: [
							{isshow: "T", field: "h_old_type", name: "", width: "130"},
						],
						
						tableFields: [
							//{isshow: "T", field: "terminal_id", name: "终端id", width: "160"},
							//{isshow: "T", field: "terminal_name", name: "终端", type: "popup", width: "160"},
							//{isshow: "T", field: "product_id", name: "产品id", width: "160"},
							{isshow: "T", field: "product_name", name: "产品", width: "160"},
							{isshow: "T", field: "spec", name: "è§„æ ¼", width: "120"},
							{isshow: "T", field: "pack", name: "包装", width: "160"},
							{isshow: "T", field: "price_bidding", name: "中标价", type: "input", width: "100", align: "right"},
							{isshow: "T", field: "price_invoice", name: "开票价(出厂价)", type: "input", width: "120", align: "right"},
							{isshow: "T", field: "amt_target_year", name: "年度指标金额", type: "input", width: "140", align: "right"},
							{isshow: "T", field: "qty_target_year", name: "年度指标数量", type: "input", width: "140", align: "right"},
						],
						
						dialog_template: false,
						value_template: [],
						props_category: {value: "id", label: "name"},
						options_template: [],
						fullscreenLoading: false,
						rowkey: "id",
						defaultExpandAll: true,
						
					},
					created() {
						this.popupParames = clone(Root.popupParames);
						if (this.popupParames.data) {
							this.oldObj = clone(this.popupParames.data);
							this.formData1 = clone(this.popupParames.data);
						}
						
						//this.formFields1 = dataRootFields.formFields.floorAgreement_;
						//this.formFields2 = dataRootFields.formFields.customerAgreement_;
						//this.tableFields = dataRootFields.tableFields.policyReportSummary_;
						if (this.popupParames.sceneCode) {
							if (this.popupParames.sceneCode == "browse") {
								this.formAttr1.disabled = true;
								this.formAttr2.disabled = true;
								this.isWithinEditTableData = false;
								this.isEditTableData = false
								this.notdisabled = false;
								this.approval = false;
							}
							else if (this.popupParames.sceneCode == "approval") {
								this.formAttr1.disabled = true;
								this.formAttr2.disabled = true;
								this.isWithinEditTableData = false;
								this.isEditTableData = false
								this.notdisabled = false;
								this.approval = true;
							}
							else if (this.popupParames.sceneCode == "add") {
								if (this.newRowDefault) {
									let formData_ = clone(this.oldObj);
									
									for (var k in this.newRowDefault) {
										formData_[k] = this.newRowDefault[k];
									}
									this.formData1 = formData_;
								}
								this.isEditTableData = true;
								this.isWithinEditTableData = true;
							}
						}
						
						//Tab设置
						// this.tabs = [];
						// this.activeName = "";
						// if (this.formData1.category_name) {
						// 	//如果已存在协议类型,则只显示并选中此类型
						// 	var agreement_ = this.tabAll[this.formData1.category_name];
						// 	this.tabs.push(agreement_);
						// 	this.activeName = agreement_.name;
						// }
						// else {
						// 	for (var recordtype_ in this.tabAll) {
						// 		var agreement_ = this.tabAll[recordtype_];
						// 		this.tabs.push(agreement_);
						// 		if (!this.activeName) {
						// 			this.activeName = agreement_.name;
						// 		}
						// 	}
							
						// 	this.formData1.category_name = "产品销售服务协议"
						// }
						
					},
					
					mounted() {
						var me = this;
						//预加载数据
						if (this.dataRequest.length) {
							var result = {};
							this.loadRequestData(this.dataRequest, result, function(data) {
								me.dataRequestObj = data;
								//预加载数据后给哪些字段设置options或formatterjson
								// me.tree_agm_category = clone(data.agm_category);
								// let agm_category_ = ArrayToTree(clone(data.agm_category), "name", "parent_id");
								// me.options_template = clone(data.agm_template);
								if (me.formFields1.length) {
									var formFields_part_ = clone(me.formFields1);
									formFields_part_.map(e=>{
										if(e.field == "template_id") {
											e.options = me.dataRequestObj.agm_template;
										}
									});
									
									me.formFields1 = formFields_part_;
								}
								if (me.formFields_t.length) {
									var formFields_part_ = clone(me.formFields_t);
									formFields_part_.map(e=>{
										if(e.field == "template_id") {
											e.options = me.dataRequestObj.agm_template;
										}
									});
									
									me.formFields_t = formFields_part_;
								}
								
								
								if (me.popupParames.sceneCode != "add") {
									me.initData();
								}
								else if (me.popupParames.sceneCode == "add") {
									me.initField();
								}
							});
						}
						else {
							if (this.popupParames.sceneCode != "add") {
								this.initData();
							}
							else if (me.popupParames.sceneCode == "add") {
								this.initField();
							}
						}
						
						
						this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
							hideLoading();
						});
					},
					methods:{
						onServerInitData(data) {
							var me = this.data;
							//me.tableFields = data.tableFields;
							if(me.tableFields && me.tableFields.length > 0) {
								for(var i=0; i < me.tableFields.length; i++) {
									let fieldObj_ = me.tableFields[i];
									
									me.tableFieldsObj[fieldObj_.field] = fieldObj_;
								}
							}
						},
						
						initField() {
							//选择协议模板
							this.dialog_template = true
							
							//获取适用备案模板,用于获取对应的备案列表
							
							//选择商业
							
							//根据协议模板和商业获取对应的备案列表
							
						},
						
						templateSave() {
							//根据不同的协议模板显示不同的字段
							this.getFieldDataByTemplate();
							
							this.dialog_template = false;
						},
						
						//根据协议模板获取协议字段
						getFieldDataByTemplate() {
							var me = this;
							let filter_table = "";
							
							if (this.formData_t.template_id) {
								filter_table = " parent_id = '" + this.formData_t.template_id + "'";
							}
							
							let param_table = {
								isClientMode: false,
								dataname: "agm_template_field_mapping",
								filter: filter_table,
							}
							
							Server.call("root/data/getEntitySet", param_table, function(result) {
								var agm_record_ = [];
								var agm_record_obj_ = {};
								
								var agm_agreement_ = [];
								var agm_agreement_obj_ = {};
								
								var agm_agreement_detail_ = [];
								var agm_agreement_detail_obj_ = {};
								
								result.data.entityset.map(entity=>{
									if (entity.table_name == "agm_record") {
										agm_record_.push(entity)
										agm_record_obj_[entity.field] = entity;
									}
									else if (entity.table_name == "agm_agreement") {
										agm_agreement_.push(entity)
										agm_agreement_obj_[entity.field] = entity;
									}
									else if (entity.table_name == "agm_agreement_detail") {
										agm_agreement_detail_.push(entity)
										agm_agreement_detail_obj_[entity.field] = entity;
									}
								})
								
								// me.formFields_default.map(entity=>{
								// 	if (!agm_record_obj_[entity.field]) {
								// 		agm_record_obj_[entity.field] = entity;
								// 	}
								// })
								
								// me.record_fields = clone(agm_record_);
								// me.agm_record_obj = clone(agm_record_obj_);
								me.agreement_fields = clone(agm_agreement_);
								me.agm_agreement_obj = clone(agm_agreement_obj_);
								me.agreement_detail_fields = clone(agm_agreement_detail_);
								me.agm_agreement_detail_obj = clone(agm_agreement_detail_);
								
								me.setFormFields();
								
								if (me.popupParames.sceneCode == "add") {
									//根据协议模板中的适用备案和商业弹出备案列表
									me.getRecordTemplateFields();
								}
								else {
									//根据协议模板id获取协议模板
									var filter_t = "";
									if (me.formData1.template_id) {
										filter_t = " id='" + me.formData1.template_id + "'";
									}
									
									let param_t = {
										isClientMode: false,
										dataname: "agm_template",
										filter: filter_t,
									}
									
									Server.call("root/data/getEntitySet", param_t, function(result_t) {
										me.formData_t.selecttemplate = clone(result_t.data.entityset[0]);
										me.getRecordTemplateFields();
									})
								}
							});
						},
						
						setFormFields() {
							var me = this;
							var formFields1_ = [];
							var formFields2_ = [];
							this.formFieldsAll1.map(f=>{
								if (this.agm_agreement_obj[f.field]) {
									formFields1_.push(f);
								}
							})
							this.formFieldsAll2.map(f=>{
								if (this.agm_agreement_obj[f.field]) {
									formFields2_.push(f);
								}
							})
							
							this.formFields1 = clone(formFields1_);
							this.formFields2 = clone(formFields2_);
						},
						
						getRecordTemplateFields() {
							var me = this;
							//根据协议模板中的适用备案生成多个协议明细
							if (this.formData_t.selecttemplate.record_to) {
								var record_to_ = this.formData_t.selecttemplate.record_to.replace(";", "','")
								var filter_ = " parent_id in ('" + record_to_ + "')";
								
								let param_table = {
									isClientMode: false,
									dataname: "agm_template_field_mapping",
									filter: filter_,
								}
								
								Server.call("root/data/getEntitySet", param_table, function(result) {
									var record_template_ids_ = [];
									var record_template_obj_ = {};
									
									result.data.entityset.map(entity=>{
										if (entity.table_name == "agm_record") {
											if (!record_template_obj_[entity.parent_id]) {
												record_template_ids_.push(entity.parent_id);
												record_template_obj_[entity.parent_id] = {};
											}
											
											record_template_obj_[entity.parent_id][entity.field] = entity;
										}
									})
									
									me.record_template_ids = clone(record_template_ids_);
									me.record_template_obj = clone(record_template_obj_);
									
									me.setTableFields();
									
									if (me.popupParames.sceneCode == "add") {
										//根据协议模板中的适用备案和商业弹出备案列表
										me.getRecordALLByType();
									}
									else {
										//根据协议id获取协议明细
										me.getAgreementDetailById();
									}
									
								})
							}
						},
						
						setTableFields() {
							var me = this;
							var tableDataObj_ = {};
							var tableDataOldObj_ = {};
							var tablefields_list_ = [];
							var tablefields_obj_ = {};
							this.record_template_ids.map(rtid=>{
								var name_ = "";
								var tablefields_ = [];
								me.tableFieldsAll.map(f=>{
									if (me.record_template_obj[rtid][f.field] && name_ == "") {
										name_ = me.record_template_obj[rtid][f.field].category_name;
									}
									if (me.agm_agreement_detail_obj[f.field] || me.record_template_obj[rtid][f.field]) {
										tablefields_.push(f);
									}
								})
								
								if (me.tableFieldsOther.length) {
									me.tableFieldsOther.map(f=>{
										tablefields_.unshift(f);
									})
								}
								
								var fields_obj_ = {
									recordtemplateid: rtid,
									name: name_,
									fields: clone(tablefields_)
								};
								
								tablefields_obj_[rtid] = clone(fields_obj_);
								tablefields_list_.push(clone(fields_obj_));
								tableDataObj_[rtid] = [];
								tableDataOldObj_[rtid] = {};
								if (me.template_tabsname == "A") {
									me.template_tabsname = rtid;
								}
							})
							
							this.tableDataObj = clone(tableDataObj_);
							this.tableDataOldObj = clone(tableDataOldObj_);
							
							this.tablefields_list = clone(tablefields_list_);
							this.tablefields_obj = clone(tablefields_obj_);
							
						},
						
						getRecordALLByType() {
							//根据协议模板中的适用备案和商业弹出备案列表
							let me = this;
							var filter_ = " state_code='Open' ";
							if (this.formData_t.selecttemplate.record_to) {
								var record_to_ = this.formData_t.selecttemplate.record_to.replace(";", "','")
								filter_ += " and type_code in ('" + record_to_ + "')";
								
								if (this.formData_t.customer_id) {
									filter_ += " and customer_id='" + this.formData_t.customer_id + "'"
								}
							}
							else {
								Root.message({
									type: 'warning',
									message: '请选择协议模板'
								});
								return
							}
							
							Root.showPopup({
								url: "../agreement/apply/popup/record_list.html",
								width: 800,
								height: 550,
								filter: filter_,
								callback: function(array, callback) {
									var select_record_list_ = [];
									var tableDataObj_ = {};
									if (array.length > 0) {
										for (var i = 0; i < array.length; i++) {
											var row_1 = array[i];
											let row_ = clone(row_1);
											//row_.id = uuid_short();
											row_.record_id = clone(row_1.id);
											row_.id = null;
											//row_.parent_id = formData_.id;
											select_record_list_.push(row_);
											
											if (!tableDataObj_[row_.type_code]) {
												tableDataObj_[row_.type_code] = []
											}
											tableDataObj_[row_.type_code].push(row_);
										}
									}
									
									me.tableDataObj = clone(tableDataObj_);
									me.select_record_list = clone(select_record_list_);
									if (callback) {
										callback();
									}
								}
							});
						},
						
						getAgreementDetailById() {
							//根据协议id获取协议明细数据
							let me = this;
							var filter_table = "";
							if (this.formData1.id) {
								filter_table = "parent_id='" + this.formData1.id + "'";
							}
							
							let param_table = {
								isClientMode: false,
								dataname: "agm_agreement_detail",
								filter: filter_table
							}
							
							//me.tableDataObj = {};
							Server.call("root/data/getEntitySet", param_table, function(result) {
								var tableDataObj_ = {};
								var tableDataOldObj_ = {};
								if (result.data.entityset) {
									result.data.entityset.map(entity=>{
										var row_1 = entity;
										let row_ = clone(row_1);
										
										let rowold_ = {};
										if (!tableDataObj_[row_.type_code]) {
											tableDataObj_[row_.type_code] = [];
											var old_id = "old" + row_.id;
											row_.h_old_type = "原版本";
											
											rowold_ = {id: old_id, h_old_type: "对比版本", area: "黑龙江省"};
											row_.children = [rowold_];
											tableDataOldObj_[row_.type_code] = {}
										}
										tableDataObj_[row_.type_code].push(row_);
										tableDataOldObj_[row_.type_code][row_.id] = rowold_;
									})
									
									me.tableDataObj = clone(tableDataObj_);
									me.tableDataOldObj = clone(tableDataOldObj_);
								}
							})
						},
						
						initData() {
							//获取备案模板By协议模板
							this.formData_t.template_id = this.formData1.template_id;
							this.getFieldDataByTemplate();
							
							// let me = this;
							// let param_table = {
							// 	isClientMode: false,
							// 	dataname: "getRecordBymappingParent",
							// 	filter: "agreement_record_mapping.parent_id='" + this.oldObj.id + "'",
							// 	returntype: "EntitySet"
							// }
							
							// Server.call("root/data/procedure", param_table, function(result) {
							// 	if (result && result.data) {
							// 		var entityset_ = result.data.entityset;
									
							// 		var tableData2_ = [];
							// 		entityset_.map(d=>{
							// 			d.isWithinEdit = false;
							// 			tableData2_.push(d)
							// 		});
							// 		me.tableData2 = tableData2_;
							// 	}
							// });
						},
						
						closeDialog() {//关闭
							var me = this;
							if (me.popupParames.totab){
								Root.tab.removeItem(Root.tab.selected);
								Root.tab.open(me.popupParames.parentOption, false); 
							}
							else {
								Root.hidePopup();
							}
						},
						saveAfter() {//调回调的关闭
							var me = this;
							if(this.popupParames.callback) {
								let obj = {
									//row: this.formData
								}
								this.popupParames.callback(obj, function() {
									me.closeDialog();
								});
							}
							else {
								me.closeDialog();
							}
						},
						
						handleClick(tab) {
							/* if (tab.name == "dj") {
								this.formAttr1.title="产品销售服务协议基本信息";
								this.formData1.recordtype = "产品销售服务协议";
							}
							else if (tab.name == "yj") {
								this.formAttr1.title="佣金基本信息";
								this.formData1.recordtype = "佣金";
							}
							else if (tab.name == "zy") {
								this.formAttr1.title="自营基本信息";
								this.formData1.recordtype = "自营备案";
							} */
						},
						
						visibleChange(obj, rowdata, callback) {
							let me = this;
							let row = obj;
							let field = obj.field;
							let options_ = [];
							
							if(field == "template_id") {
								var filter_ = "";
								if (this.value_template.length > 0) {
									filter_ = "category_id='" + this.value_template[this.value_template.length - 1] + "'"
								}
								else if (this.formData1.category_id) {
									filter_ = "category_id='" + this.formData1.category_id + "'"
								}
								else {
									this.dialog_template = true;
								}
								
								let val = row[field];
								let param_ = {
									isClientMode: false,
									dataname: "agm_template",
									filter: filter_,
								}
								Server.call("root/data/getEntitySet", param_, function(result){
									options_ = result.data.entityset;
									options_.map(o=>{
										o.label = o.name;
									})
									console.log(options_);
									//obj.options = options_;
									callback({options: clone(options_)});
									
								});
							}
						},
						
						visibleChange_t(obj, rowdata, callback) {
							
						},
						
						//表单1
						selectChange(obj) {
							let me = this;
							if (obj.fieldobj.field == "template_id" && obj.selectoption) {
								//obj.data.template_id = clone(obj.selectoption.id);
								obj.data.template_name = clone(obj.selectoption.name);
								obj.data.category_id = clone(obj.selectoption.category_id);
								obj.data.category_name = clone(obj.selectoption.category_name);
							}
						},
						
						selectChange_t(obj) {
							let me = this;
							if (obj.fieldobj.field == "template_id" && obj.selectoption) {
								// obj.data.template_id = clone(obj.selectoption.id);
								// obj.data.template_name = clone(obj.selectoption.name);
								// obj.data.category_id = clone(obj.selectoption.category_id);
								// obj.data.category_name = clone(obj.selectoption.category_name);
								
								this.formData1.template_id = clone(obj.selectoption.id);
								this.formData1.template_name = clone(obj.selectoption.name);
								this.formData1.category_id = clone(obj.selectoption.category_id);
								this.formData1.category_name = clone(obj.selectoption.category_name);
								this.formData_t.selecttemplate = clone(obj.selectoption);
							}
						},
						
						//表单2
						showPopup(obj) {
							let me = this;
							let filter_ = " 1=1 ";
							
							var fieldObj_ = obj.obj;
							if (fieldObj_.field == "platform_name") {
								let me = this;
								Root.showPopup({
									url: "../org/company/popup/managesubject_list.html",
									width: 800,
									height: 550,
									dataname:"getDelivery_part_name",
									type :"managesubjectname",
									callback: function(obj, callback) {
										let formData_ = clone(me.formData1);
										formData_.platform_name = obj.row.delivery_part_name;
										formData_.platform_id = obj.row.delivery_part_code;
										me.formData1 = formData_;
										if (callback) {
											callback();
										}
									}
								});
							}
							else if (fieldObj_.field == "customer_name") {
								Root.showPopup({
									url: "../md/customer/popup/customer_list.html",
									width: 800,
									height: 550,
									filter: filter_,
									dataname: "getCustomer_name",
									
									callback: function(obj, callback) {
										let formData_ = clone(me.formData1);
										formData_.customer_name = obj.row.md_description;
										formData_.customer_code = obj.row.md_code;
										formData_.customer_id = obj.row.id;
										//formData_.customeraddress = obj.row.address;
										//formData_.province = obj.row.province;
										me.formData1 = formData_;
										if (callback) {
											callback();
										}
									}
								});
							}
						},
						
						showPopup_t(obj) {
							let me = this;
							let filter_ = " 1=1 ";
							var fieldObj_ = obj.obj;
							if (fieldObj_.field == "customer_name") {
								Root.showPopup({
									url: "../md/customer/popup/customer_list.html",
									width: 800,
									height: 550,
									filter: filter_,
									dataname: "getCustomer_name",
									
									callback: function(obj, callback) {
										let formData_ = clone(me.formData_t);
										formData_.customer_name = obj.row.md_description;
										formData_.customer_code = obj.row.md_code;
										formData_.customer_id = obj.row.id;
										//formData_.customeraddress = obj.row.address;
										//formData_.province = obj.row.province;
										me.formData_t = formData_;
										
										me.formData1.customer_name = clone(obj.row.md_description);
										me.formData1.customer_code = clone(obj.row.md_code);
										me.formData1.customer_id = clone(obj.row.id);
										if (callback) {
											callback();
										}
									}
								});
							}
						},
						
						showOrder(obj) {
							//获取当前tab,
							//获取当前tab的已有数组
							//获取备案列表By状态state_code=Open/商业id/非已存在备案id/备案模板id
							//将筛选后选中的备案数据添加到当前tab中的列表中
							// this.template_tabsname;//当前的tab即备案模板id
							// this.tableDataObj[this.template_tabsname];//已存在备案数组
							// this.formData1.customer_id//商业id
							
							let me = this;
							let formData_ = clone(me.formData1);
							
							let orderdetailid_ = "";
							let orderdetailidList = "";
							let orderparentid_ = "";
							let filter_order = "customer_id = '"+ formData_.customer_id + "' and state_code='Open' and type_code='" + this.template_tabsname + "'";
							/* sales_order_detail.cnt - ifnull(delivery_order_detail.cnt,0) > 0 */
							if (this.tableDataObj[this.template_tabsname].length > 0) {
								for (var i = 0; i < this.tableDataObj[this.template_tabsname].length; i++) {
									orderdetailid_ = this.tableDataObj[this.template_tabsname][i].record_id;
									//orderparentid_ = me.tableData2[i].parentid;
									if (orderdetailidList == "") {
										orderdetailidList = orderdetailid_;
									} 
									else {
										orderdetailidList += "','" + orderdetailid_;
									}
									
								}
								filter_order += " and id not in ('" + orderdetailidList + "')" 
							}
							
							Root.showPopup({
								url: "../agreement/apply/popup/record_list.html",
								width: 950,
								height: 650,
								type: "delivery",
								filter: filter_order,
								data: clone(me.formData1),
								delta: clone(me.oldObj),
								callback: function(array, callback) {
									if (array.length > 0) {
										for (var i = 0; i<array.length; i++) {
											var row_1 = array[i];
											let row_ = clone(row_1);
											//row_.id = uuid_short();
											row_.record_id = clone(row_1.id);
											//row_.parent_id = formData_.id;
											row_.id = null;
											me.rowChange(row_, "add");
										}
									}
									if (callback) {
										callback();
									}
								},
								
							});
						},
						
						delData(scope) {
							let me = this;
							let row = scope.row;
							let index_ = scope.$index;
							
							Root.confirm('确定删除-' + row.code + '-吗?', '删除提示', {
							  confirmButtonText: '删除',
							  cancelButtonText: '取消',
							  type: 'warning'
							}).then(() => {
								me.rowChange(row, "del");
							}).catch(() => {
								
								Root.message({
									type: 'info',
									message: '已取消删除'
								});          
							});
						},
						
						rowChange(row, type) {
							//this.tableDataObj[this.template_tabsname];
							
							var me = this;
							if (type == "add") {
								this.tableDataObj[this.template_tabsname].push(row);
								//this.computeForm();
							}
							else if (type == "del") {
								if (row.id) {
									let param = {
										isClientMode: false,
										dataname: "agm_agreement.agm_agreement_detail",
										id: me.formData.id, 
										detailid: row.id
									}
									
									Server.call("dmsroot/data/deleteEntity", param, function(result) {
										console.log(result);
										if (result && result.data) {
											me.tableDataObj[me.template_tabsname].remove(row);
											Root.message({
												type: 'success',
												message: '删除成功!'
											});
											//me.computeForm();
										}
									});
									
								}
								else {
									this.tableDataObj[this.template_tabsname].remove(row);
									//this.computeForm();
								}
							}
							else {
								//this.computeForm();
							}
						},
						
						submitRowTable() {
							this.isCommit = true;
							this.saveRowTable();
						},
						
						saveRowTable() {
							var me = this;
							this.fullscreenLoading = true;
							if (this.formData1.id) {
								//先删除再添加
								let param = {
									isClientMode: false,
									dataname: this.form_dataname,
									id: this.formData1.id
								}
								
								Server.call("dmsroot/data/deleteEntity", param, function(result) {
									me.doSaveRowTable();
								}, function() {
									me.fullscreenLoading = false;
								})
							}
							else {
								this.doSaveRowTable()
							}
						},
						
						doSaveRowTable() {
							var me = this;
							var operator_ = "save";//保存
							
							if(me.isCommit) {
								operator_ = "commit";//提交
							}
							
							var entity_ = clone(this.formData1);
							var entity = {};
							for (var r in entity_) {
								if (entity_[r] || entity_[r] == "" || entity_[r] == false || entity_[r] == 0) {
									entity[r] = entity_[r];
								}
							}
							
							var tableData_ = [];
							this.tablefields_list.map(rt=>{
								if (this.tableDataObj[rt.recordtemplateid].length) {
									this.tableDataObj[rt.recordtemplateid].map(r=>{
										var row_ = {};
										for (var k in r) {
											if (r[k]) {
												row_[k] = r[k];
											}
										}
										tableData_.push(row_);
									})
								}
							})
							
							if (tableData_.length == 0) {
								Root.message({
									type: 'warning',
									message: '请先添加明细数据'
								});
								return
							}
							
							let param = {
								isClientMode: false,
								dataname: this.form_dataname,
								// operator: operator_,
								data: {},
							}
							param.data[this.form_dataname] = entity;
							param.data[this.table_dataname] = tableData_;
							
							Server.call("dmsroot/data/saveEntity", param, function(result) {
								console.log(result);
								me.fullscreenLoading = false;
								if (result.success) {
									if(me.isCommit){
										me.isCommit = false;
										Root.message({
											type: 'success',
											message: '提交成功'
										}); 
										me.saveAfter();
									}
									else {
										Root.message({
											type: 'success',
											message: '保存成功'
										}); 
										me.saveAfter();
									}
								}
								
							}, function() {
								me.fullscreenLoading = false;
							});
						},
						
						onpassApproval(){
							this.textarea2 = "";
							this.reasonvisible = true;
							this.passOrRefuse = true;
						},
						onrefuseApproval(){
							this.textarea2 = "";
							this.reasonvisible = true;
							this.passOrRefuse = false;
						},
						saveReason(){
							if(this.passOrRefuse){
								this.doPassApproval(true);
							}else{
								if(this.textarea2 != ""){
									this.doPassApproval(false);
								}
								else{
									Root.message({
										type: 'warning',
										message: '请填写原因'
									});
								}
							}
						},
						
						doPassApproval(bo) {
							var me = this;
							var state_ = "open"
							if (!bo) {
								state_ = "input"
							}
							
							let param = {
								isClientMode: false,
								dataname: "agreementrecordmain",
								entity: {
									id: me.formData1.id,
									recordstatus: state_
								}
							}
							Server.call("root/data/updateEntity", param, function(result) {
								console.log(result);
								if (bo) {
									Root.message({
										type: 'success',
										message: '提交成功'
									}); 
									me.saveAfter();
								}
								else {
									Root.message({
										type: 'success',
										message: '已拒绝'
									}); 
									me.saveAfter();
								}
							});
							
							/* let param = {
								isClientMode: false,
								dataname: "agreementrecordmain",
								id: me.formData1.id,
								state: state_
							}
							Server.call("root/data/changeState", param, function(result) {
								console.log(result);
								if (bo) {
									Root.message({
										type: 'success',
										message: '提交成功'
									}); 
									//拆单时要不要重新调用接口
									me.saveAfter();
								}
								else {
									Root.message({
										type: 'success',
										message: '已拒绝'
									}); 
									me.saveAfter();
								}
							}); */
						},
						
						setDefaultExpandAll() {
							let me = this;
							this.isRefresh = false;
							this.defaultExpandAll = !this.defaultExpandAll;
							
							this.$nextTick(function(){
								me.isRefresh = true;
							})
							
						},
						
					}
				});
			};
			
			loadJsCss(function () {
				initVue();
			});
		</script>
		
		<style>
			.el-dialog__body .h_table {
			    margin-top: 0px;
			}
		</style>
	</head>
	
	<body style="margin: 0px;">
		<div v-cloak id="vbody">
			<div id="page_root">
				<div ref="popup_body" style="">
					<div class="el-dialog__header">
						<div class="dialog-title">
							<i class="iconfont icon-customermanagement"></i>
							<span> {{title}} </span>
						</div>
					</div>
					<!-- 
					<el-tabs v-model="activeName" type="card" @tab-click="handleClick">
					    <el-tab-pane v-for="(item, k) in tabs" :key="k" :label="item.label" :name="item.name"></el-tab-pane>
					</el-tabs> -->
					
					<div style="height: 550px; overflow: auto; padding-top: 0px;">
						<div class="el-dialog__body">
							<h-form
								v-if="formFields1.length"
								ref="form1"
								:form-attr="formAttr1"
								:table-fields="formFields1"
								:form-data="formData1"
								
								:is-old-contrast="true"
								:form-data-old="formData1Old"
								
								v-on:show-popup="showPopup"
								v-on:select-change="selectChange"
								v-on:visible-change="visibleChange"
							>
							</h-form>
							<h-form
								v-if="formFields2.length"
								ref="form2"
								:form-attr="formAttr2"
								:table-fields="formFields2"
								:form-data="formData1"
								
								:is-old-contrast="true"
								:form-data-old="formData1Old"
								
								v-on:show-popup="showPopup"
								v-on:select-change="selectChange"
							>
							</h-form>
							
							<div style="margin-top: 10px;">
								<template>
									<el-tabs v-model="template_tabsname" @tab-click="handleClick">
										<el-tab-pane v-for="(fieldsobj, k) in tablefields_list" :key="k" :label="fieldsobj.name" :name="fieldsobj.recordtemplateid"></el-tab-pane>
									</el-tabs>
								</template>
							</div>
							
							<div v-if="tablefields_obj[template_tabsname] && tablefields_obj[template_tabsname].fields" style="width: 100%; display: inline-block; text-align: right;margin-top: 10px;">
								<el-button-group>
									<el-button @click="setDefaultExpandAll">{{defaultExpandAll ? '收起' : '展开'}}对比数据</el-button>
								</el-button-group>
							</div>
							<div v-if="tablefields_obj[template_tabsname] && tablefields_obj[template_tabsname].fields" style="width: 100%; display: inline-block; text-align: right;margin-top: 10px;">
								<el-button-group v-if="notdisabled">
									<el-button :disabled="!formData1.customer_id" @click="showOrder">新增数据</el-button>
								</el-button-group>
							</div>
							
							<h-table
								v-if="isRefresh && tablefields_obj[template_tabsname] && tablefields_obj[template_tabsname].fields"
								ref="table2"
								:table-fields="tablefields_obj[template_tabsname].fields" 
								:table-data="tableDataObj[template_tabsname]" 
								:table-height="tableHeight2"
								:is-pagination="false"
								
								:rowkey="rowkey"
								:default-expand-all="defaultExpandAll"
								:is-old-contrast="true"
								:table-data-old="tableDataOldObj[template_tabsname]"
							>
							</h-table>
							<!-- 
							 :is-show-index="true"
							 -->
						</div>
						
						<el-dialog title="填写原因" :visible.sync="reasonvisible" height="50%">
							<el-input
							  type="textarea"
							  :autosize="{ minRows: 2, maxRows: 10}"
							  placeholder="请输入原因"
							  v-model="textarea2">
							</el-input>
							
							<div slot="footer" class="dialog-footer" >
							  <el-button size="small" type="default" @click="reasonvisible = false">取 消</el-button>
							  <el-button size="small" type="primary" @click="saveReason">确 定</el-button>
							</div>
						</el-dialog>
					</div>
					
					<div class="el-dialog__footer">
						<el-button-group v-if="approval" style="margin: 0px 20px;">
							<el-button size="small" type="success" @click="onpassApproval">通过</el-button>
						</el-button-group>
						<el-button-group v-if="approval" style="margin: 0px 20px;">
							<el-button size="small" type="danger" @click="onrefuseApproval">拒绝</el-button>
						</el-button-group>
						<el-button size="small" type="default" @click="closeDialog">取 消</el-button>
						<el-button size="small" v-if="notdisabled" v-loading.fullscreen.lock="fullscreenLoading"  type="primary" @click="saveRowTable":icon="buttonsconfig.save.icon">{{buttonsconfig.save.name}}</el-button>
						<el-button size="small" v-if="notdisabled" v-loading.fullscreen.lock="fullscreenLoading" type="success" @click="submitRowTable":icon="buttonsconfig.submit.icon">{{buttonsconfig.submit.name}}</el-button>
					</div>
				</div>
				
				<el-dialog title="选择协议模板" 
				:close-on-click-modal="false"
				:close-on-press-escape="false"
				:show-close="false"
				:visible.sync="dialog_template" height="50%">
					<h-form
						ref="form_t"
						:form-attr="formAttr_t"
						:table-fields="formFields_t"
						:form-data="formData_t"
						v-on:show-popup="showPopup_t"
						v-on:select-change="selectChange_t"
						v-on:visible-change="visibleChange_t"
					>
					</h-form>
				
					<!-- <el-cascader style="width: 100%;" v-model="value_template" :options="options_template" :props="props_category" :show-all-levels="false" ></el-cascader>
					 -->
					<div slot="footer" class="dialog-footer" >
						<span v-if="!formData_t.template_id" style="color: red; font-size: 10px; margin-right: 10px;">请先选择协议模板和客户名称</span>
						<el-button size="small" :disabled="!(formData_t.template_id && formData_t.customer_id)" type="primary" @click="templateSave">确 定</el-button>
						<el-button size="small" type="default" @click="closeDialog">取 消</el-button>
					</div>
				</el-dialog>
				
			</div>
			
			<div id="page_loading" style="position: absolute; top:0px; width: 100vw; height: 50vh;">
				<div class="spinner">
				  <div class="cube1"></div>
				  <div class="cube2"></div>
				</div>
			</div>
		</div>	
		
	</body>
</html>