public with sharing class OrderSearchProductController {
    public List<productInfo> productInfoList { get; set; }
    // 判断追加按钮什么时候不可以按
    public Boolean getHascl() {
        Boolean rtn = true;
        if (productInfoList != null && productInfoList.size() > 0) {
            rtn = false;
        }
        return rtn;
    }
    // 选择的行
    public String lineNo {get;set;}
    // 通过型号检索产品
    public String ProModel { get; set; }

    public OrderSearchProductController(ApexPages.StandardController controller){
        this();
    }
    public OrderSearchProductController() {
        this.lineNo = Apexpages.currentPage().getParameters().get('lineno');

        productInfoList = new List<productInfo>();
    }

    // public void init() {
    //     searchHospital();
    // }
    public String makeSoql(String ProModel){
        String soql = 'select Id, Name, ProductCode, Packing_list_manual__c, SFDA_Expiration_Date__c, Asset_Model_No__c FROM Product2 Where Whether_Sample__c = true and SFDA_Status__c != \'停止\' ';
        if(String.isNotBlank(ProModel)){
            soql += ' and Asset_Model_No__c like \'%' + ProModel + '%\'';
        }
        soql += ' limit 200';
        return soql;
    }
    
    //检索
    public PageReference searchHospital() {
        productInfoList = new List<ProductInfo>();
        String soql = this.makeSoql(ProModel);
        List<Product2> productSelected = Database.query(soql);

        System.debug('lt123ProModel'+ProModel);
        System.debug('lt123sql'+soql);
        System.debug('lt123productSelected'+productSelected);

        // for(Product2 Pros : productSelected){
        //     productInfoList.add(new ProductInfo(Pros));
        // }
        // productInfoList = new List<productInfo>();
        // if( productSelected.size() > 1000 ){
        //     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '超出检索限制'));
        //     return null;
        // }
        for (Integer i = 0; i < productSelected.size(); i++) {
            productInfo pl = new productInfo(i, productSelected[i]);
            productInfoList.add(pl);
        }
        
        if( productSelected.size() <= 0 ){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '没检索到任何样本品'));
            return null;
        }
        return null;
    }
    //实体类
    public class productInfo {
        public Integer idx {get; set;}
        public Product2 pro { get; set; }
        public productInfo(Integer i, Product2 pro) {
            this.idx = i;
            this.pro = pro;
        }
    }
}