public with sharing class MCHPDFController {
    public MCInfo targetMC {get; set;}

    public MCHPDFController() {

    }
     public MCHPDFController(ApexPages.StandardController controller) {
    }
    public void init() {
        String Maintenance_ContractID = ApexPages.currentPage().getParameters().get('id');
        List<Maintenance_Contract__c> MCList = [select
		                                        id,
		                                        Name,
		                                        Service_Contract_Staff__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,
		                                        MainLeaderManager__c,
		                                        Service_Contract_Staff__r.JingliApprovalManager__c,
		                                        User_Salesdepartment__c,
		                                        User_Salesdepartment_Sec__c,
		                                        Service_Contract_Staff__r.Salesdepartment__c,
		                                        Small_Repair_Time__c,
		                                        Big_Repair_Time__c,
		                                        Live_Technical_Support_Time__c,
		                                        Engineer_Name__c,
		                                        Phone_Number__c,
		                                        Tour_Check__c,
		                                        Inspection_Time__c,
		                                        Dedust__c,
		                                        Negotiate_Mean__c,
		                                        First_Party_Contract_Number__c,
		                                        Second_Party_Contract_Number__c,
		                                        Term_Of_Payment__c,
		                                        one_off_payment_agreed_period__c,
		                                        Contract_Amount__c,
		                                        Total_Contract_Amount_In_Words__c,
		                                        Service_Contract_Staff__r.Name,
		                                        Total_Payment_Time__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,
		                                        Estimation_Id__c,
		                                        SalesOfficeCode_selection__c,
		                                        Hospital__r.Name,
		                                        Management_Code__c,
		                                        Contract_Start_Date__c,
		                                        Contract_End_Date__c,
		                                        Total_Contract_Number__c,
		                                        Is_New_Contract__c
		                                        from
		                                        Maintenance_Contract__c
		                                        where
		                                        id = :Maintenance_ContractID];
		if (MCList.size() == 0) {
            throw new ControllerUtil.myException('维修合同不存在。');
        }
        if(MCList[0].Estimation_Id__c == null){
        	throw new ControllerUtil.myException('维修合同报价不存在。');
        }

        //巡回、点检和除尘为0是显示为'/'
        if(MCList[0].Tour_Check__c == null || MCList[0].Tour_Check__c.equals('0')){
            MCList[0].Tour_Check__c = ' / ';
        }
        if(MCList[0].Inspection_Time__c == null || MCList[0].Inspection_Time__c.equals('0')){
            MCList[0].Inspection_Time__c = ' / ';
        }
        if(MCList[0].Dedust__c == null || MCList[0].Dedust__c.equals('0')){
            MCList[0].Dedust__c = ' / ';
        }   

        list<Maintenance_Contract_Asset_Estimate__c> MCAs = [select Id from Maintenance_Contract_Asset_Estimate__c
                                                                       where Maintenance_Contract_Estimate__c = :MCList[0].Estimation_Id__c];
        this.targetMC = new MCInfo(MCList,MCAs.size());
        

       
    }


    public integer getStringLength() {
        integer StringLength = 0;
        if( targetMC.MC.Hospital__r.Name != null ){
            StringLength = targetMC.MC.Hospital__r.Name.length();
        }
        if(targetMC.BCompany !=null && targetMC.BCompany.length() > StringLength){
            StringLength = targetMC.BCompany.length();
        }
        return StringLength;
    }

    private static Map<String, String> companyApiMap = new Map<String, String> {
        '北京RC' => '奥林巴斯（北京）销售服务有限公司',
        '上海RC' => '奥林巴斯（北京）销售服务有限公司上海分公司',
        '广州RC' => '奥林巴斯（北京）销售服务有限公司广州分公司',
        '沈阳RC' => '奥林巴斯（北京）销售服务有限公司',
        //wangweipeng  2021/07/30   start
        '成都RC' => '奥林巴斯（北京）销售服务有限公司成都维修分公司',
        '西安RC' => '奥林巴斯（北京）销售服务有限公司西安维修分公司',
        '杭州RC' => '奥林巴斯（北京）销售服务有限公司杭州维修分公司'
        //wangweipeng  2021/07/30   start
    };

        /**
     * 印刷するデータを格納するオブジェクを定義する、親
     */
    public class MCInfo {
        public Maintenance_Contract__c MC {get; private set;}
        public String BCompany {get; private set;}
        public String HospitalName {get; private set;}
        public integer AssetSize {get; private set;}
        public String StartYear {get; private set;}
        public String StartMonth {get; private set;}
        public String StartDay {get; private set;}
        public String EndYear {get; private set;}
        public String EndMonth {get; private set;}
        public String EndDay {get; private set;}
        public String First_Payment_Year {get; private set;}
        public String First_Payment_Month {get; private set;}
        public String First_Payment_Day {get; private set;}
        public String Second_Payment_Year {get; private set;}
        public String Second_Payment_Month {get; private set;}
        public String Second_Payment_Day {get; private set;}
        public String Third_Payment_Year {get; private set;}
        public String Third_Payment_Month {get; private set;}
        public String Third_Payment_Day {get; private set;}
        public String Forth_Payment_Year {get; private set;}
        public String Forth_Payment_Month {get; private set;}
        public String Forth_Payment_Day {get; private set;}
        public String Fifth_Payment_Year {get; private set;}
        public String Fifth_Payment_Month {get; private set;}
        public String Fifth_Payment_Day {get; private set;}
        public String Sixth_Payment_Year {get; private set;}
        public String Sixth_Payment_Month {get; private set;}
        public String Sixth_Payment_Day {get; private set;}
        
        public integer Total_Payment_Time {get; private set;}

        public String TermZero {get; private set;}
        public String TermOne_One {get; private set;}
        public String TermOne_Two {get; private set;}
        public String TermTwo_One {get; private set;}
        public String TermTwo_Two {get; private set;}
        public String TermFour_One {get; private set;}
        public String TermFour_Four {get; private set;}
        public String TermFour_Five_One {get; private set;}
        public String TermFour_Five_Two {get; private set;}
        public String TermFour_Five_Three {get; private set;}
        public String TermFour_Six_One {get; private set;}
        public String TermFour_Six_Two {get; private set;}
        public String TermFour_Six_Three {get; private set;}
        public String TermFour_Seven_One {get; private set;}
        public String TermFour_Seven_Two {get; private set;}
        public String TermFour_Eight {get; private set;}
        public String TermFour_Nine {get; private set;}

        // HWAG-BLWFGC  追加第八条，原八九条顺延 update by vivek start
        public String TermEightAppend_Title {get; private set;}
        public String TermEightAppend_One {get; private set;}
        public String TermEightAppend_Two {get; private set;}
        public String TermEightAppend_Three {get; private set;}
        public String TermEightAppend_Three1 {get; private set;}
        public String TermEightAppend_Four {get; private set;}
        public String TermEightAppend_Five {get; private set;}
        public String TermEightAppend_Six {get; private set;}
        public String TermEightAppend_Seven {get; private set;}
        // HWAG-BLWFGC  追加第八条，原八九条顺延 update by vivek end

        public String Payment {get; private set;}
        public String Payment_One {get; private set;}
        public String Payment_Two {get; private set;}
        public String Payment_Three {get; private set;}
        public String Payment_Four {get; private set;}
        
        public String FirstParty {get; private set;}
        public String FirstParty_One {get; private set;}
        public String FirstParty_Two {get; private set;}
        public String FirstParty_Three {get; private set;}
        public String FirstParty_Four {get; private set;}
        public String FirstParty_Four_One {get; private set;}
        public String FirstParty_Five {get; private set;}
        public String FirstParty_Six {get; private set;}
        public String FirstParty_Six_One {get; private set;}
        public String FirstParty_Seven {get; private set;}
        public String FirstParty_Eight {get; private set;}
        public String FirstParty_Nine {get; private set;}
        public String SecondParty {get; private set;}
        
        public String Negotiate {get; private set;}
        public String Negotiate1 {get; private set;}
        public String Negotiate_One {get; private set;}
        public String ContractCopies {get; private set;}
        public String ContractNumber {get; private set;}
        public String ExceptionTerm {get; private set;}
        public String ContractExtension {get; private set;}


        public String Payment_Plan_Sum_First {get; private set;}
        public String Payment_Plan_Sum_Second {get; private set;}
        public String Payment_Plan_Sum_Third {get; private set;}
        public String Payment_Plan_Sum_Forth {get; private set;}
        public String Payment_Plan_Sum_Fifth {get; private set;}
        public String Payment_Plan_Sum_Sixth {get; private set;}
        public String Contract_Amount {get; private set;}

        private String returnNewStr(String hospitalName){
            String hospitalNames = '';
            if(hospitalName.contains('/')){
                Integer index = hospitalName.lastIndexOf('/');
                hospitalNames = hospitalName.substring(0,index)+'<br/>'+hospitalName.substring(index+1);
            }else{
                if(hospitalName.length()>22){
                    hospitalNames = hospitalName.substring(0,22)+'<br/>'+hospitalName.substring(22);
                }else{
                    hospitalNames = hospitalName;
                }
                
            }
            return hospitalNames;
        }


        private void setDate(Date StartDate, Date EndDate, Date FirstPaymentDate,
        	Date SecondPaymentDate,Date ThirdPaymentDate,
        	Date ForthPaymentDate,Date FifthPaymentDate,
        	Date SixthPaymentDate){
            if( StartDate != null ){
                this.StartYear = String.valueOf(StartDate.year());
                this.StartMonth =  String.valueOf(StartDate.month());
                this.StartDay =  String.valueOf(StartDate.day());

            }else{
                this.StartYear = ' / ';
                this.StartMonth =  ' / ';
                this.StartDay =  ' / ';
            }
            if( EndDate != null ){
                this.EndYear = String.valueOf(EndDate.year());
                this.EndMonth =  String.valueOf(EndDate.month());
                this.EndDay =  String.valueOf(EndDate.day());
            }else{
                this.EndYear = ' / ';
                this.EndMonth =  ' / ';
                this.EndDay =  ' / ';
            }
            if( FirstPaymentDate != null ){
                this.First_Payment_Year = String.valueOf(FirstPaymentDate.year());
                this.First_Payment_Month =  String.valueOf(FirstPaymentDate.month());
                this.First_Payment_Day =  String.valueOf(FirstPaymentDate.day());
            }else{
                this.First_Payment_Year = ' / ';
                this.First_Payment_Month =  ' / ';
                this.First_Payment_Day =  ' / ';

            }
            if( SecondPaymentDate != null ){
                this.Second_Payment_Year = String.valueOf(SecondPaymentDate.year());
                this.Second_Payment_Month =  String.valueOf(SecondPaymentDate.month());
                this.Second_Payment_Day =  String.valueOf(SecondPaymentDate.day());
            }else{
                this.Second_Payment_Year = ' / ';
                this.Second_Payment_Month =  ' / ';
                this.Second_Payment_Day =  ' / ';
            }
            if( ThirdPaymentDate != null ){
                this.Third_Payment_Year = String.valueOf(ThirdPaymentDate.year());
                this.Third_Payment_Month =  String.valueOf(ThirdPaymentDate.month());
                this.Third_Payment_Day =  String.valueOf(ThirdPaymentDate.day());
            }else{
                this.Third_Payment_Year = ' / ';
                this.Third_Payment_Month =  ' / ';
                this.Third_Payment_Day =  ' / ';
            }
            if( ForthPaymentDate != null ){
                this.Forth_Payment_Year = String.valueOf(ForthPaymentDate.year());
                this.Forth_Payment_Month =  String.valueOf(ForthPaymentDate.month());
                this.Forth_Payment_Day =  String.valueOf(ForthPaymentDate.day());
            }else{
                this.Forth_Payment_Year = ' / ';
                this.Forth_Payment_Month =  ' / ';
                this.Forth_Payment_Day =  ' / ';
            }
            if( FifthPaymentDate != null ){
                this.Fifth_Payment_Year = String.valueOf(FifthPaymentDate.year());
                this.Fifth_Payment_Month =  String.valueOf(FifthPaymentDate.month());
                this.Fifth_Payment_Day =  String.valueOf(FifthPaymentDate.day());
            }else{
                this.Fifth_Payment_Year = ' / ';
                this.Fifth_Payment_Month =  ' / ';
                this.Fifth_Payment_Day =  ' / ';
            }
            if( SixthPaymentDate != null ){
                this.Sixth_Payment_Year = String.valueOf(SixthPaymentDate.year());
                this.Sixth_Payment_Month =  String.valueOf(SixthPaymentDate.month());
                this.Sixth_Payment_Day =  String.valueOf(SixthPaymentDate.day());
            }else{
                this.Sixth_Payment_Year = ' / ';
                this.Sixth_Payment_Month =  ' / ';
                this.Sixth_Payment_Day =  ' / ';
            }


        }
        
        public MCInfo(List<Maintenance_Contract__c> MCList,integer AssetSize) {
            if (MCList.size() > 0) {
                this.MC = MCList[0];
                this.BCompany = companyApiMap.get(MC.SalesOfficeCode_selection__c);
                this.AssetSize = AssetSize;
                String Hospital = MC.Hospital__r.Name;
                this.HospitalName = returnNewStr(Hospital);
                if('一次性付款'.equals(MC.Term_Of_Payment__c)){
                    this.Payment_One = '本合同正式签订之日起';
                    this.Payment_two = '日内，甲方向乙方支付全部合同款人民币';
                    this.Payment_Four = '元 ( 大写';
                }else{
            		this.Payment_Three = '日前，甲方向乙方支付人民币';
            		this.Payment_Four = '元 ( 大写';
                }
                setDate(MC.Contract_Start_Date__c,MC.Contract_End_Date__c, 
                	MC.Payment_Plan_Date_First__c, MC.Payment_Plan_Date_Second__c,
                	MC.Payment_Plan_Date_Third__c, MC.Payment_Plan_Date_Forth__c, 
                	MC.Payment_Plan_Date_Fifth__c, MC.Payment_Plan_Date_Sixth__c);
                if(MC.Total_Payment_Time__c!=null){
                    this.Total_Payment_Time = integer.valueof(MC.Total_Payment_Time__c);
                }
                
                list<String> args = new String[]{'0','number','###,###,###,###,##0'};
                if(MC.Payment_Plan_Sum_First__c != null ){
                    this.Payment_Plan_Sum_First     =  String.format(MC.Payment_Plan_Sum_First__c.format(), args);
                }
                if(MC.Payment_Plan_Sum_Second__c != null ){
                    this.Payment_Plan_Sum_Second    =  String.format(MC.Payment_Plan_Sum_Second__c.format(), args);
                }
                if(MC.Payment_Plan_Sum_Third__c != null ){
                    this.Payment_Plan_Sum_Third     =  String.format(MC.Payment_Plan_Sum_Third__c.format(), args);
                }
                if(MC.Payment_Plan_Sum_Forth__c != null ){
                    this.Payment_Plan_Sum_Forth     =  String.format(MC.Payment_Plan_Sum_Forth__c.format(), args);
                }
                if(MC.Payment_Plan_Sum_Fifth__c != null ){
                    this.Payment_Plan_Sum_Fifth     =  String.format(MC.Payment_Plan_Sum_Fifth__c.format(), args);
                }
                if(MC.Payment_Plan_Sum_Sixth__c != null ){
                    this.Payment_Plan_Sum_Sixth     =  String.format(MC.Payment_Plan_Sum_Sixth__c.format(), args);
                }
                if(MC.Contract_Amount__c != null ){
                    this.Contract_Amount            =  String.format(MC.Contract_Amount__c.format(), args);
                }

                this.TermZero = '甲、乙双方经过共同协商，就甲方所购买的奥林巴斯系列内镜产品及周边仪器设备的维修事宜达成如下协议（以下简称“本合同”）：';
                this.TermOne_One = '乙方为甲方';
                this.TermOne_Two = '件设备，提供有偿维修服务，维修服务合同设备清单参见附';
            	this.TermTwo_One = '二、合同总价：';
            	this.TermTwo_Two = '(人民币:';
            	this.TermFour_One = '附件中包括的产品，如果在自然损耗以及按照乙方要求的清洗消毒方法的情况下发生故障，乙方进行无条件维修；属于甲方故意人为损坏的，将不在本合同维修范围内，甲方需负担因此而发生的维修费用。';
            	this.TermFour_Four = '如果甲方属于本合同附件内所列的设备发生故障需要大维修时，甲方享有优先获得备用品的权利，乙方尽力保证及时向甲方提供备用品。';
            	this.TermFour_Five_One = '乙方指定工程师';
            	this.TermFour_Five_Two = '专门负责甲方的内镜维修和清洗保养培训工作。如指定工程师有变更，乙方另行通知甲方相关变更情况。周一至周五和周六上午一旦甲方的本合同附件中所列的内镜出现问题，工程师保证在';
            	this.TermFour_Five_Three = '小时内提供现场技术支持，并提供每周24小时×7天的电话支持服务（工程师手机号码：';
            	this.TermFour_Six_One = '乙方指定工程师为甲方提供 1 年';
            	this.TermFour_Six_Two = '次巡回，1年';
            	this.TermFour_Six_Three = '次点检，并提交检查报告给医院备档。';
            	this.TermFour_Seven_One = '乙方为甲方提供 1年';
            	this.TermFour_Seven_Two = '次的周边仪器免费除尘清洁服务（周边仪器是指本合同维修范围清单内的周边仪器）。';
            	this.TermFour_Eight = '在本合同期内，乙方可根据甲方的需求，对附件内所列设备的使用、清洗和保养方法为甲方提供院内培训。';
            	this.TermFour_Nine = '在本合同期内，乙方结合甲方的实际情况，判断是否要向合同参保设备提供故障预防工具。如乙方判断需要时，由乙方直接向甲方提供。';
            	this.FirstParty_One = '甲方设备维修范围只适用于本合同附件内所列设备。周边仪器只对附件内所列之主机，光源，超声主机、内镜清洗消毒机和监视器进行维修。其他与周边仪器相关的不可维修的设备（如电缆线、超声探头、水瓶、键盘、脚踏等)和耗品（如清洗用具、治疗附件、光源灯泡等）不属于维修范围。';
            	this.FirstParty_Two = '甲方应爱护本合同内所包含之设备，积极做好内镜的日常维护和保养工作，确保内镜始终处于良好的工作状态。';
            	this.FirstParty_Three = '无论在任何情况下，甲方均不得将本合同附件内所列的维修设备送至非奥林巴斯厂家进行维修。否则，乙方对经过非奥林巴斯厂家维修的设备进行维修时，将按正常标准向甲方收取维修费用。';
            	this.FirstParty_Four = '无论在任何情况下，甲方均不得自行拆卸本合同附件内所列的维修设';
            	this.FirstParty_Four_One = '备。否则，乙方对甲方自行拆卸的设备进行维修时，将按正常标准向甲方收取维修费用。';
                this.FirstParty_Five = '甲方不可以将附件所列维修设备借给其他医院或机构使用，若由此造成的故障，乙方将向甲方收取正常的维修费，而不视为本合同费用。';
            	this.FirstParty_Six = '甲方如果因为使用劣质或不匹配的非奥林巴斯内镜附件导致的损坏故';
                this.FirstParty_Six_One = '障，乙方将向甲方收取正常的维修费，而不视为本合同费用。';
                this.FirstParty_Seven = '甲方应该主动积极配合乙方管理设备，降低因为人为原因造成的损坏。';
            	this.FirstParty_Eight = '甲方未经乙方事先书面同意，不得将本合同当事人的地位以及依本合同产生的任何权利义务转让给第三人或提供担保。';
            	this.FirstParty_Nine = '甲方应保守乙方的商业机密，不得向任何第三方泄露任何或全部包括乙方的销售、市场、技术、维修以及任何其它被告知或从另一方取得乙方的信息；无论此信息在本合同中是否标明或界定为机密。同时不得将该信息用于非本合同所述的目的。本条规定的保密期限在本合同期满或终止后继续有效。';
            	
            	this.Negotiate_One = '任何一方有权向设在北京的中国国际经济贸易仲裁委员会申请仲裁，仲裁的裁决是终局性的，对双方均有约束力。';
            	this.ContractCopies = '份，自双方盖章之日起生效。如生效日期与本协议第三条约定的委托期限起始日期不一致时，双方同意以两者中较晚日期作为本合同的生效时间，本合同生效之前发生的修理不计入本合同，由甲乙双方另行协商解决。';
            	
                this.SecondParty 			= '四、乙方责任范围';
            	this.Payment 				= '五、付款方式';
            	this.FirstParty 			= '六、甲方责任范围';
            	this.ContractExtension   	= '七、本合同期满后，甲、乙双方对续约事宜另行协商。';

                // HWAG-BLWFGC  追加第八条，原八九条顺延 update by vivek start
                this.TermEightAppend_Title = '合同变更、修改、解除和终止';
                this.TermEightAppend_One = '经甲乙双方协商同意，可对本合同进行修改和补充，修改及补充的内容经双方签字或盖章后作为本合同的有效组成部分，与本合同具有同等法律效力。若补充协议内容与本合同有冲突以补充协议为准。';
                this.TermEightAppend_Two = '本合同任何一方有下列情形之一，对方可不经催告解除本合同：';
                this.TermEightAppend_Three = '方被采取查封、扣押、冻结等，或被申请开始进行拍卖、破产、清算、';
                this.TermEightAppend_Three1 = '合并、分立；';
                this.TermEightAppend_Four = '一方迟延履行本合同主要义务，经催告后在一方规定的合理期限内仍未履行；';
                this.TermEightAppend_Five = '一方有其他违约或违法行为致使本合同目的不能实现的。';
                this.TermEightAppend_Six = '由于不可抗力的原因，导致本合同部分不能履行、不能履行或延期履行';
                this.TermEightAppend_Seven = '时，遇有不可抗力事件的一方，应立即将事故情况通知对方，并提供部分不能履行、不能履行或延期履行的理由和所有相关的文件资料。';
                // HWAG-BLWFGC  追加第八条，原八九条顺延 update by vivek end

            	this.Negotiate 				= 
            	'本合同的解释与执行适用中华人民共和国法律，在履行本合同过程中出现的争议双方应友好协商解决；协商不成的，可选择以下第';
                this.Negotiate1             ='种方式解决：';
            	this.ContractNumber			= '本协议一式';
            	this.ExceptionTerm 			= '免责条款：若部分产品由于特殊零件订购、运输、停产等原因暂停或停止修理，则乙方只修理可维修部分。';
            }
            	
        }

    }

}