public class BidAnnouncementController {

    @AuraEnabled
    public static InitData BidAnnouncementController (String recordId){
        InitData res = new initData();
        try{
            Bid_Announcement__c report = [SELECT 	Id FROM Bid_Announcement__c WHERE Id = :recordId LIMIT 1];
            res.Id = report.Id;
            System.debug(LoggingLevel.INFO, '*** res: ' + res);
        }catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
        }
        return res;
    }

    //中标通知书 申请
    @AuraEnabled
    public static String ApplyFor(String recordId) {
        String messageText = '';
        // Bid_Announcement__c Bid_Announcement = new Bid_Announcement__c();
        try {
        Bid_Announcement__c Bid_Announcement = [SELECT 	Id,Status__c,Request_flag__c,Request_user__c,Request_date__c FROM Bid_Announcement__c WHERE Id = :recordId LIMIT 1];
        Bid_Announcement.Id = Bid_Announcement.Id;
        Bid_Announcement.Status__c = LightingButtonConstant.STATUS_Bid_Announcement_ApplyFor;
        Bid_Announcement.Request_flag__c = true;
        Bid_Announcement.Request_user__c = Userinfo.getUserId();
        Bid_Announcement.Request_date__c = Datetime.now();
        update Bid_Announcement;
        messageText = '1';
        return messageText;
        } catch (Exception e) {
        // System.debug(LoggingLevel.INFO, '*** ApplyForXu: ' + ex);
        // messageText = ex.getMessage();
        // return messageText;
            if(e.getMessage().contains(':')){
                String eMessage =e.getMessage();
                Integer left = eMessage.indexOf(',')+1;
                Integer right= eMessage.lastIndexOf('。 ')+1;
                if(right>eMessage.length()||right<=left){
                    right=eMessage.length();
                }
                String mes=eMessage.substring(left,right);
                messageText = mes;
                return messageText;
            }else{
                messageText=e.getMessage();
                return messageText;
            }
        }
    }

        //中标通知书 同意
        @AuraEnabled
        public static String ConSent(String recordId) {
            String messageText = '';
            try {
            Bid_Announcement__c Bid_Announcement = [SELECT 	Id,Status__c,Complete__c,Agree_user__c,Complete_date__c FROM Bid_Announcement__c WHERE Id = :recordId LIMIT 1];
            Bid_Announcement.Id = Bid_Announcement.Id;
            Bid_Announcement.Status__c = LightingButtonConstant.STATUS_Bid_Announcement_Consent;
            Bid_Announcement.Complete__c = true;
            Bid_Announcement.Agree_user__c = Userinfo.getUserId();
            Bid_Announcement.Complete_date__c = Datetime.now();
            update Bid_Announcement;
            messageText = '1';
            return messageText;
            } catch (Exception e) {
                if(e.getMessage().contains(':')){
                    String eMessage =e.getMessage();
                    Integer left = eMessage.indexOf(',')+1;
                    Integer right= eMessage.lastIndexOf('。 ')+1;
                    if(right>eMessage.length()||right<=left){
                        right=eMessage.length();
                    }
                    String mes=eMessage.substring(left,right);
                    messageText = mes;
                    return messageText;
                }else{
                    messageText=e.getMessage();
                    return messageText;
                }
            }
        }

    //中标通知书 驳回
    @AuraEnabled
    public static String Reject(String recordId) {
        String messageText = '';
        try {
        Bid_Announcement__c Bid_Announcement = [SELECT 	Id,Status__c,Complete__c,Agree_user__c,Complete_date__c,Request_flag__c,Request_user__c,Request_date__c FROM Bid_Announcement__c WHERE Id = :recordId LIMIT 1];
        Bid_Announcement.Id = Bid_Announcement.Id;
        Bid_Announcement.Status__c = LightingButtonConstant.STATUS_Bid_Announcement_Reject;
        Bid_Announcement.Complete__c = false;
        Bid_Announcement.Agree_user__c = null;
        Bid_Announcement.Complete_date__c = null;
        Bid_Announcement.Request_flag__c = false;
        Bid_Announcement.Request_user__c = null;
        Bid_Announcement.Request_date__c = null;
        update Bid_Announcement;
        messageText = '1';
        return messageText;
        } catch (Exception e) {
        // System.debug(LoggingLevel.INFO, '*** Reject: ' + ex);
        // messageText = ex.getMessage();
        // return messageText;
        if(e.getMessage().contains(':')){
            String eMessage =e.getMessage();
            Integer left = eMessage.indexOf(',')+1;
            Integer right= eMessage.lastIndexOf('。 ')+1;
            if(right>eMessage.length()||right<=left){
                right=eMessage.length();
            }
            String mes=eMessage.substring(left,right);
            messageText = mes;
            return messageText;
        }else{
            messageText=e.getMessage();
            return messageText;
        }
        }
    }


    //获取当前登录人的 id
    @AuraEnabled
    public static UserResult UserInfo_Owner() {
        UserResult result = new UserResult();
        ID myUserID = UserInfo.getUserId();
        try { 
            User tempUser =
                [select id,BidCancel__c from user where id = : myUserID ];
            result.id = tempUser.id;
            result.BidCancel = tempUser.BidCancel__c;
        } catch (exception e) {
            
            result.result = e.getMessage();
        }
        return result;
    }

    public class InitData{
        @AuraEnabled
        public String Id;
    }
    public class UserResult {
        @AuraEnabled
        public string result;
        public UserResult( ) {
            result = 'Success';
        }
        @AuraEnabled
        public string id;
        @AuraEnabled
        public Boolean BidCancel;
    }
}


