public without sharing class ApplicationMessageController {
    String oldResponse;
    // 备品借出申请
    public loaner_application__c ra { get; set; }
    public loaner_application__c ra1 { get; set; }
    public loaner_application__c cc { get; set; }
    // 备品借出申请ID
    public Id raid { get; private set; }

    public Boolean hasError { get; private set; }
    public String baseUrl { get; private set; }
    

    public ApplicationMessageController() {
        //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        raid = ApexPages.currentPage().getParameters().get('raid');
    }

    public ApplicationMessageController(ApexPages.StandardController stdController) {
        //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        raid = stdController.getId();
    }

    // 画面初始化
    public void init() {
        hasError = false;
        ra = new loaner_application__c();
        cc = new loaner_application__c();
        List<loaner_application__c> raList = [select Id, Name, OCN_Internal_Notes__c from loaner_application__c where 

Id = :raid];
        if (raList.size() > 0) {
            ra = raList[0];
        }
    }
    
     // 画面初始化-POP UP
    public void init1() {
        hasError = false;
        ra1 = new loaner_application__c();
        List<loaner_application__c> ra1List = [select Id, Name, OCN_Internal_Notes__c from loaner_application__c where 

Id = :raid];
        if (ra1List.size() > 0) {
            ra1 = ra1List[0];
        }
        oldResponse = ra1.OCN_Internal_Notes__c;
        ra1.OCN_Internal_Notes__c = '';
    }



    // 保存按钮
    public PageReference saveBtn() {
        hasError = false;

        String response = ra1.OCN_Internal_Notes__c;
        if (response == null || response.trim().length() == 0) {
            ra1.OCN_Internal_Notes__c.addError('请输入内容。');
            hasError=true;
            return null;
        }

        // 送信者
        String username = UserInfo.getName();
        // 发送时间
        Datetime dt = Datetime.now();
        // 原留言-oldResponse
        // oldResponse = ra.OCN_Internal_Notes__c;
        
        // 现留言
        String temp = '';
        temp += '****** ' + username + ' ' + dt.format() + ' ******\n';
        temp += ra1.OCN_Internal_Notes__c;
        if (oldResponse != null && oldResponse.trim().length() > 0) {
            temp += '\n\n' + oldResponse;
        }
        try {
            // 更新留言
    //        loaner_application__c updra = new loaner_application__c();
    //        updra.Id = ra.Id;
            ra1.OCN_Internal_Notes__c = temp;

            update ra1;
            ra1.OCN_Internal_Notes__c = '';

            //Create a dummy instance of outbound email object
    //        Messaging.SingleEmailMessage message= new Messaging.SingleEmailMessage();
            //set a roolback point
    //        Savepoint sp = Database.setSavepoint();
            //set desired email template id
    //        message.templateId = 'xxxxxx';
            //set the target object record id(this is mandatory while using email template)
    //        Id recId = [select Id from Contact where Email != null limit 1].id;
    //        message.targetObjectId = recId;
            //fire dummy email
    //        messaging.sendEmail(new Messaging.SingleEmailMessage[] {message});
            //roll back to savepoint so that email is not sent
    //        Database.rollback(sp);
            //Create insatnce of actual email
            Messaging.SingleEmailMessage messageNEW= new 

Messaging.SingleEmailMessage();
            //get the bode from above dummy instance and set it to your actual email
            //messageNEW.HTMLBody = message.getHtmlBody();
            //messageNEW.Subject = '备品借出申请' + ra.Name + '' + 'OCN内部留言';
            messageNEW.Subject = '备品借出申请 ' + ra1.Name + '-OCSM内部留言';
            messageNEW.PlainTextBody = temp;
            //set desired email addresses
            messageNEW.setCharset('UTF-8');          
            
        } catch (Exception ex) {
            system.debug('=====' + ex.getMessage());
            hasError = true;
            ApexPages.addMessages(ex);
            return null;
        }

        return null;
    }
}