public with sharing class SWOSearchProductContaroller {
    public Integer quoteEntryMaxSet {get; private set;}

    public List<ProductInfo> productInfoList { get; set; }

    public Integer ProductInfoListSize {get; set;}
   
    public String searchMode;                         // 検索モード
    public String lineNo {get;set;}                   // 親画面の選択した行
    public String val {get;set;}
    public String type {get;set;}                     //主数据类型
    public String SearchName {get;set;}               // 検索文字列
    
    public SWOSearchProductContaroller(ApexPages.StandardController controller){
        this();
    }

    public SWOSearchProductContaroller() {
        this.quoteEntryMaxSet = Integer.valueOf(System.Label.QuoteEntryMaxSet);
        
        this.lineNo = Apexpages.currentPage().getParameters().get('lineno');
        this.val = Apexpages.currentPage().getParameters().get('val');
        this.type = Apexpages.currentPage().getParameters().get('type');
        if (String.isNotBlank(val)) {
            SearchName = val;
            val = null;
        }
        productInfoList = new  List<ProductInfo>();
        ProductInfoListSize = 1;
    }

    public void init () {
        system.debug('init==========> start');
        system.debug('SearchName==========>' + SearchName);
        if (SearchName != null && SearchName != '') {
            serProduct();
        }

        system.debug('init==========> end');

    }

    public PageReference serProduct(){
        productInfoList = new  List<ProductInfo>();
        ProductInfoListSize = 1;
        if (String.isNotBlank(SearchName)) {

             String sql = 'Select Id,Name,ProductCode,Description,PART_NUMBER__c,ProductInventory__c,ProductReferencePrice__c,Product_ECCode__c from Product2 Where Id != null ';
            String likename = String.escapeSingleQuotes(SearchName);
            String prdcd = String.escapeSingleQuotes(SearchName);
            String prdec = String.escapeSingleQuotes(SearchName);
            if (likename.indexOf('*') >= 0) {
                likename = likename.replace('*', '%');
            } else {
                likename = '%' + likename + '%';
            }
            if (prdcd.indexOf('*') >= 0) {
                prdcd = prdcd.replace('*', '%');
            } else {
                prdcd = '%' + prdcd + '%';
            }
            if (prdec.indexOf('*') >= 0) {
                prdec = prdcd.replace('*', '%');
            } else {
                prdec = '%' + prdec + '%';
            }
           
            sql += ' and (Name Like ' + '\'' + likename + '\' or ProductCode Like ' + '\'' + prdcd + '\' or Product_ECCode__c like ' + '\'' + prdec + '\')';
            sql += ' order by ProductCode,Name desc limit 200';
            List<Product2> product2List = Database.query(sql);
            if (product2List.size() > 0) {
                for (Product2 product :product2List) {
                    productInfoList.add(new ProductInfo(ProductInfoListSize,product));
                    ProductInfoListSize++;
                }
            }
            
        }
        return null;
    }
    public class ProductInfo {

        public Product2 productPage {get; set;}//产品
        public String productId {get; set;} //产品Id
        public Integer lineNumber {get; set;}//行号
        
        public ProductInfo (Integer productNo){
            lineNumber = productNo;
        }
        public ProductInfo ( Integer productNo,Product2 product) {
            productPage = product;
            lineNumber = productNo;
            productId = product.Id;
        }
    }
}