public with sharing class SearchMemberPageController {
    //页面输入检索条件
    public String searchByName{get;set;}
    public String searchByAccount{get;set;}
    public String memberType{get;set;}
    public List<LineMember> lineList{get;set;} 
    public Integer lineNum{get;set;}
    public String memberId{get;set;}
    public String memberName{get;set;}
    public String memberEmail{get;set;}
    public Boolean isError{get;set;}

    public SearchMemberPageController() {
        lineNum = Integer.valueOf(System.currentPageReference().getParameters().get('lineNum'));
    }
    public void init(){
        lineList = new List<LineMember>();
        memberType = 'Contact';
    }
    //前台点击查询
    public void SearchMember(){
        lineList.clear();
        System.debug('memberType:'+memberType);
        String soql = '';
        if(memberType == 'Contact'){

            soql = 'select Id,Name,Email,Account.Name from Contact where Id!=null ';

            if(String.isNotBlank(searchByName)){
                soql += 'and Name like \'%'+searchByName.trim()+'%\' ';
            }
            if(String.isNotBlank(searchByAccount)){
                soql += 'and Account.Name like \'%'+searchByAccount.trim()+'%\' ';
            }
            soql += 'limit 200';
            List<Contact> conList = Database.query(soql);
            if(conList!=null&&conList.size()!=0){
                for(Contact con : conList){
                    LineMember line = new LineMember();
                    line.check = false;
                    line.Id = con.Id;
                    line.name = con.Name;
                    line.email = con.Email;
                    line.account = con.Account.Name;
                    lineList.add(line);
                }
            }
        }else if(memberType == 'User'){
            
            soql = 'select Id,UserFullName__c,Email,AccountId,Account.Name from User where Id!=null ';
            if(String.isNotBlank(searchByName)){
                soql += 'and UserFullName__c like \'%'+searchByName.replaceAll(' ', '')+'%\'';
            }
            soql += 'limit 200';
            System.debug(soql);
            List<User> userList = Database.query(soql);
            if (userList!=null && userList.size()!=0) {
                for (User user:userList) {
                    LineMember line = new LineMember();
                    line.check = false;
                    line.Id = user.Id;
                    line.name = user.UserFullName__c;
                    line.email = user.Email;
                    line.account = user.Account.Name;
                    lineList.add(line);
                }
            }
        }
    }
    //前台点击保存，
    public void save(){
        isError = false;
        String errorMessage = '';
        if(lineList!=null && lineList.size()!=0){
            Integer i = 0;
            for(LineMember line : lineList){
                if(line.check){
                    memberId = line.Id;
                    memberName = line.name;
                    memberEmail = line.email;
                    System.debug(line.email);
                    i++;
                }
                if(i>1){    
                    isError = true;
                    errorMessage = '选择人数大于1！';
                }
            }
            if(i == 0){
                isError = true;
                errorMessage = '请选择一条数据!';
            }
            if(isError){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, errorMessage));
            }
        }

    }

    /*public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Contact','Contact'));
        options.add(new SelectOption('User','User'));
        return options;
    }*/

    public class LineMember{
        public Boolean check{get;set;}
        public String Id{get;set;}
        public String name{get;set;}
        public String email{get;set;}
        public String account{get;set;}
    }
}