public with sharing class RentalEquipmentCreateController{
	public String campaignId {get;set;}
	public String opdId {get;set;}
	public List<Rental_Equipment__c> prods ;
	// public Map<String,String> planProducts {get; set;}
	public List<Plan_Rental_Equipment__c> campaignEquipments;
	public List<Plan_Rental_Equipment__c> opdEquipments;
	public String eventRecordType;
	public String campaignRecordType;
	public String otherRecordType;
	public Boolean isEdit {get;set;}
	public OPDPlan__c targetOPD;
	public Map<String,String> planProductMap{get;set;}
	public Campaign targetCampaign; 

	public RentalEquipmentCreateController(){
		this.prods = [SELECT Name FROM Rental_Equipment__c ORDER BY Serial_Number__c ASC NULLS FIRST];
		this.campaignEquipments = new List<Plan_Rental_Equipment__c>();
		this.opdEquipments = new List<Plan_Rental_Equipment__c>();
		this.planProductMap = new Map<String,String>();
		List<RecordType> recordTypes = [SELECT Id,Name FROM RecordType WHERE SobjectType = 'Plan_Rental_Equipment__c'];
		for(RecordType re : recordTypes){
			if ('事件'.equals(re.Name)) {
			    eventRecordType = re.Id;
			}
			if ('学会'.equals(re.Name)) {
			    campaignRecordType = re.Id;
			}
			if ('其他'.equals(re.Name)) {
			    otherRecordType = re.Id;
			}
		}
	}

	public List<Rental_Equipment__c> getProds() {
		// Map<String,String> planProducts = new Map<String,String>();
		System.debug('-----camp---'+campaignId);
		if (campaignId != null && campaignId != '') {
			UserRecordAccess useraccess = [SELECT recordId,HasEditAccess FROM UserRecordAccess WHERE userId = :UserInfo.getUserId() AND recordId = :campaignId];
			this.isEdit = useraccess.HasEditAccess;
			// this.isEdit = Campaign.sObjectType.getDescribe().isUpdateable();
			this.targetCampaign = [SELECT Id,LoadNum__c,Status,Is_LendProduct__c FROM Campaign WHERE Id = :campaignId]; 
			if ((!('草案中').equals(targetCampaign.Status)) && isEdit) {
			    this.isEdit = false;
			}
			// else if (targetCampaign.Is_LendProduct__c == '否' && isEdit) {
			//     this.isEdit = false;
			// }
			System.debug('-----campisEdit---'+isEdit);
			this.campaignEquipments = [SELECT Rental_Equipment__c,Rental_Quantity__c FROM Plan_Rental_Equipment__c WHERE Campaign__c = :campaignId];
		    this.planProductMap = campaignMap();

		}
		if (opdId != null && opdId != '') {
			UserRecordAccess useraccess = [SELECT recordId,HasEditAccess FROM UserRecordAccess WHERE userId = :UserInfo.getUserId() AND recordId = :opdId];
			this.isEdit = useraccess.HasEditAccess;
			this.targetOPD = [SELECT Id,PlanProdDetail__c,Campaign__c,OPDType__c,Status__c FROM OPDPlan__c WHERE Id = :opdId]; 
			// ***SWAG-BZCAHL***XHL***20210331***START*********
			// 因OPD计划再申请生成的OPD计划 状态是 草案中 无法创建 计划出借备品 
			// 同时 从事件与学会创建的OPD计划 不会是草案中，因此将其注掉，不影响其它逻辑
			// if ((('事件').equals(targetOPD.OPDType__c) || ('学会').equals(targetOPD.OPDType__c)) && isEdit) {
			// 	if (!targetOPD.Reapply__c) {
			// 		this.isEdit = false;
			// 	}
			    
			// }
			// ***SWAG-BZCAHL***XHL***20210331***END*********
			if ((!('草案中').equals(targetOPD.Status__c)) && isEdit) {
			    this.isEdit = false;
			}
			this.opdEquipments = [SELECT Rental_Equipment__c,Rental_Quantity__c FROM Plan_Rental_Equipment__c WHERE OPD_Plan__c = :opdId];
		    this.planProductMap = eventMap();
		}
		
		return this.prods;
	}

	public Map<String,String> campaignMap(){
		System.debug('-----campaignMap111---'+campaignId);
		// campaignEquipments = new List<Plan_Rental_Equipment__c>();
		
		Map<String,String> tempPlanProducts = new Map<String,String>();
		// if (campaignId != null && campaignId != '') {
		//     campaignEquipments = [SELECT Rental_Equipment__c,Rental_Quantity__c FROM Plan_Rental_Equipment__c WHERE Campaign__c = :campaignId];
		// }
		Map<String,String> tempMap = new Map<String,String>();
		if (campaignEquipments.size() > 0) {
			for(Plan_Rental_Equipment__c rentalEquipment : campaignEquipments){
				if (String.isBlank(rentalEquipment.Rental_Quantity__c)) {
				    tempMap.put(rentalEquipment.Rental_Equipment__c, '');
				}else{
					tempMap.put(rentalEquipment.Rental_Equipment__c, rentalEquipment.Rental_Quantity__c);
				}
	    	}
		}
		if (prods.size() > 0 ) {
			if (campaignEquipments.size() > 0) {
			    for(Rental_Equipment__c prod : prods){
					if (tempMap.containsKey(prod.Name)) {
						if (String.isBlank(tempMap.get(prod.Name)) || tempMap.get(prod.Name) == '0') {
						    tempPlanProducts.put(prod.Name, '');
						}else{
							tempPlanProducts.put(prod.Name, tempMap.get(prod.Name));
						}
					    
					}else{
						tempPlanProducts.put(prod.Name, '');
					}
				}
			}else{
				for(Rental_Equipment__c prod : prods){
					tempPlanProducts.put(prod.Name, '');
				}
			}
			
		}
		return tempPlanProducts;
	}

	public Map<String,String> eventMap(){
		// opdEquipments = new List<Plan_Rental_Equipment__c>();
		Map<String,String> tempPlanProducts = new Map<String,String>();
		// if (opdId != null && opdId != '') {
		//     opdEquipments = [SELECT Rental_Equipment__c,Rental_Quantity__c FROM Plan_Rental_Equipment__c WHERE OPD_Plan__c = :opdId];
		// }
		Map<String,String> tempMap = new Map<String,String>();
		if (opdEquipments.size() > 0) {
			for(Plan_Rental_Equipment__c rentalEquipment : opdEquipments){
				if (String.isBlank(rentalEquipment.Rental_Quantity__c)) {
				    tempMap.put(rentalEquipment.Rental_Equipment__c, '');
				}else{
					tempMap.put(rentalEquipment.Rental_Equipment__c, rentalEquipment.Rental_Quantity__c);
				}
	    	}
		}
		if (prods.size() > 0 ) {
			if (opdEquipments.size() > 0) {
			    for(Rental_Equipment__c prod : prods){
					if (tempMap.containsKey(prod.Name)) {
						if (String.isBlank(tempMap.get(prod.Name)) || tempMap.get(prod.Name) == '0') {
						    tempPlanProducts.put(prod.Name, '');
						}else{
							tempPlanProducts.put(prod.Name, tempMap.get(prod.Name));
						}
					}else{
						tempPlanProducts.put(prod.Name, '');
					}
				}
			}else{
				for(Rental_Equipment__c prod : prods){
					tempPlanProducts.put(prod.Name, '');
				}
			}
			
		}
		return tempPlanProducts;
	}

	

	public void save(){
		List<Plan_Rental_Equipment__c> ineuqipments = new List<Plan_Rental_Equipment__c>();
		List<Plan_Rental_Equipment__c> upeuqipments = new List<Plan_Rental_Equipment__c>();
		Map<String,Plan_Rental_Equipment__c> camTemp = new Map<String,Plan_Rental_Equipment__c>();
		Map<String,Plan_Rental_Equipment__c> eveTemp = new Map<String,Plan_Rental_Equipment__c>();	
		// OPDPlan__c targetOPD = new OPDPlan__c();
		Event targetEvent = new Event();
	    for(Plan_Rental_Equipment__c plan : campaignEquipments){
	    	if (String.isBlank(plan.Rental_Quantity__c)) {
	    	    plan.Rental_Quantity__c = '0';
	    	}
	    	camTemp.put(plan.Rental_Equipment__c, plan);
	    }			
	    for(Plan_Rental_Equipment__c plan : opdEquipments){
	    	if (String.isBlank(plan.Rental_Quantity__c)) {
	    	    plan.Rental_Quantity__c = '0';
	    	}
	    	eveTemp.put(plan.Rental_Equipment__c, plan);
	    }
		if (campaignId != null && campaignId != '') {
			if (campaignEquipments.size() > 0) { //学会已经有出借备品
				for(Rental_Equipment__c prod : prods){
					if (camTemp.containsKey(prod.Name)) {
					    if (!camTemp.get(prod.Name).Rental_Quantity__c.equals(planProductMap.get(prod.Name))) { //出借备品数量有变化
					    	if (String.isBlank(planProductMap.get(prod.Name))) {
					    	    camTemp.get(prod.Name).Rental_Quantity__c = '0';
					    	}else {
					    		camTemp.get(prod.Name).Rental_Quantity__c = planProductMap.get(prod.Name);
					    	}
					        upeuqipments.add(camTemp.get(prod.Name));
					    }
					}else{ //新增的出借备品数量
						if (!'0'.equals(planProductMap.get(prod.Name)) && String.isNotBlank(planProductMap.get(prod.Name))) {
							Plan_Rental_Equipment__c planeuqipment = new Plan_Rental_Equipment__c();
						    planeuqipment.Campaign__c = campaignId;
						    planeuqipment.RecordTypeId = campaignRecordType;
						    planeuqipment.Name = prod.Name;
						    planeuqipment.Rental_Equipment__c = prod.Name;
						    planeuqipment.Rental_Quantity__c = planProductMap.get(prod.Name);
						    ineuqipments.add(planeuqipment);
						    campaignEquipments.add(planeuqipment);
						}
					}
				}
			}else{ //学会没有出借备品，需要新增
				for(Rental_Equipment__c prod : prods){
					if (!'0'.equals(planProductMap.get(prod.Name)) && String.isNotBlank(planProductMap.get(prod.Name))) {
					    Plan_Rental_Equipment__c planeuqipment = new Plan_Rental_Equipment__c();
					    planeuqipment.Campaign__c = campaignId;
					    planeuqipment.RecordTypeId = campaignRecordType;
					    planeuqipment.Name = prod.Name;
					    planeuqipment.Rental_Equipment__c = prod.Name;
					    planeuqipment.Rental_Quantity__c = planProductMap.get(prod.Name);
					    ineuqipments.add(planeuqipment);
					    campaignEquipments.add(planeuqipment);
					}
				}
			}
		    
		}
		else if (opdId != null && opdId != '') {
			// targetOPD = [SELECT Id,PlanProdDetail__c,Campaign__c FROM OPDPlan__c WHERE Id = :opdId]; 
			
			if (targetOPD.OPDType__c == '事件') {
				List<Event> getEventList = new List<Event>();
			    getEventList = [SELECT Id,PlanProdDetail__c FROM Event WHERE Opd_Plan__c = :opdId];
			    if (getEventList.size() > 0) {
			    	targetEvent = getEventList[0];
			    }
			}
			if (opdEquipments.size() > 0) { //OPD已经有出借备品
				for(Rental_Equipment__c prod : prods){
					if (eveTemp.containsKey(prod.Name)) {
					    if (!eveTemp.get(prod.Name).Rental_Quantity__c.equals(planProductMap.get(prod.Name))) { //出借备品数量有变化
					    	if (String.isBlank(planProductMap.get(prod.Name))) {
					    	    eveTemp.get(prod.Name).Rental_Quantity__c = '0';
					    	}else{
					    		eveTemp.get(prod.Name).Rental_Quantity__c = planProductMap.get(prod.Name);
					    	}
					        upeuqipments.add(eveTemp.get(prod.Name));
					    }
					}else{ //新增的出借备品数量
						if (!'0'.equals(planProductMap.get(prod.Name)) && String.isNotBlank(planProductMap.get(prod.Name))) {
							Plan_Rental_Equipment__c planeuqipment = new Plan_Rental_Equipment__c();
						    planeuqipment.OPD_Plan__c = opdId;
						    planeuqipment.Name = prod.Name;
						    planeuqipment.Rental_Equipment__c = prod.Name;
						    planeuqipment.Rental_Quantity__c = planProductMap.get(prod.Name);
						    if (targetOPD.OPDType__c == '事件') {
						    	if (String.isNotBlank(targetEvent.Id)) {
						    		planeuqipment.Event_ID__c = targetEvent.Id;
						    		planeuqipment.RecordTypeId = eventRecordType;	
						    	} else {
						    		planeuqipment.RecordTypeId = otherRecordType;	
						    	}
						    	
						    }else if(targetOPD.OPDType__c == '学会') {
						    	planeuqipment.Campaign__c = targetOPD.Campaign__c;
						    	planeuqipment.RecordTypeId = campaignRecordType;
						    }else{
						    	planeuqipment.RecordTypeId = otherRecordType;
						    }
						    ineuqipments.add(planeuqipment);
						    opdEquipments.add(planeuqipment);
						}
					}
				}
			}else{ //OPD没有出借备品，需要新增
				for(Rental_Equipment__c prod : prods){
					if (!'0'.equals(planProductMap.get(prod.Name)) && String.isNotBlank(planProductMap.get(prod.Name))) {
					    Plan_Rental_Equipment__c planeuqipment = new Plan_Rental_Equipment__c();
					    planeuqipment.OPD_Plan__c = opdId;
					    planeuqipment.Name = prod.Name;
					    planeuqipment.Rental_Equipment__c = prod.Name;
					    planeuqipment.Rental_Quantity__c = planProductMap.get(prod.Name);
					    if (targetOPD.OPDType__c == '事件') {
					    	if (String.isNotBlank(targetEvent.Id)) {
						    	planeuqipment.Event_ID__c = targetEvent.Id;
						    	planeuqipment.RecordTypeId = eventRecordType;
						    } else {
						    	planeuqipment.RecordTypeId = otherRecordType;
						    }
					    }else if(targetOPD.OPDType__c == '学会') {
					    	planeuqipment.Campaign__c = targetOPD.Campaign__c;
					    	planeuqipment.RecordTypeId = campaignRecordType;
					    }else{
					    	planeuqipment.RecordTypeId = otherRecordType;
					    }
					    ineuqipments.add(planeuqipment);
					    opdEquipments.add(planeuqipment);
					}
				}
			}
		}
		System.debug('--ineuqipments--'+ineuqipments +' ----upeuqipments---'+upeuqipments);
		if (ineuqipments.size()>0) {
		    insert ineuqipments;
		}
		if (upeuqipments.size()>0) {
		    update upeuqipments;
		}
		if (campaignId != null && campaignId != '') {
			String quantity = '';
		    for(Plan_Rental_Equipment__c plan : campaignEquipments){
		    	if (String.isNotBlank(plan.Rental_Quantity__c) && plan.Rental_Quantity__c != '0') {
		    	    quantity += plan.Rental_Equipment__c	 + '*' + plan.Rental_Quantity__c + '; ';
		    	}
		    }
			targetCampaign.LoadNum__c = quantity;
			update targetCampaign;
		}
		else if(opdId != null && opdId != ''){
			String quantity = '';
			for(Plan_Rental_Equipment__c plan : opdEquipments){
				if (String.isNotBlank(plan.Rental_Quantity__c) && plan.Rental_Quantity__c != '0') {
				    quantity += plan.Rental_Equipment__c	 + '*' + plan.Rental_Quantity__c + '; ';
				}
			}
			// targetEvent.PlanProdDetail__c = quantity;
			// update targetEvent;
			targetOPD.PlanProdDetail__c = quantity;
			update targetOPD;
		}
		// return null;

	}
}