global with sharing  class LostByCompany {
    public List<retLine> results {get; private set;}
    public String lineNo {get;set;}
    public String SearchName {get;set;}   
    public String queryCopy {get;set;}   
    public Boolean getIsOverLimit() {
        if (results != null && results.size() > 50) {
            return true;
        }
        return false;
    }
    public LostByCompany() {
        this.results = new List<retLine>();
    }

    // 获取列表值
    /*
    public void search() {
        String sname = 'PCLLostBrand__c';
        String fname = 'Lost_By_Company__c';
        List<String> retlist = new List<String>();
        Map<String, Schema.SObjectField> sof;
        List<SObject> so = Database.query('Select Id from ' + sname +  ' limit 1');
        if (so.size() > 0) {
            sof = so[0].getSObjectType().getDescribe().fields.getMap();
        } else {
            sof = Schema.getGlobalDescribe().get(sname).newSObject().getSObjectType().getDescribe().fields.getMap();
        }
        if (!sof.containsKey(fname)) this.results =  null;
        Schema.DescribeFieldResult dfr = sof.get(fname).getDescribe();
        String ftype = dfr.getType().name();
        if (ftype != 'PICKLIST' && ftype != 'MULTIPICKLIST') this.results =  null;

        for (Schema.PicklistEntry pe : dfr.getPicklistValues()) {
            String val = '';
            val = pe.getLabel(); 
            if (pe.isDefaultValue()) val += '（デフォルト）';
     
            if (!pe.isActive()) val += '（無効）';
            retlist.add(val);
        }
        this.results =  retlist;
    }
    */

    // SWAG-CJNAJG 获取列表的值 上面那个太复杂了 换一种写法
    public void search() {
        
        // 增加一个输入参数的筛选
        String query = System.currentPageReference().getParameters().get('q');
        String line_no = System.currentPageReference().getParameters().get('l');
        query = query.trim();
        SearchName=query;
        queryCopy=query;
        this.lineNo = String.isNotBlank(line_no) ? line_no : '';
        this.results =  searchLogic(query);
    }
    public PageReference searchOne() {
        system.debug('SearchName++'+SearchName);
        this.results =  searchLogic(SearchName);
        return null;
    }
    public List<retLine> searchLogic(String query){
        // 初始化参数
        List<retLine> retlist = new List<retLine>();
        system.debug('query++'+query);
        // 获取对象结构
        Map<String, Schema.SObjectField> sof = Schema.getGlobalDescribe().get('PCLLostBrand__c').getDescribe().fields.getMap();
        // 获取选项列表
        List<Schema.PicklistEntry> brandPLEntries = sof.get('Lost_By_Company__c').getDescribe().getPicklistValues();
        // 循环列表
        Integer i = 0;
        for (Schema.PicklistEntry entry : brandPLEntries) {
            system.debug('111++'+(String.isNotBlank(query) && !entry.getValue().contains(query)));
            system.debug('222++'+!entry.isActive());
            // 跳过被禁用的选项，跳过和输入值不匹配的选项
            if (!entry.isActive() || (String.isNotBlank(query) && !entry.getValue().contains(query))) {
                continue;
            }
            retLine ret = new retLine();
            ret.idx=i;
            ret.retvalue=entry.getValue();
            system.debug('ret++'+ret);
            retlist.add(ret);
            i++;
        }
        system.debug('retlist++'+retlist);
        return retlist;
    }
    public class retLine {
        public Integer idx {get; set;}
        public String retvalue {get; set;}
    }

}