public with sharing class DateModifyController {
    public String coId {get; set;}
    public String submitResult {get; set;}
    public Campaign cam {get; set;}
    public DateModifyController() {
        // coId='70110000000lWs2AAE';
        coId = System.currentPageReference().getParameters().get('id');
    }
    //初始化
    public void init() {
        cam =
            [select id, is_pass_or_fail__c, CeremonyLeaders__c, CeremonyDate__c, CeremonyDate_copy__c,
             Lesson1Date_copy__c, Lesson2Date_copy__c, Lesson3Date_copy__c,
             Lesson4Date_copy__c, Lesson5Date_copy__c, Lesson6Date_copy__c, Lesson7Date_copy__c,
             Lesson8Date_copy__c, FinalExamDate_copy__c, is_true_or_false__c,
             Lesson1Date__c, Lesson2Date__c, Lesson3Date__c, Lesson4Date__c,
             Lesson5Date__c, Lesson6Date__c, Lesson7Date__c, Lesson8Date__c, FinalExamDate__c,
             IsOpenCeremony__c, Open_day__c, Status_Service__c
             from Campaign
             where id = : coId];
        if (cam == null) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '找不到对应的内镜临床工程师培养。'));
            return;

        }
        setUpDate();
        //System.debug(cam.CeremonyLeaders__c);
    }
    public boolean getIsblock() {
        boolean Isblock = true;
        if (cam.Status_Service__c.equals('开班筹备中') || cam.Status_Service__c.equals('开班中') ) {
            Isblock = false;

        }
        return Isblock;
    }
    // 初始化日期赋值
    private void setUpDate() {
        if (cam != null) {
            if (cam.CeremonyDate_copy__c == null) {
                cam.CeremonyDate_copy__c = cam.CeremonyDate__c;
            }
            if (cam.Lesson1Date_copy__c == null) {
                cam.Lesson1Date_copy__c = cam.Lesson1Date__c;
            }
            if (cam.Lesson2Date_copy__c == null) {
                cam.Lesson2Date_copy__c = cam.Lesson2Date__c;
            }
            if (cam.Lesson3Date_copy__c == null) {
                cam.Lesson3Date_copy__c = cam.Lesson3Date__c;
            }
            if (cam.Lesson4Date_copy__c == null) {
                cam.Lesson4Date_copy__c = cam.Lesson4Date__c;
            }
            if (cam.Lesson5Date_copy__c == null) {
                cam.Lesson5Date_copy__c = cam.Lesson5Date__c;
            }
            if (cam.Lesson6Date_copy__c == null) {
                cam.Lesson6Date_copy__c = cam.Lesson6Date__c;
            }
            if (cam.Lesson7Date_copy__c == null) {
                cam.Lesson7Date_copy__c = cam.Lesson7Date__c;
            }
            if (cam.Lesson8Date_copy__c == null) {
                cam.Lesson8Date_copy__c = cam.Lesson8Date__c;
            }
            if (cam.FinalExamDate_copy__c == null) {
                cam.FinalExamDate_copy__c = cam.FinalExamDate__c;
            }
        }
    }
    //保存按钮
    public void saveBtn() {
        Savepoint  sp = Database.setSavepoint();
        try {
            upsert cam;
            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, '保存成功！'));
        } catch (Exception ex) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage() + ' | Line:' + ex.getLineNumber()));
            Database.rollback(sp);
        }

    }

    // 设置 时间修改邮件显示内容
    @TestVisible
    private string setUpEmailStr() {
        string EmailStr = '';
        if (cam.CeremonyDate__c != cam.CeremonyDate_copy__c) {
            string tempOrigin =  cam.CeremonyDate__c == null ? '无' : cam.CeremonyDate__c.format();
            string tempNew =  cam.CeremonyDate_copy__c == null ? '无' : cam.CeremonyDate_copy__c.format();
            EmailStr += '申请更改时间的事项：开班仪式时间 原定日期：' + tempOrigin
                        + ' 希望修改日期：' + tempNew +  '\n\n';
        }
        if (cam.Lesson1Date__c != cam.Lesson1Date_copy__c) {
            string tempOrigin =   cam.Lesson1Date__c == null ? '无' : cam.Lesson1Date__c.format();
            string tempNew =  cam.Lesson1Date_copy__c == null ? '无' : cam.Lesson1Date_copy__c.format();
            EmailStr += '申请更改时间的事项： 第一次授课 原定日期：' + tempOrigin + ' 希望修改日期：'
                        + tempNew +  '\n\n';

        }
        if (cam.Lesson2Date__c != cam.Lesson2Date_copy__c) {
            string tempOrigin =   cam.Lesson2Date__c == null ? '无' : cam.Lesson2Date__c.format();
            string tempNew =  cam.Lesson2Date_copy__c == null ? '无' : cam.Lesson2Date_copy__c.format();
            EmailStr += '申请更改时间的事项： 第二次授课 原定日期：' + tempOrigin + ' 希望修改日期：'
                        + tempNew +  '\n\n';

        }
        if (cam.Lesson3Date__c != cam.Lesson3Date_copy__c) {
            string tempOrigin =   cam.Lesson3Date__c == null ? '无' : cam.Lesson3Date__c.format();
            string tempNew =  cam.Lesson3Date_copy__c == null ? '无' : cam.Lesson3Date_copy__c.format();
            EmailStr += '申请更改时间的事项： 第三次授课 原定日期：' + tempOrigin + ' 希望修改日期：'
                        + tempNew +  '\n\n';

        }
        if (cam.Lesson4Date__c != cam.Lesson4Date_copy__c) {
            string tempOrigin =   cam.Lesson4Date__c == null ? '无' : cam.Lesson4Date__c.format();
            string tempNew =  cam.Lesson4Date_copy__c == null ? '无' : cam.Lesson4Date_copy__c.format();
            EmailStr += '申请更改时间的事项： 第四次授课 原定日期：' + tempOrigin + ' 希望修改日期：'
                        + tempNew +  '\n\n';

        }
        if (cam.Lesson5Date__c != cam.Lesson5Date_copy__c) {
            string tempOrigin =   cam.Lesson5Date__c == null ? '无' : cam.Lesson5Date__c.format();
            string tempNew =  cam.Lesson5Date_copy__c == null ? '无' : cam.Lesson5Date_copy__c.format();
            EmailStr += '申请更改时间的事项： 第五次授课 原定日期：' + tempOrigin + ' 希望修改日期：'
                        + tempNew +  '\n\n';

        }
        if (cam.Lesson6Date__c != cam.Lesson6Date_copy__c) {
            string tempOrigin =   cam.Lesson6Date__c == null ? '无' : cam.Lesson6Date__c.format();
            string tempNew =  cam.Lesson6Date_copy__c == null ? '无' : cam.Lesson6Date_copy__c.format();
            EmailStr += '申请更改时间的事项： 第六次授课 原定日期：' + tempOrigin + ' 希望修改日期：'
                        + tempNew +  '\n\n';

        }
        if (cam.Lesson7Date__c != cam.Lesson7Date_copy__c) {
            string tempOrigin =   cam.Lesson7Date__c == null ? '无' : cam.Lesson7Date__c.format();
            string tempNew =  cam.Lesson7Date_copy__c == null ? '无' : cam.Lesson7Date_copy__c.format();
            EmailStr += '申请更改时间的事项： 第七次授课 原定日期：' + tempOrigin + ' 希望修改日期：'
                        + tempNew +  '\n\n';
        }
        if (cam.Lesson8Date__c != cam.Lesson8Date_copy__c) {
            string tempOrigin =   cam.Lesson8Date__c == null ? '无' : cam.Lesson8Date__c.format();
            string tempNew =  cam.Lesson8Date_copy__c == null ? '无' : cam.Lesson8Date_copy__c.format();
            EmailStr += '申请更改时间的事项： 第八次授课 原定日期：' + tempOrigin + ' 希望修改日期：'
                        + tempNew +  '\n\n';

        }
        if (cam.FinalExamDate__c != cam.FinalExamDate_copy__c) {
            string tempOrigin =   cam.FinalExamDate__c == null ? '无' : cam.FinalExamDate__c.format();
            string tempNew =  cam.FinalExamDate_copy__c == null ? '无' : cam.FinalExamDate_copy__c.format();
            EmailStr += '申请更改时间的事项： 最终考核 原定日期：' + tempOrigin
                        + ' 希望修改日期：' + tempNew +  '\n\n';

        }
        return EmailStr;
    }

    private void dateCopy() {
        cam.CeremonyDate_original__c = cam.CeremonyDate__c;
        cam.Lesson1Date_original__c = cam.Lesson1Date__c;
        cam.Lesson2Date_original__c = cam.Lesson2Date__c;
        cam.Lesson3Date_original__c = cam.Lesson3Date__c;
        cam.Lesson4Date_original__c = cam.Lesson4Date__c;
        cam.Lesson5Date_original__c = cam.Lesson5Date__c;
        cam.Lesson6Date_original__c = cam.Lesson6Date__c;
        cam.Lesson7Date_original__c = cam.Lesson7Date__c;
        cam.Lesson8Date_original__c = cam.Lesson8Date__c;
        cam.FinalExamDate_original__c = cam.FinalExamDate__c;


        cam.CeremonyDate__c = cam.CeremonyDate_copy__c;
        cam.Lesson1Date__c = cam.Lesson1Date_copy__c;
        cam.Lesson2Date__c = cam.Lesson2Date_copy__c;
        cam.Lesson3Date__c = cam.Lesson3Date_copy__c;
        cam.Lesson4Date__c = cam.Lesson4Date_copy__c;
        cam.Lesson5Date__c = cam.Lesson5Date_copy__c;
        cam.Lesson6Date__c = cam.Lesson6Date_copy__c;
        cam.Lesson7Date__c = cam.Lesson7Date_copy__c;
        cam.Lesson8Date__c = cam.Lesson8Date_copy__c;
        cam.FinalExamDate__c = cam.FinalExamDate_copy__c;

        cam.is_true_or_false__c = true;
        cam.Status = '时间修改申请中';
        cam.is_pass_or_fail__c = 4;

    }
    //提交审批按钮
    public void submitBtn() {


        Date startDate = cam.CeremonyDate_copy__c == null ?
                         cam.Lesson1Date_copy__c : cam.CeremonyDate_copy__c;
        if (startDate == null ) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,
                                 Schema.SObjectType.Campaign.fields.StartDate__c.label
                                 + '不能为空。'));
            return;
        }
         Date startDate_original = cam.CeremonyDate__c == null ?
                         cam.Lesson1Date__c : cam.CeremonyDate__c;
        if (startDate < cam.Open_day__c && startDate != startDate_original) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,
                                 Schema.SObjectType.Campaign.fields.StartDate__c.label
                                 + '不能早于审核通过日。'));
            return;
        }
        if (startDate > cam.Lesson1Date_copy__c ||
                startDate > cam.Lesson2Date_copy__c ||
                startDate > cam.Lesson3Date_copy__c ||
                startDate > cam.Lesson4Date_copy__c ||
                startDate > cam.Lesson5Date_copy__c ||
                startDate > cam.Lesson6Date_copy__c ||
                startDate > cam.Lesson7Date_copy__c ||
                startDate > cam.Lesson8Date_copy__c ||
                startDate > cam.FinalExamDate_copy__c) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,
                                 '其他日期不能早于开班日期。'));
            return;
        }
        Date endDate = cam.Lesson8Date_copy__c;
        if (endDate == null) {
            endDate = cam.Lesson7Date_copy__c;
        }
        if (endDate == null) {
            endDate = cam.Lesson6Date_copy__c;
        }
        if (endDate == null) {
            endDate = cam.Lesson5Date_copy__c;
        }
        if (endDate == null) {
            endDate = cam.Lesson4Date_copy__c;
        }
        if (endDate == null) {
            endDate = cam.Lesson3Date_copy__c;
        }
        if (endDate == null) {
            endDate = cam.Lesson2Date_copy__c;
        }
        if (endDate == null) {
            endDate = cam.Lesson1Date_copy__c;
        }
        if (endDate != null && endDate > cam.FinalExamDate_copy__c) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,
                                 '最后考核日期不能早于最后一次授课日期'));
            return;

        }

        Savepoint  sp1 = Database.setSavepoint();
        try {
            cam.DateModifiedEmailContext__c = setUpEmailStr();
            if (cam.IsOpenCeremony__c != null && cam.IsOpenCeremony__c.equals('否')) {

                dateCopy();
                upsert cam;
            } else if (cam.IsOpenCeremony__c != null  && cam.IsOpenCeremony__c.equals('是')
                       &&  cam.CeremonyDate_copy__c != null) {
                dateCopy();
                upsert cam;
            } else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,
                                     '选择有开班仪式时请填写开班仪式时间！'));
                return;
            }

            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setObjectId(this.cam.id);
            Approval.ProcessResult apo = Approval.process(psr);
            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, '审批已提交'));
            submitResult = 'success';
        } catch (Exception ex2) {
            System.debug('异常：' + ex2.getMessage() + ' | Line:' + ex2.getLineNumber());
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex2.getMessage()));
            Database.rollback(sp1);
            return;
        }
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '审批已提交'));


    }


}