public with sharing class CampaignMeetingcostController {
	private String camId;
	public Boolean status { get; set; }
	public Boolean canseePage { get; set; }
	public Campaign camMain { get; set; }
	public Campaign_Cost__c CampaignCostMain  { get; set; }
	public String errorMessage { get; set; }
	public Boolean errorflg { get; set; }
	public CampaignMeetingcostController() {
		camId = ApexPages.currentPage().getParameters().get('id');
	}
	//SWAG-BBG8AK 添加批准列表 start
	public CampaignMeetingcostController(ApexPages.StandardController controller) {
		this();
	}
	//SWAG-BBG8AK 添加批准列表 end

	public void init() {
		canseePage = true;
		errorMessage = '';
		errorflg = false;
		status = false;
		String userid = UserInfo.getUserId();

		camMain =
		    [select id, CostStatus__c , OwnerId,
		     Meeting_Type__c, cost_Create_Date__c,
		     JingliApprovalManager__c, BuchangApprovalManager__c,
		     ZongjianApprovalManager__c, ZongjianApprovalManager_CancleShen__c,
		     Sponsor_Ones_Post__c
		     from Campaign
		     WHERE id = : camId];

		List<GroupMember> memberG = new List<GroupMember>();
		if (camMain != null && camMain.Sponsor_Ones_Post__c == '1.华北') {
			memberG = [SELECT Id FROM GroupMember WHERE Group.DeveloperName = 'X00_Meetingfee_Huabei' and UserorGroupId  = :userid];
		}
		List<Campaign_Cost__c> CampaignCostList =
		    [SELECT id, OwnerId
		     , totalMeetingCost__c
		     , medicalInsititute__c, medicalInsitituteCost__c
		     , nonMedicalInstitute__c, nonMedicalInstituteCost__c
		     , standFittingCompany__c, StandFittingComCost__c
		     , broadcastingCompany__c, broadcastingCompanyCost__c
		     , OtherSponsorCost__c, animalExperienceCost__c
		     , isStand__c, IsSatelliteMeeting__c, isTitle__c, IsDocLOGO__c, IsShowingEquipment__c
		     , IsAnimalExperience__c, isOther__c
		     , standArea__c, AnimalExperienceNumber__c
		     , satelliteMeetingHours__c, showingEquipmentNumber__c
		     , titleArea__c, otherWords__c
		     , DocLOGOcontent__c
		     , paymentFromCompany__c,  paymentFromCooperator__c, livingCost__c
		     , tansportCost__c, travelByAir__c, travelByTrain__c, travelByCar__c
		     , banquetCost__c, lunchCost__c, teaBreakCost__c, broadcastingCost__c
		     , publicityCost__c, sundriesCost__c , sundryContent__c, hostCompany__c
		     , hostCompanyCostAndTax__c
		     , Campaign__r.CostStatus__c, Campaign__r.cost_Create_Date__c // SWAG-BBG8AK 费用状态和会议费用创建日
		     FROM Campaign_Cost__c WHERE Campaign__c = : camId];



		if  (camMain == null || (memberG.size() == 0
		                         && camMain.OwnerId != userid
		                         && userid != camMain.JingliApprovalManager__c
		                         && userid != camMain.BuchangApprovalManager__c
		                         && userid != camMain.ZongjianApprovalManager__c
		                         && userid != camMain.ZongjianApprovalManager_CancleShen__c
		                         && UserInfo.getProfileId() != system.label.ProfileId_SystemAdmin)) {
			errorMessage = '您无权查看会议费用情况。';
			errorflg = true;
			canseePage = false;
			return;
		}
		if (CampaignCostList.size() > 0) {
			CampaignCostMain = CampaignCostList[0];
		} else {
			CampaignCostMain = new Campaign_Cost__c();
			CampaignCostMain.Campaign__c = camId;
			CampaignCostMain.OwnerId = camMain.OwnerId;
		}
		//system.debug('CampaignCostMain:' + CampaignCostMain.Campaign__r.CostStatus__c);
		if (camMain != null
		        && (
		            (camMain.OwnerId == userid
		             && (camMain.CostStatus__c == null ||
		                 camMain.CostStatus__c == '费用已填写')
		            )
		            || UserInfo.getProfileId() == system.label.ProfileId_SystemAdmin
		        )) {
			status = true;
		}

	}
	public void dummy() {

	}
	// 判断是否显示赞助会
	public boolean getshowSponsor() {
		boolean temp =  false;
		if (camMain != null
		        && camMain.Meeting_Type__c != null
		        && camMain.Meeting_Type__c.equals('赞助会')) {
			temp = true;
		}
		return temp;
	}
	// 判断是否显示自办会//共同主办会
	public boolean getshowHost() {
		boolean temp =  false;
		if (camMain != null
		        && camMain.Meeting_Type__c != null
		        && ( camMain.Meeting_Type__c.equals('主办会')
		             || camMain.Meeting_Type__c.equals('共同主办会')
		             || CampaignCostMain.IsSatelliteMeeting__c == true)) {
			temp = true;
		}
		return temp;
	}
	@TestVisible
	private Campaign_Cost__c setupSaveCampaign() {
		Campaign_Cost__c camCostUP = new Campaign_Cost__c();
		camCostUP.id = CampaignCostMain.id;
		camCostUP.JingliApprovalManager__c = camMain.JingliApprovalManager__c;
		camCostUP.BuchangApprovalManager__c = camMain.BuchangApprovalManager__c;
		camCostUP.ZongjianApprovalManager__c = camMain.ZongjianApprovalManager__c;
		camCostUP.Campaign__c = camMain.id;
		camCostUP.OwnerId = camMain.OwnerId;
		if (getshowSponsor()) {
			camCostUP.medicalInsititute__c       = CampaignCostMain.medicalInsititute__c;
			camCostUP.medicalInsitituteCost__c     = CampaignCostMain.medicalInsitituteCost__c;
			camCostUP.nonMedicalInstitute__c     = CampaignCostMain.nonMedicalInstitute__c;
			camCostUP.nonMedicalInstituteCost__c   = CampaignCostMain.nonMedicalInstituteCost__c;
			camCostUP.standFittingCompany__c     = CampaignCostMain.standFittingCompany__c;
			camCostUP.StandFittingComCost__c     = CampaignCostMain.StandFittingComCost__c;
			camCostUP.broadcastingCompany__c     = CampaignCostMain.broadcastingCompany__c;
			camCostUP.broadcastingCompanyCost__c   = CampaignCostMain.broadcastingCompanyCost__c;
			camCostUP.OtherSponsorCost__c       = CampaignCostMain.OtherSponsorCost__c;
			camCostUP.animalExperienceCost__c     = CampaignCostMain.animalExperienceCost__c;
		} else {
			camCostUP.medicalInsititute__c      = null;
			camCostUP.medicalInsitituteCost__c     = null;
			camCostUP.nonMedicalInstitute__c     = null;
			camCostUP.nonMedicalInstituteCost__c  = null;
			camCostUP.standFittingCompany__c     = null;
			camCostUP.StandFittingComCost__c     = null;
			camCostUP.broadcastingCompany__c     = null;
			camCostUP.broadcastingCompanyCost__c   = null;
			camCostUP.OtherSponsorCost__c       = null;
			camCostUP.animalExperienceCost__c     = null;
		}

		camCostUP.isStand__c             = CampaignCostMain.isStand__c;
		camCostUP.IsSatelliteMeeting__c       = CampaignCostMain.IsSatelliteMeeting__c;
		camCostUP.isTitle__c             = CampaignCostMain.isTitle__c;
		camCostUP.IsDocLOGO__c             = CampaignCostMain.IsDocLOGO__c;
		camCostUP.IsShowingEquipment__c       = CampaignCostMain.IsShowingEquipment__c;
		camCostUP.IsAnimalExperience__c       = CampaignCostMain.IsAnimalExperience__c;
		camCostUP.isOther__c             = CampaignCostMain.isOther__c;
		if (camCostUP.isStand__c == true) {
			camCostUP.standArea__c           = CampaignCostMain.standArea__c;
		} else {
			camCostUP.standArea__c           = null;
		}
		if (camCostUP.IsAnimalExperience__c == true) {
			camCostUP.AnimalExperienceNumber__c   = CampaignCostMain.AnimalExperienceNumber__c;
		} else {
			camCostUP.AnimalExperienceNumber__c   = null;
		}
		if (camCostUP.IsSatelliteMeeting__c == true) {
			camCostUP.satelliteMeetingHours__c     = CampaignCostMain.satelliteMeetingHours__c;
		} else {
			camCostUP.satelliteMeetingHours__c     = null;
		}
		if (camCostUP.IsShowingEquipment__c == true) {
			camCostUP.showingEquipmentNumber__c   = CampaignCostMain.showingEquipmentNumber__c;
		} else {
			camCostUP.showingEquipmentNumber__c   = null;
		}
		if (camCostUP.isTitle__c == true) {
			camCostUP.titleArea__c           = CampaignCostMain.titleArea__c;
		} else {
			camCostUP.titleArea__c           = null;
		}
		if (camCostUP.isOther__c == true) {
			camCostUP.otherWords__c         = CampaignCostMain.otherWords__c;
		} else {
			camCostUP.otherWords__c         = null;
		}
		if (camCostUP.IsDocLOGO__c == true) {
			camCostUP.DocLOGOcontent__c       = CampaignCostMain.DocLOGOcontent__c;
		} else {
			camCostUP.DocLOGOcontent__c       = null;
		}

		if (getshowHost()) {
			camCostUP.paymentFromCompany__c     = CampaignCostMain.paymentFromCompany__c;
			camCostUP.paymentFromCooperator__c     = CampaignCostMain.paymentFromCooperator__c;
			camCostUP.livingCost__c         = CampaignCostMain.livingCost__c;
			camCostUP.tansportCost__c         = CampaignCostMain.tansportCost__c;
			camCostUP.travelByAir__c         = CampaignCostMain.travelByAir__c;
			camCostUP.travelByTrain__c         = CampaignCostMain.travelByTrain__c;
			camCostUP.travelByCar__c         = CampaignCostMain.travelByCar__c;
			camCostUP.banquetCost__c         = CampaignCostMain.banquetCost__c;
			camCostUP.lunchCost__c           = CampaignCostMain.lunchCost__c;
			camCostUP.teaBreakCost__c         = CampaignCostMain.teaBreakCost__c;
			camCostUP.broadcastingCost__c       = CampaignCostMain.broadcastingCost__c;
			camCostUP.publicityCost__c         = CampaignCostMain.publicityCost__c;
			camCostUP.sundriesCost__c         = CampaignCostMain.sundriesCost__c;
			camCostUP.sundryContent__c         = CampaignCostMain.sundryContent__c;
			camCostUP.hostCompany__c         = CampaignCostMain.hostCompany__c;
			camCostUP.hostCompanyCostAndTax__c     = CampaignCostMain.hostCompanyCostAndTax__c;
		} else {
			camCostUP.paymentFromCompany__c     = null;
			camCostUP.paymentFromCooperator__c     = null;
			camCostUP.livingCost__c         = null;
			camCostUP.tansportCost__c         = null;
			camCostUP.travelByAir__c         = false;
			camCostUP.travelByTrain__c         = false;
			camCostUP.travelByCar__c         = false;
			camCostUP.banquetCost__c         = null;
			camCostUP.lunchCost__c           = null;
			camCostUP.teaBreakCost__c         = null;
			camCostUP.broadcastingCost__c       = null;
			camCostUP.publicityCost__c         = null;
			camCostUP.sundriesCost__c         = null;
			camCostUP.sundryContent__c         = null;
			camCostUP.hostCompany__c         = null;
			camCostUP.hostCompanyCostAndTax__c     = null;
		}
		if (camMain.cost_Create_Date__c == null) {

			camMain.cost_Create_Date__c = date.today();
			camMain.CostStatus__c = '费用已填写';
		}
		return camCostUP;
	}

	private Decimal NullZero(Decimal d ) {
		return d == null ? 0 : d;
	}
	//SWAG-BBG8AK 提交待审批后台逻辑 start
	public void submit() {
		Campaign_Cost__c camCostUP = setupSaveCampaign();
		
		

		// SWAG-C3RBA8 20210608S Start 
		Decimal costTotal = NullZero(camCostUP.medicalInsitituteCost__c) + NullZero(camCostUP.nonMedicalInstituteCost__c)
							+ NullZero(camCostUP.StandFittingComCost__c) + NullZero(camCostUP.broadcastingCompanyCost__c )
							+ NullZero(camCostUP.animalExperienceCost__c) + NullZero(camCostUP.OtherSponsorCost__c)
							+ NullZero(camCostUP.paymentFromCompany__c) + NullZero(camCostUP.paymentFromCooperator__c)
							+ NullZero(camCostUP.livingCost__c) + NullZero(camCostUP.tansportCost__c)
							+ NullZero(camCostUP.banquetCost__c) + NullZero(camCostUP.lunchCost__c )
							+ NullZero(camCostUP.teaBreakCost__c) + NullZero(camCostUP.broadcastingCost__c )
							+ NullZero(camCostUP.publicityCost__c) + NullZero(camCostUP.sundriesCost__c) + NullZero(camCostUP.hostCompanyCostAndTax__c);
		
		if (costTotal == 0) {
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '金额为0，不能提交审批。'));
			return;
		}
		// SWAG-C3RBA8 20210608S End 

		Savepoint sp = Database.setSavepoint();
		try {
			status = false;
			upsert camCostUP;
			
			CampaignCostMain = camCostUP;
			camMain.CostStatus__c = '费用已提交';
			camMain.Campaign_Cost__c = camCostUP.id;
			update camMain;
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '已提交审批。'));
		} catch (Exception e) {
			Database.rollback(sp);
			ApexPages.addMessages(e);
		}
	}
	//SWAG-BBG8AK 提交待审批后台逻辑 end

	//保存画面数据
	public void save() {
		errorMessage = '';

		Campaign_Cost__c camCostUP = setupSaveCampaign();


		system.debug('camMain:' + camMain);
		try {
			upsert camCostUP;
			CampaignCostMain = camCostUP;
			camMain.Campaign_Cost__c = camCostUP.id;
			update camMain;
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, ' 保存完了。'));
		} catch (Exception e) {
			ApexPages.addMessages(e);
		}
	}

}