public class EventHandleController {
    public Boolean isEventDelay {get; set;}
    public Boolean isEventCancel {get; set;}
    public String title {get; set;}
    public User LoginUser {get; set;}
    public Boolean isFSE {get; set;}
    public Event targetEvent { get; set; }
    private String eventId;
    //  2021-09-15   mzy  任务管理改善开发计划 start
    public Boolean isOPDPlanFlag {get; set;}
    public Task__c taskOption {get;set;}  //用来根据记录类型显示不同的选项
    public Boolean isTaskBlank {get; set;}
    public static Integer FIELDMAX = 25;
    public Map<String,String> TaskAndOPDMapping {get;set;}
    //  2021-09-15   mzy  任务管理改善开发计划 end

    public EventHandleController(ApexPages.StandardController controller) {
        
    }
    public void init() {
        LoginUser = [Select Id, Name, Post__c, Employee_No__c, Department, Job_Category__c, Category4__c, Category6__c, ManagerId, Manager.Email From User Where Id = :UserInfo.getUserId()];
        String isDelay = ApexPages.currentPage().getParameters().get('isEventDelay');
        String isCancel = ApexPages.currentPage().getParameters().get('isEventCancel');
        // String isCancel = ApexPages.currentPage().getParameters().get('isEventCancel');
        this.eventId = ApexPages.currentPage().getParameters().get('Id');
        //2021-04-22  mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 add  查询结果增加Opd_Plan__r.Status__c, Opd_Plan__c,subject 字段  start
        this.targetEvent = [SELECT Id,Task_ID__c,Opd_Plan__c,Opd_Plan__r.Status__c,Subject,delayReasonSelect__c,delayReasonSelectFSE__c,cancelReasonSelect__c,cancelReasonSelectFSE__c,delayReasonOther__c,cancelReasonOther__c,EventStatus__c FROM Event WHERE Id = :eventId];
        //2021-04-22  mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 add  查询结果增加 Opd_Plan__r.Status__c,Opd_Plan__c,,subject 字段  end
        
        //  2021-09-15   mzy  任务管理改善开发计划 start
        if(this.targetEvent.Opd_Plan__c != null){
            isOPDPlanFlag = true;
        }else {
            isOPDPlanFlag = false;
        }
        if(String.isNotBlank(targetEvent.Task_ID__c)){
           List<Task__c> taskOptionList = [SELECT Id,delayReasonSelect__c,delayReasonSelectFSE__c,cancelReasonSelect__c,cancelReasonSelectFSE__c FROM Task__c WHERE Id = :targetEvent.Task_ID__c];
            if(taskOptionList.size()>0){
                taskOption = taskOptionList.get(0);
                isTaskBlank = false;
            }else{
                isTaskBlank = true;
            }
        }else {
            //如果没有则显事件的理由
            isTaskBlank = true;
        }
        //  2021-09-15   mzy  任务管理改善开发计划 end



        if ('true'.equals(isDelay)) {
            this.isEventDelay = true;
            this.title = '请确认延期理由';
            this.isEventCancel = false;
        }
        if ('true'.equals(isCancel)) {
            this.isEventCancel = true;
            this.title = '请确认取消理由';
            this.isEventDelay = false;
        }
        if (LoginUser.Job_Category__c != null &&  LoginUser.Job_Category__c.equals('销售服务')) {
            isFSE = true;
        }else{
            isFSE = false;
        }

        
        //2021-11-02  mzy  任务管理改善开发计划  start
        TaskAndOPDMapping = new Map<String,String>();
        TaskEventReportOPD__c mpdmapping = null;
        if('true'.equals(isDelay)){
           mpdmapping = TaskEventReportOPD__c.getValues('TaskAndOPD_Delay');
        }else {
           mpdmapping = TaskEventReportOPD__c.getValues('TaskAndOPD_Cancel');
        }
        for (Integer i = 1; i <= FIELDMAX; i++) {
            if(mpdmapping!=null){
                String lpadI = ('00' + i).right(3);
                String SSColumn = 'SS_Column_' + lpadI + '__c';
                String taskSC = String.valueOf(mpdMapping.get(SSColumn));
                if(String.isNotBlank(taskSC)){
                    String FRColumn = 'From_Column_' + lpadI + '__c';
                    String OPDFC = String.valueOf(mpdMapping.get(FRColumn));
                    TaskAndOPDMapping.put(taskSC, OPDFC);
                }
            }
        }
        //2021-11-02  mzy  任务管理改善开发计划  end
        System.System.debug('自定义映射:'+TaskAndOPDMapping);
    }
    public PageReference save() {
        Savepoint sp = Database.setSavepoint();
        try {

            String delayReason = '';
            String cancelReason = '';
            System.debug('isEventDelay---'+isEventDelay);
            System.debug('isEventCancel---'+isEventCancel);
            System.debug('isFSE---'+isFSE);
            System.debug('delayReasonSelect---'+targetEvent.delayReasonSelect__c);
            if (this.isFSE ) {
                //  2021-09-15   mzy  任务管理改善开发计划 start
                if(isTaskBlank){       
                    //任务为空             
                    if (isEventDelay && String.isBlank(targetEvent.delayReasonSelectFSE__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由不能为空'));
                        return null;
                    }
                    if (isEventCancel && String.isBlank(targetEvent.cancelReasonSelectFSE__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由不能为空'));
                        return null;
                    }
                    
                    delayReason = targetEvent.delayReasonSelectFSE__c;
                    cancelReason = targetEvent.cancelReasonSelectFSE__c;
                }else {
                    //任务不为空      
                    if (isEventDelay && String.isBlank(taskOption.delayReasonSelectFSE__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由不能为空'));
                        return null;
                    }
                    if (isEventCancel && String.isBlank(taskOption.cancelReasonSelectFSE__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由不能为空'));
                        return null;
                    }
                    delayReason = taskOption.delayReasonSelectFSE__c;
                    cancelReason = taskOption.cancelReasonSelectFSE__c;
                }

                //  2021-09-15   mzy  任务管理改善开发计划 end
            }else{
                //  2021-09-15   mzy  任务管理改善开发计划 start
                if(isTaskBlank){  
                    //任务为空                    
                    if (isEventDelay && String.isBlank(targetEvent.delayReasonSelect__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由不能为空'));
                        return null;
                    }
                    if (isEventCancel && String.isBlank(targetEvent.cancelReasonSelect__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由不能为空'));
                        return null;
                    }
                    delayReason = targetEvent.delayReasonSelect__c;
                    cancelReason = targetEvent.cancelReasonSelect__c;
                }else {
                    if (isEventDelay && String.isBlank(taskOption.delayReasonSelect__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由不能为空'));
                        return null;
                    }
                    if (isEventCancel && String.isBlank(taskOption.cancelReasonSelect__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由不能为空'));
                        return null;
                    }
                    delayReason = taskOption.delayReasonSelect__c;
                    cancelReason = taskOption.cancelReasonSelect__c;
                }
            }
            if ('其他'.equals(delayReason) && String.isBlank(targetEvent.delayReasonOther__c) && isEventDelay) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由为其他时请输入延期理由（其他）！'));
                return null;
            }
            if ('其他'.equals(cancelReason) && String.isBlank(targetEvent.cancelReasonOther__c) && isEventCancel) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由为其他时请输入取消理由（其他）！'));
                return null;
            }

            // 2021-04-22  mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 add start 
            //如果有OPD计划的话   根据取消理由  新建一条OPD计划取消延期
            //有OPD计划  , 创建一条 OPD计划取消延期
            CancelPostponePlan__c tempCancelPostponePlan = new  CancelPostponePlan__c();
            if(targetEvent.Opd_Plan__c != null){
                //判断OPD状态是否是取消,如果是取消则不创建OPD计划取消延期对象
                if(!'取消'.equals(targetEvent.Opd_Plan__r.Status__c)){
                    //0.OPD计划
                    tempCancelPostponePlan.CancelOPDPlan__c =  targetEvent.Opd_Plan__c;
                    //要进行取消
                    if(isEventCancel){
                        //1.取消状态
                        tempCancelPostponePlan.Status__c = '取消成功';
                        //2.记录类型
                        Id recodeTypeId = Schema.SObjectType.CancelPostponePlan__c.getRecordTypeInfosByDeveloperName().get('CancelType').getRecordTypeId();
                        tempCancelPostponePlan.RecordTypeId = recodeTypeId;
                        //3.取消理由
                        tempCancelPostponePlan.cancelReasonCombobox__c = TaskAndOPDMapping.get(cancelReason);
                        /*
                        if('客户事件变更或冲突'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '医院取消试用';
                        }else if('与其他突发事件冲突'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '计划调整变更';
                        }else if('问题已经解决无需拜访'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '问题已解决无需OPD';
                        }else if('无合适病例（适合病例流失）OPD取消'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '手术取消';
                        }else if('问题已解决无需OPD'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '问题已解决无需OPD';
                        }else if('客户已采购竞品，失单'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '客户已采购竞品，失单';
                        }else if('客户取消OPD'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '医院取消试用';
                        }else if('计划重复'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '计划重复';
                        }else if('其他'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }
                        //2021-09-08 Yjk 补充条件
                        else if('已失单'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('已订货'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('与客户安排冲突'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('上月已跟进'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('项目暂停'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('医院建设中'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('项目取消'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('任务分配不准'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('订货推迟'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        } */
                      
                        
                        //insert tempCancelPostponePlan;

                        System.debug('延期对象的Id:'+tempCancelPostponePlan.Id);
                    }
                }                
            }
            // 2021-04-22  mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 add end

            
            task__c task = new task__c();
            if (targetEvent.Task_ID__c != null) {
                task = [SELECT Id,Initial_Task__c,isDelay__c,delayReason__c,delayReasonOther__c,cancelReason__c,cancelReasonOther__c,taskStatus__c,cancelDate__c FROM task__c WHERE Id = :targetEvent.Task_ID__c];

                if (isEventDelay) {
                    task.isDelay__c = true;
                    task.delayReason__c = delayReason;
                    task.delayReasonOther__c = targetEvent.delayReasonOther__c;
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  start
                    if(this.isFSE){
                        task.delayReasonSelectFSE__c = delayReason;
                        targetEvent.delayReasonSelectFSE__c =delayReason; //  2021-09-15   mzy  任务管理改善开发计划 start
                    }else{
                        task.delayReasonSelect__c = delayReason; 
                        targetEvent.delayReasonSelect__c =delayReason;   //  2021-09-15   mzy  任务管理改善开发计划 start
                    }
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  start
                    
                    // //  2021-09-15   mzy  任务管理改善开发计划 start
                    // if(isTaskBlank==false){
                    //     //如果任务不为空
                    //     if(this.isFSE){
                    //         targetEvent.delayReasonSelectFSE__c =delayReason;
                    //     }else{
                    //         targetEvent.delayReasonSelect__c =delayReason;
                    //     }
                    // }
                    // //  2021-09-15   mzy  任务管理改善开发计划 end
                    targetEvent.delayReason__c = delayReason;
                    

                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  start
                    //如果有源任务,则将延期理由同步到源任务上
                    if(task.Initial_Task__c != null){
                        task__c InitialTask = new task__c();
                        InitialTask.Id = task.Initial_Task__c;
                        InitialTask.isDelay__c = true;
                        InitialTask.delayReason__c = task.delayReason__c;
                        InitialTask.delayReasonOther__c = task.delayReasonOther__c;
                        if(this.isFSE){
                            InitialTask.delayReasonSelectFSE__c = delayReason;
                        }else{
                            InitialTask.delayReasonSelect__c = delayReason;
                        }
                        update InitialTask;
                    }
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  end
                    update task;
                    update targetEvent;
                    //2021-04-25 mzy  WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update 将 isEventDelay 改为 isDelay
                    return new PageReference('/apex/SimpleEventRegister?isDelay=true&taskId='+task.Id+'&id='+targetEvent.Id);
                }else if (isEventCancel) {
                    task.cancelReason__c = cancelReason;
                    task.cancelReasonOther__c = targetEvent.cancelReasonOther__c;
                    //2021-04-25 mzy  WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update start
                    //task.cancelReasonSelectFSE__c = targetEvent.cancelReasonSelectFSE__c == null? '':targetEvent.cancelReasonSelectFSE__c;
                    //task.cancelReasonSelect__c = targetEvent.cancelReasonSelect__c == null? '':('客户时间变更或冲突'.equals(targetEvent.cancelReasonSelect__c)==true ?'客户事件变更或冲突':targetEvent.cancelReasonSelect__c);            
                    //2021-04-25 mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update end
                    if(this.isFSE){
                        task.cancelReasonSelectFSE__c = cancelReason;
                        targetEvent.cancelReasonSelectFSE__c =cancelReason==null?'':cancelReason; //  2021-09-15   mzy  任务管理改善开发计划 start
                    }else{
                        task.cancelReasonSelect__c = cancelReason;
                        System.debug('理由:'+cancelReason);
                        //targetEvent.cancelReasonSelect__c =cancelReason ==null?'':('客户事件变更或冲突'.equals(cancelReason)==true?'客户时间变更或冲突':cancelReason);//  2021-09-15   mzy  任务管理改善开发计划 start
                        targetEvent.cancelReasonSelect__c =cancelReason ==null?'':cancelReason; //  2021-09-15   mzy  任务管理改善开发计划 start
                    }
                    task.taskStatus__c = '04 取消';
                    task.cancelDate__c = date.today();
                    update task;
                    //2021-06-09 mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update start
                    targetEvent.Subject += '【已取消】';
                    //2021-06-09 mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update end
                    // //  2021-09-15   mzy  任务管理改善开发计划 start
                    // //如果任务不为空
                    // if(this.isFSE){
                    //     targetEvent.cancelReasonSelectFSE__c =cancelReason==null?'':cancelReason;
                    // }else{
                    //     targetEvent.cancelReasonSelect__c =cancelReason ==null?'':('客户事件变更或冲突'.equals(cancelReason)==true?'客户时间变更或冲突':cancelReason);
                    // }
                    // //  2021-09-15   mzy  任务管理改善开发计划 end
                    targetEvent.EventStatus__c = '04 取消';
                    targetEvent.cancelReason__c = cancelReason; //  2021-09-15   mzy  任务管理改善开发计划 start
                    update targetEvent;

                    // //  2021-09-15   mzy  任务管理改善开发计划 start
                    if('取消成功'.equals(tempCancelPostponePlan.Status__c)){
                        insert tempCancelPostponePlan;
                    }
                    // //  2021-09-15   mzy  任务管理改善开发计划 end



                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  start
                    //如果有源任务,则将取消理由同步到源任务上
                    if(task.Initial_Task__c != null){
                        task__c InitialTask = new task__c();
                        InitialTask.Id = task.Initial_Task__c;
                        InitialTask.taskStatus__c = '04 取消';
                        InitialTask.cancelDate__c = date.today();
                        InitialTask.cancelReason__c = task.cancelReason__c;
                        InitialTask.cancelReasonOther__c = task.cancelReasonOther__c;
                        InitialTask.cancelReasonSelectFSE__c = task.cancelReasonSelectFSE__c;
                        InitialTask.cancelReasonSelect__c = task.cancelReasonSelect__c;
                        update InitialTask;
                    }
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  end

                    return new PageReference('/');
                }
            }else{


                if (isEventDelay) {
                    targetEvent.delayReason__c = delayReason;
                    //20210723 zh SWAG-C5784H 【委托】事件无法延期 start
                    update targetEvent;
                    return new PageReference('/apex/SimpleEventRegister?isDelay=true&id='+targetEvent.Id);
                    //20210723 zh SWAG-C5784H 【委托】事件无法延期 end
                }else if (isEventCancel) {
                    targetEvent.Subject += '【已取消】';
                    targetEvent.EventStatus__c = '04 取消';
                    //2021-08-19  mzy SWAG-C5784H 【委托】事件无法延期 start
                    targetEvent.cancelReason__c = cancelReason;
                    //2021-08-19  mzy SWAG-C5784H 【委托】事件无法延期 end
                    update targetEvent;
                }
                
                return new PageReference('/');
            }
            
            return null;
        } catch (Exception e) {
            System.debug('错误信息 :'+ e.getLineNumber());
            
            System.debug('错误信息 :'+ e);
            ApexPages.addMessages(e);
            Database.rollback(sp);
            return null;
        }
        // return null;
    }

}