public with sharing class SI_Opportunity_ResponseController {
	public String opporID{get;set;}
	public String MailDetail{get;set;}
	public String Cc{get;set;}
	public String MailTarget {get;set;}
	public List<Opportunity> oppoList {get;set;}
	public Opportunity oppEle {get;set;}
	public String isShowFlg {get;set;}
	public String Ccid {get;set;}
	public String mailType {get;set;}
	public String mailStatus {get;set;}
	public SI_Opportunity_ResponseController() {
		
	}
	public SI_Opportunity_ResponseController(ApexPages.StandardController stdController) {
		List<Opportunity> oppoList = new List<Opportunity>();
		opporID = stdController.getId();
		if(opporID==null||opporID == ''){
			opporID = ApexPages.currentPage().getParameters().get('id');
		}
		
		isShowFlg = ApexPages.currentPage().getParameters().get('showFlg');
		mailType = ApexPages.currentPage().getParameters().get('mailType');
	}
	public void init(){
		mailStatus = 'Fin';
		oppoList = [SELECT name,Opportunity_No__c,Owner.Email,response__c,Mail_Tar_Selector__c from Opportunity where id =:opporID];
    	if(isShowFlg==''||isShowFlg==null){
    		isShowFlg = 'true';
    	}
    	if(oppoList.size()==1){
			oppEle = oppoList[0];
		}
	}
	public void initPop(){
		oppoList = [SELECT name,Opportunity_No__c,Owner.Email,response__c,Mail_Tar_Selector__c from Opportunity where id =:opporID];
	}
	public void sendMail(){
		if(mailType=='SI'){
			MailTarget = System.label.SI_Group_MailAdd+' <SI部门> ';
		}else if(mailType=='apply'){
			MailTarget = oppEle.Owner.Email+' <申请者>';
		}
		Datetime dt = Datetime.now();
		String temp = '';
		temp += '\n      '  + ' ' + dt.format() + '       \n';
		temp += '    From：' + UserInfo.getUserEmail() +'       \n';
		temp += '    To：' + MailTarget + '\n';
		temp += '    Cc：' + Cc + '\n';
		temp += MailDetail +'\n';
		temp += '============================================================================';
		if(oppoList.size()==1){
			oppEle = oppoList[0];
			if(oppEle.response__c == null){
				oppEle.response__c = temp;
			}else{
				oppEle.response__c += temp;
			}
			
		}
		savepoint sp = Database.setsavepoint(); 
		try{
			update oppoList;
			Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage();
			system.debug(opporID+'233333333');
			messageNEW.Subject =  oppEle.Name + '询价应答：'+oppEle.Opportunity_No__c+' \n' ;
			messageNEW.PlainTextBody = temp;
			//set desired email addresses
			messageNEW.setCharset('UTF-8');
			List<String> idList = Ccid.split(',');
			List<user> UserEmail = [SELECT Id, Name, Email from user where id in :idList];
			List<String> MailCc = new List<String>();
			for(User us : UserEmail){
				MailCc.add(us.Email);
			}
			//messageNEW.toAddresses = toMailList;
			system.debug('==*****==' + mailType+''+oppEle.Owner.Email);
			List<String> MailAdd = new List<String>();
			String addLine = mailType=='SI'?System.label.SI_Group_MailAdd:oppEle.Owner.Email;
			MailAdd.add(addLine);
			//system.debug('==$$$$==' + mailType+''+oppEle.Owner.Email);
			messageNEW.toAddresses = MailAdd;
			//system.debug('==$$$$==' + mailType+''+oppEle.Owner.Email);
			messageNEW.ccAddresses = MailCc;
			//send the mail
			system.debug('==*****==' + messageNEW);
			Messaging.SendEmailResult[] results = messaging.sendEmail(new Messaging.SingleEmailMessage[] {messageNEW});
			if(!results[0].success){
				system.debug('=====' + results[0].errors[0].message);
				Database.rollback(sp);
				//hasError=true;
				mailStatus = 'Denied';
			}else{
				//邮件发送成功
				//hasError=false;
			}
		} catch (Exception ex) {
			Database.rollback(sp);
			mailStatus = 'Denied';
			system.debug('=====' + ex.getMessage());
			//hasError = true;
			ApexPages.addMessages(ex);
		}
	}
}