public without sharing class MoreMaintenanceContractController {
    public Maintenance_Contract__c mc {get; set;}
    public List<RepairInfo> repairL  {get; set;}
    public Integer proportion {get; set;}
    public Integer GoFlag{get; set;}
    //当前的维修合同id
    public String Id {get; set;}
    public String RecordTypeName {get;set;}
    public Boolean isVM {get;set;} //20210106 zh LJPH-BWJ8PM 制作报告书按钮
    public Integer repairLCount{ 
        get {
            return repairL == null ? 0 : repairL.size();
        }
    }
    public Boolean HATextEdit {get;set;}
    public MoreMaintenanceContractController() {
        Id = ApexPages.currentPage().getParameters().get('Id');
        RecordTypeName = ApexPages.currentPage().getParameters().get('RecordTypeName');
        repairL = new List<RepairInfo>();
        //20210106 zh LJPH-BWJ8PM 制作报告书按钮 start
        isVM = false;
        if ('VM_Contract'.equals(RecordTypeName)) {
            isVM = true;
        }
        //20210106 zh LJPH-BWJ8PM 制作报告书按钮 end
    }
    public void init(){
        mc = [Select Id,Name,HospitalAmountText__c,hospitalQuotationTextSum__c,Management_Code__c,Status__c,RecordType.Name,Contract_Conclusion_Date__c,Contract_Start_Date__c from Maintenance_Contract__c where id =:Id];
        /*[经销商对医院签约价格]当该项有值时，则在合同开始日的3个月时点，信息保存锁死，无法修改。
        合同开始日指：
        1）合同状态为询价中的，不锁定
        2）合同状态为【合同中】的合同：合同日和合同开始日取晚的日期，加3个月作为期限值。
        */
        HATextEdit = false;
        if(mc.Contract_Conclusion_Date__c != null || mc.Contract_Start_Date__c != null){
            Date MaxDate = null; 
            if(mc.Contract_Conclusion_Date__c == null){
                MaxDate = mc.Contract_Start_Date__c.addMonths(3);
            }else if(mc.Contract_Start_Date__c == null){
                MaxDate = mc.Contract_Conclusion_Date__c.addMonths(3);
            }else{
                Integer daysBetween = mc.Contract_Conclusion_Date__c.daysBetween(mc.Contract_Start_Date__c);
                //mc.Contract_Start_Date__c-mc.Contract_Conclusion_Date__c
                if(daysBetween >= 0){
                    MaxDate = mc.Contract_Start_Date__c.addMonths(3);
                }else{
                    MaxDate = mc.Contract_Conclusion_Date__c.addMonths(3);
                }  
            }
            if(mc.Status__c.equals('契約') && MaxDate != null && Date.today() <= MaxDate){
                HATextEdit = true;
            }
        }
        if(mc.HospitalAmountText__c == NULL || mc.Status__c.equals('引合中')){
            HATextEdit = true;
        }
        List<Repair__c> repairList = new List<Repair__c>();
        if(RecordTypeName !=null&&RecordTypeName.equals('NewMaintenance_Contract')){
        	repairList = [Select Id, Name,Status1__c,Delivered_Product__c,Delivered_Product__r.Name,SerialNumber__c,Repair_List_Price_formula__c,Repair_Rank__c,
                    hospitalQuotationText__c from Repair__c where  Maintenance_Contract__c =:Id and Status1__c !='0.取消' and Status1__c != '0.删除' order by hospitalQuotationText__c];
        }else if(RecordTypeName !=null&&RecordTypeName.equals('VM_Contract')){
        	repairList = [Select Id, Name,Status1__c,Delivered_Product__c,Delivered_Product__r.Name,SerialNumber__c,Repair_List_Price_formula__c,Repair_Rank__c,
                    hospitalQuotationText__c from Repair__c where  VM_Maintenance_Contract__c =:Id and Status1__c !='0.取消' and Status1__c != '0.删除' order by hospitalQuotationText__c ];
        }
        Map<String, RepairInfo> MidMap = new Map<String, RepairInfo>();
        for (Integer i = 0; i < repairList.size(); i++) {
            MidMap.put(repairList[i].Id, new RepairInfo(repairList[i]));
        }
        for (RepairInfo rr : MidMap.values()) {
            repairL.add(rr);
        }
        for(RepairInfo rl:repairL){
	        //if(rl.rr.hospitalQuotationText__c ==null||rl.rr.hospitalQuotationText__c ==0.00){
            if(rl.rr.hospitalQuotationText__c ==null){
	            rl.edit = true;
	        }
	    }
    }
    public void RefreshPrice(){
        if(proportion !=null){
            //合同期间修理金额（上传）
            Decimal temphospitalQuotationTextSum = 0.00;
            for(RepairInfo rl:repairL){
            	if(rl.edit){
                    rl.rr.hospitalQuotationText__c = rl.rr.Repair_List_Price_formula__c*proportion/100;
                }
                temphospitalQuotationTextSum +=rl.rr.hospitalQuotationText__c;
            }
            mc.hospitalQuotationTextSum__c = temphospitalQuotationTextSum;
        }else{
        	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  '请输入经销商对医院修理价格调整比例'));
        }
    }
    public void HQSum(){
        Decimal temphospitalQuotationTextSum = 0.00;
        for(RepairInfo rl:repairL){
            if(rl.rr.hospitalQuotationText__c != null){
                temphospitalQuotationTextSum +=rl.rr.hospitalQuotationText__c;
            }  
        }
        mc.hospitalQuotationTextSum__c = temphospitalQuotationTextSum;
        proportion = String.isBlank(proportion.format())?null:proportion;
    }
    
    public PageReference Save(){
        //更新维修合同和修理
        Maintenance_Contract__c mcNew = new Maintenance_Contract__c();
        List<Repair__c> repairLNew = new List<Repair__c>();
        Savepoint sp = Database.setSavepoint();
        try {
            /*Boolean isHaveNull = false;
            for(RepairInfo rl:repairL){
            	if(rl.rr.hospitalQuotationText__c==null && rl.edit){
            		isHaveNull = true;
            	}
            }
            if(isHaveNull){
            	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  '修理价格存在空项，请确认是否保存'));
            	//return null;
            }*/
            //else{
            	mcNew.Id = Id;
	            mcNew.HospitalAmountText__c = mc.HospitalAmountText__c;	            
	            Decimal temphospitalQuotationTextSum = 0.00;
	            for(RepairInfo rl:repairL){
	            	Repair__c rUpdate = new Repair__c();
	                rUpdate.Id = rl.rr.Id;
	                rUpdate.hospitalQuotationText__c = rl.rr.hospitalQuotationText__c;
	                repairLNew.add(rUpdate);
                    if(rl.rr.hospitalQuotationText__c !=null){
                         temphospitalQuotationTextSum +=rl.rr.hospitalQuotationText__c;
                    } 
	            }
	            mcNew.hospitalQuotationTextSum__c = temphospitalQuotationTextSum;
	            update mcNew;
	            update repairLNew;
	            return GoURL();
            //}
            
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
            Database.rollback(sp);
            return null;
        }   
    }
    public PageReference GoURL() {
        PageReference ref;
        if(GoFlag==0){
            ref = new Pagereference('/apex/MoreMaintenanceContract?Id=' +Id+'&RecordTypeName='+RecordTypeName);
        }else if(GoFlag==1){
            ref = new Pagereference('http://powerbi.olympus.com.cn/Home/Login');
        }
        ref.setRedirect(true);
        return ref;
    }
    class RepairInfo{
        public Boolean edit { get; set; }
        public Repair__c rr {get; set;}
        public RepairInfo(Repair__c e) {
            rr = e;
            edit = false;
        }
    }  
}