//同意
// var Bid_Announcement = new sforce.SObject("Bid_Announcement__c");
// Bid_Announcement.Id = "{!Bid_Announcement__c.Id}";
// Bid_Announcement.Status__c = "处理完毕";
// Bid_Announcement.Complete__c = "true";
// Bid_Announcement.Agree_user__c = "{!$User.Id}";
// var serverTimestamp = sforce.connection.getServerTimestamp();
// Bid_Announcement.Complete_date__c = serverTimestamp.timestamp;

// var result = sforce.connection.update([Bid_Announcement]);
// var messages = getConnectDMLErrorMessages(result);
// if (messages.length > 0) {
// alert(messages.join("\n"));
// }
// location.reload();


//驳回
// if ('{!$User.BidCancel__c}' == 'false') {
//     alert("没有驳回的权限");
//     } else {
//     var Bid_Announcement = new sforce.SObject("Bid_Announcement__c");
//     Bid_Announcement.Id = "{!Bid_Announcement__c.Id}";
//     Bid_Announcement.Status__c = "草案中";
//     Bid_Announcement.Complete__c = "false";
//     Bid_Announcement.Agree_user__c = null;
//     Bid_Announcement.Complete_date__c = null;
//     Bid_Announcement.Request_flag__c = "false";
//     Bid_Announcement.Request_user__c = null;
//     Bid_Announcement.Request_date__c = null;
    
//     var result = sforce.connection.update([Bid_Announcement]);
//     var messages = getConnectDMLErrorMessages(result);
//     if (messages.length > 0) {
//     alert(messages.join("\n"));
//     }
//     location.reload();
//     }



//申请
// var Bid_Announcement = new sforce.SObject("Bid_Announcement__c");
// Bid_Announcement.Id = "{!Bid_Announcement__c.Id}";
// Bid_Announcement.Status__c = "申请中";
// Bid_Announcement.Request_flag__c = "true";
// Bid_Announcement.Request_user__c = "{!$User.Id}";
// var serverTimestamp = sforce.connection.getServerTimestamp();
// Bid_Announcement.Request_date__c = serverTimestamp.timestamp;

// var result = sforce.connection.update([Bid_Announcement]);
// var messages = getConnectDMLErrorMessages(result);
// if (messages.length > 0) {
// alert(messages.join("\n"));
// }
// location.reload();