public with sharing class CampaignCreateController {
    // public List<AdjudicationData> AdjudicationDataList{ get; set; }
    public AdjudicationData AdjudicationDataobj{ get; set; }
    public boolean hasError { get; set; }
    // public String MeetingApprovedName{ get; set; }
    public String flag{ get; set; }
    public String CampaignTheme{ get; set; }
    private String RecordTypeId{ get; set; }
    private String CMRT{ get; set; }
    private String retURL{ get; set; }
    private String p8{ get; set; }
    private String CF00N10000002DIsr{ get; set; }
    private String ent{ get; set; }
    private String CF00N10000002DIsr_lkid{ get; set; }

    public CampaignCreateController(ApexPages.StandardController controller) {
        RecordTypeId = ApexPages.currentPage().getParameters().get('RecordType');
        CMRT = ApexPages.currentPage().getParameters().get('CMRT');
        retURL = ApexPages.currentPage().getParameters().get('retURL');
        p8 = ApexPages.currentPage().getParameters().get('p8');
        CF00N10000002DIsr = ApexPages.currentPage().getParameters().get('CF00N10000002DIsr');
        ent = ApexPages.currentPage().getParameters().get('ent');
        CF00N10000002DIsr_lkid = ApexPages.currentPage().getParameters().get('CF00N10000002DIsr_lkid');
    }

    public Pagereference init(){
        hasError = false;
        RecordType recordType = [select Id, Name, DeveloperName from RecordType where IsActive = true and id = :RecordTypeId];
        String RecordTypeName = recordType.DeveloperName;
        //记录类型为1.学会/会议或者6.营业部专用会议的时候才跳转小页面
        if(RecordTypeName == 'Society' || RecordTypeName == 'BusinessTraining'){
            // AdjudicationDataList = new List<AdjudicationData>();
            AdjudicationData ad = new AdjudicationData();
            ad.cam = new Campaign();
            // 默认选中
            ad.cam.IF_Approved__c = true;
            // AdjudicationDataList.add(ad);
            AdjudicationDataobj = ad;
            return null;
        } else{
            String buildUrl = skipBuildPage();
            PageReference pg = new PageReference(buildUrl);
            pg.setRedirect(true);
            return pg;
        }
    }

    //标准页面跳转新建学会页面
    public String skipBuildPage(){
        String str = '/701/e?cpn1=*';
        if(RecordTypeId != null){
            str += '&RecordType=' + RecordTypeId;
        }
        if(CMRT != null){
            str += '&CMRT=' + CMRT;
        }
        if(retURL != null){
            str += '&retURL=' + retURL;
        }
        if(p8 != null){
            str += '&p8=' + p8;
        }
        if(CF00N10000002DIsr != null){
            str += '&CF00N10000002DIsr=' + CF00N10000002DIsr;
        }
        if(ent != null){
            str += '&ent=' + ent;
        }
        if(CF00N10000002DIsr_lkid != null){
            str += '&CF00N10000002DIsr_lkid=' + CF00N10000002DIsr_lkid;
        }
        str += '&nooverride=1';
        return str;
    }
    public void changeCheckboxs(){
        System.debug('flag++++'+flag);
        if(flag=='0'){
            AdjudicationDataobj.cam.IF_Approved__c=false;
        }else if(flag=='1'){
            AdjudicationDataobj.cam.IF_Approved__c=true;
        }
    }
    //小页面跳转学会新建页面
    public Pagereference skipPage(){
        List<String> MeetingApprovedIdList = new List<String>();
        Map<String, Application_for_Conference_Adjudication__c> MeetingApprovedMap = new Map<String, Application_for_Conference_Adjudication__c>();
        // for(AdjudicationData ad : AdjudicationDataList){
            // if(String.isNotBlank(ad.cam.Meeting_Approved_No__c)){
            //     MeetingApprovedIdList.add(ad.cam.Meeting_Approved_No__c);
            // }
        // }
        if(String.isNotBlank(AdjudicationDataobj.cam.Meeting_Approved_No__c)){
            MeetingApprovedIdList.add(AdjudicationDataobj.cam.Meeting_Approved_No__c);
        }
        List<Application_for_Conference_Adjudication__c> MeetingApprovedList = 
            [select Id, Name, StartDate__c, EndDate__c, Implementation_Purpose1__c, OfficeCategory__c, MeetingType__c, WorkshopPlace__c,
                HostName__c, CooperatorCompany__c, ConveningParticipantsNum__c, ExpectedOlympusAttendance__c,ExpectedHcpAttendance__c,
                SponsorshipCategory__c, IsPaidToSponsor__c, ActivityTypeName__c, Type_of_adjudication_meeting__c 
                from Application_for_Conference_Adjudication__c where Id = :MeetingApprovedIdList];
                
        for(Application_for_Conference_Adjudication__c afca : MeetingApprovedList){
            MeetingApprovedMap.put(afca.Id, afca);
        }
        String url = skipBuildPage();
        // for(AdjudicationData ad : AdjudicationDataList){
            system.debug('123321+++++++===='+AdjudicationDataobj.cam.IF_Approved__c);
            if(AdjudicationDataobj.cam != null){ 
                if(AdjudicationDataobj.cam.IF_Approved__c || String.isNotBlank(AdjudicationDataobj.cam.Meeting_Approved_No__c)){
                    url += NFMUtil.isSandbox() ? '&00N1m000005tHDi=1' : '&00N10000009HBVs=1';
                }else{
                    url += NFMUtil.isSandbox() ? '&00N1m000005tHDi=0' : '&00N10000009HBVs=0';
                }
            }
            if(AdjudicationDataobj.cam != null && String.isNotBlank(AdjudicationDataobj.cam.Meeting_Approved_No__c)){
                Application_for_Conference_Adjudication__c mam = MeetingApprovedMap.get(AdjudicationDataobj.cam.Meeting_Approved_No__c);
                //会议决裁编码的Name,查找字段赋值Name和ID都要赋值
                if(String.isNotBlank(mam.Name)){
                    url += '&CF00N1m000006G5NG=' + mam.Name;
                }
                //会议决裁编码的ID,如果根据名字找到多个,就根据ID确定唯一的一个值
                url += '&CF00N1m000006G5NG_lkid=' + AdjudicationDataobj.cam.Meeting_Approved_No__c;
                //开始日期、结束日期、活动性质、科室分类、会议类型、会场地点、会议主办方、会议承办方、参加人数（召开）、预计参会Olympus人数
                if(String.isNotBlank(String.valueOf(mam.StartDate__c))){
                    url += '&cpn5=' + String.valueOf(mam.StartDate__c).replace('-','/');
                }
                if(String.isNotBlank(String.valueOf(mam.EndDate__c))){
                    url += '&cpn6=' + String.valueOf(mam.EndDate__c).replace('-','/');
                }
                if(String.isNotBlank(mam.Implementation_Purpose1__c)){
                    url += '&00N100000098OEy=' + mam.Implementation_Purpose1__c;
                }
                if(String.isNotBlank(mam.OfficeCategory__c)){
                    url += '&00N10000006pwAg=' + mam.OfficeCategory__c;
                }
                if(String.isNotBlank(mam.MeetingType__c)){
                    url += '&00N100000072rGE=' + mam.MeetingType__c;
                }
                if(String.isNotBlank(mam.WorkshopPlace__c)){
                    url += '&00N10000002DGF0=' + mam.WorkshopPlace__c;
                }
                if(String.isNotBlank(mam.HostName__c)){
                    url += '&00N10000008qcCO=' + mam.HostName__c;
                }
                if(String.isNotBlank(mam.CooperatorCompany__c)){
                    url += '&00N10000008qcCT=' + mam.CooperatorCompany__c;
                }
                if(String.isNotBlank(String.valueOf(mam.ConveningParticipantsNum__c))){
                    url += '&00N10000008ptmP=' + mam.ConveningParticipantsNum__c;
                }
                if(String.isNotBlank(String.valueOf(mam.ExpectedOlympusAttendance__c))){
                    url += (NFMUtil.isSandbox() ? '&00N1m00000652oO=' : '&00N10000009HBYD=') + mam.ExpectedOlympusAttendance__c;
                }
                //预计参会HCP人数、赞助类别、赞助款项是否支付给主办方、活动形式
                if(String.isNotBlank(String.valueOf(mam.ExpectedHcpAttendance__c))){
                    url += (NFMUtil.isSandbox() ? '&00N1m00000652oT=' : '&00N10000009HBY8=') + mam.ExpectedHcpAttendance__c;
                }
                if(String.isNotBlank(mam.SponsorshipCategory__c)){
                    url += (NFMUtil.isSandbox() ? '&00N1m000006fdGn=' : '&00N10000009HBYI=') + mam.SponsorshipCategory__c;
                }
                if(String.isNotBlank(mam.IsPaidToSponsor__c)){
                    url += (NFMUtil.isSandbox() ? '&00N1m000006fcjJ=' : '&00N10000009HBYN=') + mam.IsPaidToSponsor__c;
                }
                if(String.isNotBlank(mam.Type_of_adjudication_meeting__c)){
                    url += (NFMUtil.isSandbox() ? '&00N1m00000652jY=' : '&00N10000009HBY3=') + mam.Type_of_adjudication_meeting__c;
                }
            }
        // }
        PageReference pg = new PageReference(url);
        pg.setRedirect(true);
        return pg;
    }

    //跳转回学会主页面
    public Pagereference returnPage(){
        PageReference pg = new PageReference('/701/o');
        pg.setRedirect(true);
        return pg;
    }

    //返回会议裁决主题
    public void displayDecisionTheme(){
        Savepoint sp = Database.setSavepoint();
        try {
            String aid = '';
            // for(AdjudicationData ad : AdjudicationDataList){
            //     if(String.isNotBlank(ad.cam.Meeting_Approved_No__c)){
            //         aid = ad.cam.Meeting_Approved_No__c;
            //         break;
            //     }
            // }
            if(String.isNotBlank(AdjudicationDataobj.cam.Meeting_Approved_No__c)){
                aid = AdjudicationDataobj.cam.Meeting_Approved_No__c;
                AdjudicationDataobj.cam.IF_Approved__c = true;
            }
            
            Application_for_Conference_Adjudication__c afca = [select Id, CampaignTheme__c from Application_for_Conference_Adjudication__c
                                                                where id = :aid];
            //查出来对应的会议主题传给全局变量，前台显示
            CampaignTheme = afca.CampaignTheme__c;
        } catch (System.Exception e) {
            Database.rollback(sp);
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
            hasError = true;
        }
    }

    public class AdjudicationData{
        public Boolean isAdjudication{ get; set; }
        public Campaign cam{ get; set; }
    }
}