public with sharing class NewDBPaymentPlanLoadController {
	public String idl {get; set;}
	public String statusSave {get; set;}
	public Datetime testDateTime {set; get;}
	public MaintanceContractPack__c PageMCEElement {get; set;}
	public  List<Maintenance_Contract__c> MC {get; set;}
	public Boolean printContract { get; private set; }
	public Boolean printAgent { get; private set; }
	public Boolean limitPrice { get; private set; }
	public Integer Contract_validMonth {get; set;}
	public Boolean UpperLimitPrice { get; private set; }
    public NewDBPaymentPlanLoadController() {
		idl = ApexPages.currentPage().getParameters().get('id');
	}
    public void init() {
		statusSave = 'Fin';
		testDateTime = system.now();
		//printContract = true;
		List<MaintanceContractPack__c> PageMCE = [select
		                                         id,
		                                         Name,
		                                         RecordTypeId,
		                                         Contract_Type__c,
												 Is_Standard_Contract__c,
												 Small_Repair_Time__c,
												 Big_Repair_Time__c,
												 Live_Technical_Support_Time__c,
												 Tour_Check__c,
												 URF_Contract__c,
												 Engineer_Name__c,
												 Phone_Number__c,
												 Inspection_Time__c,
												 Dedust__c,
												 First_Party_Contract_Number__c,
												 Second_Party_Contract_Number__c,
												 Negotiate_Mean__c,
												 Term_Of_Payment__c,
												 old_Is_RecognitionModel__c,
												 Total_Payment_Time__c,
												 Payment_Plan_Sum_First__c,
		                                         Payment_Plan_Date_First__c,
		                                         Payment_Plan_Sum_Fifth__c,
		                                         Payment_Plan_Date_Fifth__c,
		                                         Payment_Plan_Sum_Third__c,
		                                         Payment_Plan_Date_Third__c,
		                                         Payment_Plan_Sum_Forth__c,
		                                         Payment_Plan_Date_Forth__c,
		                                         Payment_Plan_Sum_Second__c,
		                                         Payment_Plan_Date_Second__c,
		                                         Payment_Plan_Sum_Sixth__c,
		                                         Payment_Plan_Date_Sixth__c,
												 Payment_Plan_Sum_First_in_Word__c,
		                                         Payment_Plan_Sum_Second_in_Word__c,
		                                         Payment_Plan_Sum_Third_in_Word__c,
		                                         Payment_Plan_Sum_Forth_in_Word__c,
		                                         Payment_Plan_Sum_Fifth_in_Word__c,
		                                         Payment_Plan_Sum_Sixth_in_Word__c,
												 Amount_Warranty_Coverage__c,
												 Amount_Warranty_Coverage_In_Word__c,
												 Total_Contract_Amount_In_Words__c,
												 Contract_Amount__c,
												 Limit_Price_Amount__c,
												 Contract_Range__c,
												 one_off_payment_agreed_period__c,
												 Service_contract_target_number__c,
												 Contract_End_Date__c,
												 Contract_Start_Date__c,
												 Estimation_Id__c,
												 Estimation_Id__r.Estimate_Target__c,
												 Estimate_Target__c,
												 Decided_Estimation__c,
												 FieldRequest_quotation_Amount__c,
												 Total_FieldRequest_quotation_In_Words__c,
												 Limit_Price_Amount_In_Words__c
		                                         from
		                                         MaintanceContractPack__c
		                                         where
		                                         id = :idl];


												  //小合同
        List<Maintenance_Contract__c> MCList = [select
												id,
												Name,
												Payment_Plan_Sum_First__c,
												Payment_Plan_Date_First__c,
												Payment_Plan_Sum_Fifth__c,
												Payment_Plan_Date_Fifth__c,
												Payment_Plan_Sum_Third__c,
												Payment_Plan_Date_Third__c,
												Payment_Plan_Sum_Forth__c,
												Payment_Plan_Date_Forth__c,
												Payment_Plan_Sum_Second__c,
												Payment_Plan_Date_Second__c,
												Payment_Plan_Sum_Sixth__c,
												Payment_Plan_Date_Sixth__c,
												Payment_Plan_Sum_First_in_Word__c,
												Payment_Plan_Sum_Second_in_Word__c,
												Payment_Plan_Sum_Third_in_Word__c,
												Payment_Plan_Sum_Forth_in_Word__c,
												Payment_Plan_Sum_Fifth_in_Word__c,
												Payment_Plan_Sum_Sixth_in_Word__c,
												Management_Code__c,
												Contract_Amount__c,
												Contract_Amount_Text__c,
												Total_Contract_Amount_In_Words__c,
												Service_contract_target_number__c,
												Contract_End_Date__c,
												Contract_Start_Date__c,
												Contract_Range__c,
												Contract_Period__c
												from
												Maintenance_Contract__c
												where
												MaintanceContractPack__c = :idl];
		if (MCList.size()>0) {
			MC = MCList;
		}
		//权限管理-Denid
		if (PageMCE.size() == 1) {
			PageMCEElement = PageMCE[0];
		} else {
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '找不到对应的维修合同'));
			statusSave = 'Denied';
		}
		if (statusSave.equals('Fin') && PageMCEElement.RecordTypeId.equals('012C80000000NHZIA2')) {
			printContract = true;
			printAgent = false;
		} 
		if (statusSave.equals('Fin') && PageMCEElement.RecordTypeId.equals('012C80000000NHYIA2')) {
			printContract = false;
			printAgent = true;
		}
		if (PageMCEElement.Contract_Type__c == '上限合同') {
			limitPrice = true;
		}
		if (PageMCEElement.Contract_End_Date__c != null || PageMCEElement.Contract_Start_Date__c != null) {
			Contract_validMonth = Integer.valueOf(PageMCEElement.Contract_Range__c);
			// Contract_validMonth = PageMCEElement.Contract_Start_Date__c.monthsBetween(PageMCEElement.Contract_End_Date__c);
			System.debug('Contract_validMonth:'+Contract_validMonth);
		}
	}

	public PageReference save() {
		/*验证是否输入付款计划
		*多次付款至少输入付款计划1和付款计划1金额
		*一次性付款需要输入一次性付款约定期限
		*/
		if (PageMCEElement.Is_Standard_Contract__c == null  ) {
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '请选择是否标准合同！'));
			statusSave = 'Denied';
			return null;
		}
		if (PageMCEElement.Term_Of_Payment__c != null &&
		        '多次付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
			if (PageMCEElement.Payment_Plan_Sum_First__c == null || PageMCEElement.Payment_Plan_Date_First__c == null || PageMCEElement.Payment_Plan_Sum_First__c == 0) {
				ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '请输入至少一个付款计划！'));
				statusSave = 'Denied';
				return null;
			}

		} else if (PageMCEElement.Term_Of_Payment__c != null
		           && '一次性付款'.equals(PageMCEElement.Term_Of_Payment__c)
		           && PageMCEElement.one_off_payment_agreed_period__c == null ) {
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '请输入付款计划！'));
			statusSave = 'Denied';
			return null;
		}
		// gzw fix 验证设置为全局
		if ((PageMCEElement.Inspection_Time__c == null || ''.equals(PageMCEElement.Inspection_Time__c) || '0'.equals(PageMCEElement.Inspection_Time__c))
			//URF限次合同2期 LY 20220811 start
			&& !PageMCEElement.URF_Contract__c) {
			//URF限次合同2期 LY 20220811 end
			System.debug('11111111111' + PageMCEElement.Inspection_Time__c);
			PageMCEElement.Inspection_Time__c.adderror('点检次数必选项');
			statusSave = 'Denied';
			return null;
		}
		// 点检次数必须填 Star
		// saveErrorflag = false;
		// if (PageMCEElement.Is_Standard_Contract__c == '是') {
		// 	if (PageMCEElement.Contract_End_Date__c == null || PageMCEElement.Contract_Start_Date__c == null) {
		// 		ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '请审批报价并decide'));
		// 		statusSave = 'Denied';
		// 		return null;
		// 	}
		// }

		// 点检次数必须填 end
		Savepoint sp = Database.setSavepoint();
		try {
		// 	// 对于标准合同进行验证必填项
			string requiredInput = '';
			if (PageMCEElement.Is_Standard_Contract__c.equals('是')) {
				if (PageMCEElement.Engineer_Name__c == null || ''.equals(PageMCEElement.Engineer_Name__c)) {
					requiredInput += '工程师姓名';
				}
				if (PageMCEElement.Small_Repair_Time__c == null || ''.equals(PageMCEElement.Small_Repair_Time__c)) {
					if (!''.equals(requiredInput)) {
						requiredInput += '、';
					}
					requiredInput += '小修理时限';
				}
				if (PageMCEElement.Big_Repair_Time__c == null || ''.equals(PageMCEElement.Big_Repair_Time__c)) {
					if (!''.equals(requiredInput)) {
						requiredInput += '、';
					}
					requiredInput += '大修理时限';
				}
				if (PageMCEElement.Live_Technical_Support_Time__c == null || ''.equals(PageMCEElement.Live_Technical_Support_Time__c)) {
					if (!''.equals(requiredInput)) {
						requiredInput += '、';
					}
					requiredInput += '现场技术支持时限';
				}
				if (PageMCEElement.Tour_Check__c == null || ''.equals(PageMCEElement.Tour_Check__c)) {
					if (!''.equals(requiredInput)) {
						requiredInput += '、';
					}
					requiredInput += '巡回';
				}
				if (PageMCEElement.Inspection_Time__c == null || ''.equals(PageMCEElement.Inspection_Time__c)) {
					if (!''.equals(requiredInput)) {
						requiredInput += '、';
					}
					requiredInput += '点检';
				}
				if (PageMCEElement.Dedust__c == null || ''.equals(PageMCEElement.Dedust__c)) {
					if (!''.equals(requiredInput)) {
						requiredInput += '、';
					}
					requiredInput += '除尘';
				}

				if (PageMCEElement.First_Party_Contract_Number__c == null || ''.equals(PageMCEElement.First_Party_Contract_Number__c)) {
					if (!''.equals(requiredInput)) {
						requiredInput += '、';
					}
					requiredInput += '合同份数：甲方:';
				}
				if (PageMCEElement.Second_Party_Contract_Number__c == null || ''.equals(PageMCEElement.Second_Party_Contract_Number__c)) {
					if (!''.equals(requiredInput)) {
						requiredInput += '、';
					}
					requiredInput += '合同份数：乙方:';
				}
				if (!''.equals(requiredInput)) {
					statusSave = 'Denied';
					ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '该合同是标准合同，请输入：' + requiredInput));
					return null;
				}
			}
			List<Maintenance_Contract__c> listMc = new List<Maintenance_Contract__c>();
			// 处理大合同 若付款计划金额为空，则对应的付款计划日也为空
			if (PageMCEElement.Payment_Plan_Sum_First__c == null) {
				PageMCEElement.Payment_Plan_Date_First__c = null;
			}
			if (PageMCEElement.Payment_Plan_Sum_Second__c == null) {
				PageMCEElement.Payment_Plan_Date_Second__c = null;
			}
			if (PageMCEElement.Payment_Plan_Sum_Third__c == null) {
				PageMCEElement.Payment_Plan_Date_Third__c = null;
			}
			if (PageMCEElement.Payment_Plan_Sum_Forth__c == null) {
				PageMCEElement.Payment_Plan_Date_Forth__c = null;
			}
			if (PageMCEElement.Payment_Plan_Sum_Fifth__c == null) {
				PageMCEElement.Payment_Plan_Date_Fifth__c = null;
			}
			if (PageMCEElement.Payment_Plan_Sum_Sixth__c == null) {
				PageMCEElement.Payment_Plan_Date_Sixth__c = null;
			}
			// 处理 将大合同的文本信息 录入小合同中 start wu
			for (Maintenance_Contract__c m : MC) {
				// 文本信息
				m.Small_Repair_Time__c = PageMCEElement.Small_Repair_Time__c;
				m.Engineer_Name__c = PageMCEElement.Engineer_Name__c;
				m.Big_Repair_Time__c = PageMCEElement.Big_Repair_Time__c;
				m.Live_Technical_Support_Time__c = PageMCEElement.Live_Technical_Support_Time__c;
				m.Tour_Check__c = PageMCEElement.Tour_Check__c;
				m.Dedust__c = PageMCEElement.Dedust__c;
				m.First_Party_Contract_Number__c = PageMCEElement.First_Party_Contract_Number__c;
				m.Second_Party_Contract_Number__c = PageMCEElement.Second_Party_Contract_Number__c;
				m.Phone_Number__c = PageMCEElement.Phone_Number__c;
				m.Inspection_Time__c = PageMCEElement.Inspection_Time__c;
				m.Negotiate_Mean__c = PageMCEElement.Negotiate_Mean__c;
				// 日期 小合同付款金额不为空 方可给日期赋值
				if (m.Payment_Plan_Sum_First__c != null) {
					m.Payment_Plan_Date_First__c = PageMCEElement.Payment_Plan_Date_First__c;
				}
				if (m.Payment_Plan_Sum_Second__c != null) {
					m.Payment_Plan_Date_Second__c = PageMCEElement.Payment_Plan_Date_Second__c;
				}
				if (m.Payment_Plan_Sum_Third__c != null) {
					m.Payment_Plan_Date_Third__c = PageMCEElement.Payment_Plan_Date_Third__c;
				}
				if (m.Payment_Plan_Sum_Forth__c != null) {
					m.Payment_Plan_Date_Forth__c = PageMCEElement.Payment_Plan_Date_Forth__c;
				}
				if (m.Payment_Plan_Sum_Fifth__c != null) {
					m.Payment_Plan_Date_Fifth__c = PageMCEElement.Payment_Plan_Date_Fifth__c;
				}
				if (m.Payment_Plan_Sum_Sixth__c != null) {
					m.Payment_Plan_Date_Sixth__c = PageMCEElement.Payment_Plan_Date_Sixth__c;
				}
				if (PageMCEElement.Term_Of_Payment__c != null &&
				'多次付款'.equals(PageMCEElement.Term_Of_Payment__c)) {

					// 判断每一期的金额总额是否等于最终用户合同金额	Contract_Amount__c
							decimal PriceSum2 =  (m.Payment_Plan_Sum_First__c == null ? 0 :  m.Payment_Plan_Sum_First__c) +
							(m.Payment_Plan_Sum_Second__c == null ? 0 : m.Payment_Plan_Sum_Second__c) +
							(m.Payment_Plan_Sum_Third__c == null ? 0 :  m.Payment_Plan_Sum_Third__c) +
							(m.Payment_Plan_Sum_Forth__c == null ? 0 :  m.Payment_Plan_Sum_Forth__c) +
							(m.Payment_Plan_Sum_Fifth__c == null ? 0 :  m.Payment_Plan_Sum_Fifth__c) +
							(m.Payment_Plan_Sum_Sixth__c == null ? 0 :  m.Payment_Plan_Sum_Sixth__c);
							if (PriceSum2 != m.Contract_Amount__c ) {
							System.debug('总金额：'+PriceSum2);
							ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,
												'编号为：'+m.Management_Code__c+'付款总金额与合同金额不符。'));
							statusSave = 'Denied';
							return null;
							}
				}	
				if (PageMCEElement.Inspection_Time__c != null) {

					if (m.Contract_End_Date__c != null || m.Contract_Start_Date__c != null) {
						// 总年数
						//合同总月数
						Integer Contract_validMonth = Integer.valueOf(m.Contract_Period__c);
						// add 一年内点检区间不够生成一次点检计划，默认生成一个点检计划，计划区间为合同开始日-结束日
						if((Integer.valueOf(PageMCEElement.Inspection_Time__c) == 1 && Contract_validMonth < 9) 
							|| (Integer.valueOf(PageMCEElement.Inspection_Time__c) == 2 && Contract_validMonth < 6) 
							|| (Integer.valueOf(PageMCEElement.Inspection_Time__c) == 3 && Contract_validMonth < 4)
							|| (Integer.valueOf(PageMCEElement.Inspection_Time__c) == 4 && Contract_validMonth < 3)){
							m.Service_contract_target_number__c = 1;
						}else{
						// add 一年内点检区间不够生成一次点检计划，默认生成一个点检计划，计划区间为合同开始日-结束日
							Integer All_check;
							//24
							if (Contract_validMonth - Integer.valueOf(Contract_validMonth / 12) * 12 >= 6
									&& Integer.valueOf(PageMCEElement.Inspection_Time__c) == 1) {
								All_check = (Contract_validMonth * Integer.valueOf(PageMCEElement.Inspection_Time__c)) / 12 + 1;
							} else {
								All_check = (Contract_validMonth * Integer.valueOf(PageMCEElement.Inspection_Time__c)) / 12;
							}
							// Integer All_check = (Contract_validMonth * Integer.valueOf(PageMCEElement.Inspection_Time__c)) / 12;
							m.Service_contract_target_number__c = All_check;	
						}
					}
				}
				list<String> args = new String[]{'0','number','###,###,###,###,##0'};
				// 处理小合同的金额文字
				if (m.Contract_Amount__c != null) {
					m.Contract_Amount_Text__c = String.format(m.Contract_Amount__c.format(),args);
					m.Total_Contract_Amount_In_Words__c = convertNumberToChinese(m.Contract_Amount__c+'');
				}
				System.debug('点检次数：'+m.Service_contract_target_number__c);
				listMc.add(m);
			}
			System.debug('update listMc>>>>>>>>');
			// 更新 小合同
			update listMc;
			System.debug('update listMc  end>>>>>>>>');
			// 处理 将大合同的文本信息 录入小合同中 end wu

			if (PageMCEElement.Term_Of_Payment__c != null &&
			'多次付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
				decimal PriceSum =  (PageMCEElement.Payment_Plan_Sum_First__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_First__c) +
									(PageMCEElement.Payment_Plan_Sum_Second__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Second__c) +
									(PageMCEElement.Payment_Plan_Sum_Third__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Third__c) +
									(PageMCEElement.Payment_Plan_Sum_Forth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Forth__c) +
									(PageMCEElement.Payment_Plan_Sum_Fifth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Fifth__c) +
									(PageMCEElement.Payment_Plan_Sum_Sixth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Sixth__c);
									// 2023-11-17 WYL 修改 申请报价金额改为最终用户合同金额 FieldRequest_quotation_Amount__c =》Contract_Amount__c
				if (PriceSum != PageMCEElement.Contract_Amount__c ) {
					System.debug('总金额：'+PriceSum);
					ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,
										'付款总金额与合同金额不符。'));
					statusSave = 'Denied';
					return null;
				}
			}
			if (PageMCEElement.Term_Of_Payment__c != null
					&& '一次性付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
						// 2023-11-17 WYL 修改 申请报价金额改为最终用户合同金额 FieldRequest_quotation_Amount__c =》Contract_Amount__c
				PageMCEElement.Payment_Plan_Sum_First__c 	= PageMCEElement.Contract_Amount__c;
				PageMCEElement.Payment_Plan_Sum_Second__c   = null;
				PageMCEElement.Payment_Plan_Sum_Third__c    = null;
				PageMCEElement.Payment_Plan_Sum_Forth__c    = null;
				PageMCEElement.Payment_Plan_Sum_Fifth__c    = null;
				PageMCEElement.Payment_Plan_Sum_Sixth__c    = null;

				PageMCEElement.Payment_Plan_Date_First__c   = null;
				PageMCEElement.Payment_Plan_Date_Second__c  = null;
				PageMCEElement.Payment_Plan_Date_Third__c   = null;
				PageMCEElement.Payment_Plan_Date_Forth__c   = null;
				PageMCEElement.Payment_Plan_Date_Fifth__c   = null;
				PageMCEElement.Payment_Plan_Date_Sixth__c   = null;
				PageMCEElement.Total_Payment_Time__c        = null;
				// 合同为一次付款需要清空小合同上的所有付款日期和金额
				List<Maintenance_Contract__c> listMc2 = new List<Maintenance_Contract__c>();
				for (Maintenance_Contract__c m : MC) {
					m.Payment_Plan_Date_First__c = null;
					m.Payment_Plan_Sum_First__c = m.Contract_Amount__c;
					m.Payment_Plan_Sum_First_in_Word__c = null;
					m.Payment_Plan_Date_Third__c = null;
					m.Payment_Plan_Sum_Third__c = null;
					m.Payment_Plan_Sum_Third_in_Word__c = null;
					m.Payment_Plan_Date_Second__c = null;
					m.Payment_Plan_Sum_Second__c = null;
					m.Payment_Plan_Sum_Second_in_Word__c = null;
					m.Payment_Plan_Date_Fifth__c = null;
					m.Payment_Plan_Sum_Fifth__c = null;
					m.Payment_Plan_Sum_Fifth_in_Word__c = null;
					m.Payment_Plan_Date_Sixth__c = null;
					m.Payment_Plan_Sum_Sixth__c = null;
					m.Payment_Plan_Sum_Sixth_in_Word__c = null;
					m.Payment_Plan_Date_Forth__c = null;
					m.Payment_Plan_Sum_Forth__c = null;
					m.Payment_Plan_Sum_Forth_in_Word__c = null;
					listMc2.add(m);
				}
				update listMc2;
			} else {
				PageMCEElement.one_off_payment_agreed_period__c        = null;

			}
			if (PageMCEElement.Is_Standard_Contract__c != null && PageMCEElement.Is_Standard_Contract__c.equals('否')) {
				System.debug('-------1-------'+PageMCEElement.Is_Standard_Contract__c);
				PageMCEElement.Engineer_Name__c = null;
				PageMCEElement.Phone_Number__c = null;
				PageMCEElement.Small_Repair_Time__c = null;
				PageMCEElement.Big_Repair_Time__c = null;
				PageMCEElement.Live_Technical_Support_Time__c = null;
				PageMCEElement.Tour_Check__c = null;
				// gzw fix 20210929 追加非标准合同点检次数
				// PageMCEElement.Inspection_Time__c = null;
				PageMCEElement.Dedust__c = null;
				PageMCEElement.First_Party_Contract_Number__c = null;
				PageMCEElement.Second_Party_Contract_Number__c = null;
				// gzw fix 20210929 追加非标准合同点检次数
				// PageMCEElement.Service_contract_target_number__c = null;

			}
			System.debug('update PageMCEElement>>>>>>>>');
			System.debug('PageMCEElement>>>>>>>>' + PageMCEElement);
			update PageMCEElement;
			System.debug('update PageMCEElement end>>>>>>>>');
			statusSave = 'Fin';
			//
			// NFM118Controller.sendPostRequest();
			//
			return null;
		} catch (exception ex) {
			statusSave = 'Denied';
			Database.rollback(sp);
			ApexPages.addmessages(ex);
			System.debug('=========>' + ex.getLineNumber());
			return null;
		}
		// return null;
	}

	public void print() {
		System.debug('print start');
		save();
		System.debug('save end');
		if (PageMCEElement.Term_Of_Payment__c != null &&
		        '多次付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
			decimal PriceSum =  (PageMCEElement.Payment_Plan_Sum_First__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_First__c) +
			                    (PageMCEElement.Payment_Plan_Sum_Second__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Second__c) +
			                    (PageMCEElement.Payment_Plan_Sum_Third__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Third__c) +
			                    (PageMCEElement.Payment_Plan_Sum_Forth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Forth__c) +
			                    (PageMCEElement.Payment_Plan_Sum_Fifth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Fifth__c) +
			                    (PageMCEElement.Payment_Plan_Sum_Sixth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Sixth__c);
				// 2023-11-17 WYL 修改 申请报价金额改为最终用户合同金额 FieldRequest_quotation_Amount__c =》Contract_Amount__c					
			if (PriceSum != PageMCEElement.Contract_Amount__c ) {
				ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,
				                     '付款总金额与合同金额不符。'));
				statusSave = 'Denied';
			}
		
		// 	// HWAG-BHT7XX 2019-12-09 by vivek start
			Integer payTimeSum = 0;
			if (PageMCEElement.Payment_Plan_Sum_First__c != null  && PageMCEElement.Payment_Plan_Date_First__c != null) {
				payTimeSum++; 
			} 
			if (PageMCEElement.Payment_Plan_Sum_Second__c != null  && PageMCEElement.Payment_Plan_Date_Second__c != null) {
				payTimeSum++; 
			} 
			if (PageMCEElement.Payment_Plan_Sum_Third__c != null  && PageMCEElement.Payment_Plan_Date_Third__c != null) {
				payTimeSum++; 
			} 
			if (PageMCEElement.Payment_Plan_Sum_Forth__c != null  && PageMCEElement.Payment_Plan_Date_Forth__c != null) {
				payTimeSum++; 
			} 
			if (PageMCEElement.Payment_Plan_Sum_Fifth__c != null  && PageMCEElement.Payment_Plan_Date_Fifth__c != null) {
				payTimeSum++; 
			} 
			if (PageMCEElement.Payment_Plan_Sum_Sixth__c != null  && PageMCEElement.Payment_Plan_Date_Sixth__c != null) {
				payTimeSum++; 
			} 
			if (PageMCEElement.Total_Payment_Time__c != null) {
				system.debug(payTimeSum + '======' + Integer.valueOf(PageMCEElement.Total_Payment_Time__c));
				if (Integer.valueOf(PageMCEElement.Total_Payment_Time__c) != payTimeSum) {
					System.debug(Integer.valueOf(PageMCEElement.Total_Payment_Time__c) + '======');
					ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,
					                     '付款期数和付款计划数目不符。'));
					statusSave = 'Denied';
				}
			}

		// 	// HWAG-BHT7XX 2019-12-09 by vivek end
		}
		if (statusSave.equals('Fin') && PageMCEElement.RecordTypeId.equals('012C80000000NHZ')) {
			printContract = true;
			printAgent = false;
		} 
		if (statusSave.equals('Fin') && PageMCEElement.RecordTypeId.equals('012C80000000NHYIA2')) {
			printContract = false;
			printAgent = true;
		}


		System.debug(statusSave);
		System.debug('print end111111111111'+printContract);
		System.debug('print end111111111111'+printAgent);
		System.debug('print end'+PageMCEElement);
	}
	public List<SelectOption> getSmall_Repair_Time() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('5', '5'));
		options.add(new SelectOption('7', '7'));
		options.add(new SelectOption('10', '10'));
		return options;
	}
	public List<SelectOption> getLive_Technical_Support_Time() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('24', '24'));
		options.add(new SelectOption('48', '48'));
		options.add(new SelectOption('72', '72'));
		return options;
	}
	public List<SelectOption> getBig_Repair_Time() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('7', '7'));
		options.add(new SelectOption('10', '10'));
		options.add(new SelectOption('15', '15'));
		options.add(new SelectOption('20', '20'));
		options.add(new SelectOption('25', '25'));
		options.add(new SelectOption('30', '30'));
		return options;
	}
	public List<SelectOption> getTour_Check() {
		List<SelectOption> options = new List<SelectOption>();
		if (PageMCEElement.URF_Contract__c ){
			options.add(new SelectOption('0', '0'));
		}else {
			options.add(new SelectOption('0', '0'));
			options.add(new SelectOption('1', '1'));
			options.add(new SelectOption('2', '2'));
			options.add(new SelectOption('3', '3'));
			options.add(new SelectOption('4', '4'));
		}
		return options;
	}
	public List<SelectOption> getDedust() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('0', '0'));
		options.add(new SelectOption('1', '1'));
		options.add(new SelectOption('2', '2'));
		return options;
	}
	public List<SelectOption> getFirst_Party_Contract_Number() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('1', '1'));
		options.add(new SelectOption('2', '2'));
		options.add(new SelectOption('3', '3'));
		options.add(new SelectOption('4', '4'));
		options.add(new SelectOption('5', '5'));
		options.add(new SelectOption('6', '6'));
		options.add(new SelectOption('7', '7'));
		options.add(new SelectOption('8', '8'));
		options.add(new SelectOption('9', '9'));
		options.add(new SelectOption('10', '10'));
		return options;
	}
	public List<SelectOption> getNegotiate_Mean() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('1', '1'));
		options.add(new SelectOption('2', '2'));
		options.add(new SelectOption('3', '3'));
		return options;
	}
	public List<SelectOption> getTerm_Of_Payment() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('一次性付款', '一次性付款'));
		if (!PageMCEElement.URF_Contract__c){
			options.add(new SelectOption('多次付款', '多次付款'));
		}
		return options;
	}

	public boolean getCheck_Of_Payment() {
		boolean Check_Of_Payment = true;
		System.debug('uuuu2'+PageMCEElement.Term_Of_Payment__c);
		if (PageMCEElement.Term_Of_Payment__c != null && '多次付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
			Check_Of_Payment = false;
		}
		System.debug('uuuu1'+Check_Of_Payment);
		return Check_Of_Payment;
	}
	public List<SelectOption> getTotal_Payment_Time() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('1', '1'));
		options.add(new SelectOption('2', '2'));
		options.add(new SelectOption('3', '3'));
		options.add(new SelectOption('4', '4'));
		options.add(new SelectOption('5', '5'));
		options.add(new SelectOption('6', '6'));
		return options;
	}
	public void dummy() {
				PageMCEElement.Payment_Plan_Sum_First__c 	= null;
				PageMCEElement.Payment_Plan_Sum_Second__c   = null;
				PageMCEElement.Payment_Plan_Sum_Third__c    = null;
				PageMCEElement.Payment_Plan_Sum_Forth__c    = null;
				PageMCEElement.Payment_Plan_Sum_Fifth__c    = null;
				PageMCEElement.Payment_Plan_Sum_Sixth__c    = null;

				PageMCEElement.Payment_Plan_Date_First__c   = null;
				PageMCEElement.Payment_Plan_Date_Second__c  = null;
				PageMCEElement.Payment_Plan_Date_Third__c   = null;
				PageMCEElement.Payment_Plan_Date_Forth__c   = null;
				PageMCEElement.Payment_Plan_Date_Fifth__c   = null;
				PageMCEElement.Payment_Plan_Date_Sixth__c   = null;
		List<Maintenance_Contract__c> listMc2 = new List<Maintenance_Contract__c>();
				for (Maintenance_Contract__c m : MC) {
					m.Payment_Plan_Date_First__c = null;
					m.Payment_Plan_Sum_First__c = null;
					m.Payment_Plan_Sum_First_in_Word__c = null;
					m.Payment_Plan_Date_Third__c = null;
					m.Payment_Plan_Sum_Third__c = null;
					m.Payment_Plan_Sum_Third_in_Word__c = null;
					m.Payment_Plan_Date_Second__c = null;
					m.Payment_Plan_Sum_Second__c = null;
					m.Payment_Plan_Sum_Second_in_Word__c = null;
					m.Payment_Plan_Date_Fifth__c = null;
					m.Payment_Plan_Sum_Fifth__c = null;
					m.Payment_Plan_Sum_Fifth_in_Word__c = null;
					m.Payment_Plan_Date_Sixth__c = null;
					m.Payment_Plan_Sum_Sixth__c = null;
					m.Payment_Plan_Sum_Sixth_in_Word__c = null;
					m.Payment_Plan_Date_Forth__c = null;
					m.Payment_Plan_Sum_Forth__c = null;
					m.Payment_Plan_Sum_Forth_in_Word__c = null;
					listMc2.add(m);
				}
				update listMc2;
	}
	public void Dummy2(){}
	public List<SelectOption> getone_off_payment_agreed_period() {

		//LJPH-BT535P 《委托》 付款计划的字段更新 精琢技术 wql 2020/09/04 start
		map<string, list<pickerWrapper>> pickerFields = new map<string, list<pickerWrapper>>();

		// 维修合同 一次性付款约定期限 选项值获取
		list<pickerWrapper> taskStatusOptions = new list<pickerWrapper>();
		Schema.DescribeFieldResult taskStatusfieldResult = Maintenance_Contract__c.one_off_payment_agreed_period__c.getDescribe();
		List<Schema.PicklistEntry> taskStatusple = taskStatusfieldResult.getPicklistValues();
		for ( Schema.PicklistEntry pickListVal : taskStatusple) {
			pickerWrapper temppickerWrapper =
			    new pickerWrapper(pickListVal.getLabel(), pickListVal.getValue());
			taskStatusOptions.add(temppickerWrapper);
		}
		pickerFields.put('taskStatusOptions', taskStatusOptions);

		List<pickerWrapper> pickerWrapperList = pickerFields.get('taskStatusOptions');
		List<SelectOption> options = new List<SelectOption>();
		List<String> filedList = new List<String>();
		List<String> selectOptionList = new List<String>();
		for (pickerWrapper pw : pickerWrapperList) {

			// options.add(new SelectOption(pw.label, pw.value));
			filedList.add(pw.value);
		}
		system.debug('PageMCEElement:' + PageMCEElement);
		system.debug('Estimate_Target__c:' + PageMCEElement.Estimate_Target__c);

		//LJPH-BV8ABG 《委托》当报价提交对象为医院时，付款天数增加【90】日内。 2020/11/11  start
		if (PageMCEElement.Estimate_Target__c != null && PageMCEElement.Estimate_Target__c == '医院') {
			filedList.add('90');
		}
		//LJPH-BV8ABG 《委托》当报价提交对象为医院时，付款天数增加【90】日内。 2020/11/11  end
		System.debug('PageMCEElement111：' + PageMCEElement);
		if (PageMCEElement.one_off_payment_agreed_period__c != null) {
			if (!filedList.contains(PageMCEElement.one_off_payment_agreed_period__c)) {
				filedList.add(PageMCEElement.one_off_payment_agreed_period__c);

				// options.add(new SelectOption(PageMCEElement.one_off_payment_agreed_period__c, PageMCEElement.one_off_payment_agreed_period__c));
			}
		}
		system.debug('filedList:' + filedList);
		//冒泡排序
		selectOptionList = BubbleSort(filedList);
		system.debug('selectOptionList :' + selectOptionList);
		for (String sol : selectOptionList) {
			options.add(new SelectOption(sol, sol));
		}

		return options;

		//LJPH-BT535P 《委托》 付款计划的字段更新 精琢技术 wql 2020/09/04 end
	}
	public class pickerWrapper {
		@AuraEnabled
		public string label;
		@AuraEnabled
		public string value;
		public pickerWrapper(string label, string value) {
			this.label = label;
			this.value = value;
		}

	}

	//冒泡排序
	public List<String> BubbleSort(List<String> arrayList) {
		//外层循环控制排序趟数
		for (Integer i = 1; i < arrayList.size(); i++) {

			//内层循环控制每一趟排序多少次 。 每次最后面的值都是最大值了 。 所以后面的都可以不用排序了。
			for (Integer j = 0; j < arrayList.size() - 1; j++) {
				if (arrayList[j] > arrayList[i]) {
					String temp = arrayList[j];
					arrayList[j] = arrayList[i];
					arrayList[i] = temp;
				}
			}
		}
		return arrayList;
	}
	// 处理数字转中文
	public String convertNumberToChinese(String inputNumber) {
		String[] CN_NUMERIC = new String[]{'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};
		String[] CN_UNIT = new String[]{'', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟'};
	  
		String result = '';
	  
		// 删除可能的逗号
		inputNumber = inputNumber.replace(',', '');
	  
		// 处理小数部分
		String[] parts = inputNumber.split('\\.');
	  
		if (parts.size() > 2) {
			return '无法处理的数字格式';
		}
	  
		String integerPart = parts[0];
	  
		// 处理整数部分
		if (!integerPart.equals('')) {
			Integer numLen = integerPart.length();
	  
			for (Integer i = 0; i < numLen; i++) {
				Integer digit = Integer.valueOf(integerPart.substring(i, i + 1));
				String digitCn = CN_NUMERIC[digit];
				Integer unitPos = numLen - i - 1;
				String unit = CN_UNIT[unitPos];
	  
				if (digit == 0) {
					if (unitPos == 8 || unitPos == 4 || unitPos == 0) {
						result += unit;
					}
				} else {
					result += digitCn + unit;
				}
			}
			
			result += '元整'; // 添加"整"字表示整数金额
		} else {
			return '零元整';
		}
	  
		return result;
	}
	

}