public with sharing class MessageBoardController {
    String oldResponse;
    // 询价
    public User_FaultInfo__c userfaultInfo { get; set; }
    public User_FaultInfo__c userfaultInfoPoPuP { get; set; }
    // CaseId
    public Id caseId { get; private set; }

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

    public MessageBoardController() {
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        caseId = ApexPages.currentPage().getParameters().get('caseId');
    }

    public MessageBoardController(ApexPages.StandardController stdController) {
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        caseId = stdController.getId();
    }

    // 画面初始化
    public void init() {
        hasError = false;
        userfaultInfo = new User_FaultInfo__c();
        // cc = new User_FaultInfo__c();
        usertype = UserInfo.getUserType();
        List<User_FaultInfo__c> raList = [select Id, Name, MessageBoard__c from User_FaultInfo__c where Id = :caseId];
        if (raList.size() > 0) {
            userfaultInfo = raList[0];
        }
    }
    
     // 画面初始化-POP UP
    public void init1() {
        hasError = false;
        userfaultInfoPoPuP = new User_FaultInfo__c();
        List<User_FaultInfo__c> ra1List = [select Id, Name, MessageBoard__c from User_FaultInfo__c where Id = :caseId];
        if (ra1List.size() > 0) {
            userfaultInfoPoPuP = ra1List[0];
        }
        oldResponse = userfaultInfoPoPuP.MessageBoard__c;
        userfaultInfoPoPuP.MessageBoard__c = '';
    }



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

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

        // 送信者
        String username = UserInfo.getName();
        // 发送时间
        Datetime dt = Datetime.now();
        
        // 现留言
        String temp = '';
        temp += '****** ' + username + ' ' + dt.format() + ' ******\n';
        temp += userfaultInfoPoPuP.MessageBoard__c;
        if (oldResponse != null && oldResponse.trim().length() > 0) {
            temp += '\n\n' + oldResponse;
        }
        try {
            // 更新留言
   
            userfaultInfoPoPuP.MessageBoard__c = temp;

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

        return null;
    }
}