public with sharing class OrderShippingNotificationController {

    public String type { get; set; }

    // 订单
    public Order ra { get; set; }
    // 订单ID
    public Id raid { get; private set; }
    // 订单记录类型
    public String recordtype { get; private set; }
    
    public Boolean hasError { get; private set; }
    public String baseUrl { get; private set; }    

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

    }

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

    // 画面初始化
    public void init() {
        hasError = false;
        ra = new Order();
        List<Order> raList = [select Id, Name,ContractCode__c, ShippingNotes__c,RecordTypeId,ShippingRecieverEmailAdr__c, OppExpectedDeliveryDate__c, ContractLink__c from Order where Id = :raid];
        if (raList.size() > 0) {
            ra = raList[0];
        }
       
        ra.ShippingNotes__c = '期望发货日期:' + ra.OppExpectedDeliveryDate__c;

    }

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

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

        // 送信者
        String username = UserInfo.getName();
        // 发送时间
        Datetime dt = Datetime.now();
        // 相关用户检索
        List<Id> ccList = new List<Id>();
//      ccList.add(ra.Person_In_Charge__c);
//      ccList.add(ra.ApplyUser__c);
//        if (cc.JingliApprovalManager__c != null) {
//            ccList.add(cc.JingliApprovalManager__c);
//        }
//       if (cc.SalesManager__c != null) {
//            ccList.add(cc.SalesManager__c);
//        }
//        if (cc.BuchangApprovalManager__c != null) {
//            ccList.add(cc.BuchangApprovalManager__c);
//        }
//        if (cc.BuchangApprovalManagerSales__c != null) {
//            ccList.add(cc.BuchangApprovalManagerSales__c);
//        }
//        if (cc.ZongjianApprovalManager__c != null) {
//            ccList.add(cc.ZongjianApprovalManager__c);
//        }
        Map<Id, User> userMap = new Map<Id, User>([
            select Id, Name, Email from User where Id in :ccList
        ]);
        // 收信者
        String toName = '';
        List<String> toMailList = new List<String>();
        toName = ra.ShippingRecieverEmailAdr__c;
        toMailList.add(ra.ShippingRecieverEmailAdr__c);

        // 原应答沟通信息
//        String oldShippingNotes = ra.ShippingNotes__c;
        // 现应答沟通信息
//        String URL = baseUrl + '/production/' + ra.Id;
        String temp = '';
        temp += ra.ShippingNotes__c;
/***
        if (oldShippingNotes != null && oldShippingNotes.trim().length() > 0) {
            temp += '\n\n' + oldShippingNotes;
        } else {
            //于2016-07-01加入<a></a>
            temp += '\n\n备品借出申请链接： ' + baseUrl + '/' + ra.Id +' ';
        }
***/
        try {
            // 更新应答沟通
            ra.ShippingNotes__c = temp;

    //        update ra;
    //        ra.ShippingNotes__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.HTMLBody = temp;
              messageNEW.Subject = '发货提醒:订单'+ ra.ContractCode__c;
            //messageNEW.PlainTextBody = temp;
            //set desired email addresses
            messageNEW.setCharset('UTF-8');
            messageNEW.toAddresses = toMailList;
 //           messageNEW.ccAddresses = ccMailList;
            //send the mail
            messaging.sendEmail(new Messaging.SingleEmailMessage[] {messageNEW});
            ra.ShippingNotes__c = '';


        } catch (Exception ex) {
            system.debug('=====' + ex.getMessage());
            hasError = true;
            ApexPages.addMessages(ex);
            return null;
        }

        return null;
    }
}