public class SearchSetProductController {
	
    public List<productSet__c> cl {get;set;}

	public String SearchName {get;set;}
	public String SearchCode {get;set;}

	public Integer activitiesSize {get;set;}
	public List<QELine> activities {get;set;}
	public String setPFString {get;set;}
    public String gainQuoteSetNameAndSetQty;
    public Map<String,Decimal> setNameAndSetQtyMap;
    public Map<String,Decimal> setNameAndSetDiscountMap;
	public SearchSetProductController(){
		this.setNameAndSetQtyMap = new Map<String,Decimal>();
        this.setNameAndSetDiscountMap = new Map<String,Decimal>();
        cl = new List<productSet__c>();
        //获取从前一个页面传过来的"产品配套名称"和"产品配套数量"
        this.gainQuoteSetNameAndSetQty = Apexpages.currentPage().getParameters().get('gainQuoteSetNameAndSetQty');
        system.debug('gainQuoteSetNameAndSetQty_Test_0_' + gainQuoteSetNameAndSetQty);
        if (gainQuoteSetNameAndSetQty.length() >0) {

            for (String setNameAndSetQty : gainQuoteSetNameAndSetQty.split(';')) {
                setNameAndSetQtyMap.put(setNameAndSetQty.split(',')[0],Decimal.valueOf(setNameAndSetQty.split(',')[1]));
                setNameAndSetDiscountMap.put(setNameAndSetQty.split(',')[0], Decimal.valueOf(setNameAndSetQty.split(',')[2]));
            }
        }
        system.debug('setNameAndSetQtyMap_Test_0_' + setNameAndSetQtyMap);
	}
  public SearchSetProductController(ApexPages.StandardController controller){
    this();    
  }

  public void init(){
    system.debug('gainQuoteSetNameAndSetQty_Test' + gainQuoteSetNameAndSetQty);
    system.debug('setNameAndSetQtyMap_Test' + setNameAndSetQtyMap);
    activitiesSize = 0;
    
    searchproductSet();
    activitiesSize = activities.size();
  } 

    public PageReference searchproductSet() {

        //查找有效的"产品配套"
        cl = [Select id, Name,Code__c From productSet__c Where  IsValid__c = true order by Name];

        activities = new List<QELine>();
        List<QELine> selectedActivities = new List<QELine>();
        //将上个页面传过来的产品配套选中，并放到前面
        for(productSet__c psl : cl){            
            if (setNameAndSetQtyMap.containsKey(psl.Name)) {
                QELine a = new QELine(psl);
                a.Quantity =  setNameAndSetQtyMap.get(psl.Name);
                a.QuantityReference = setNameAndSetQtyMap.get(psl.Name);
                a.Discount = setNameAndSetDiscountMap.get(psl.Name);
                a.isSelected = true;
                selectedActivities.add(a);  
            } 
        
        }
        for(productSet__c psl : cl){
            
            if (!setNameAndSetQtyMap.containsKey(psl.Name)) {
                QELine a = new QELine(psl);
                a.Quantity = 0;
                a.QuantityReference = 0;
                a.Discount = 0 ;
                selectedActivities.add(a); 
            } else {
                continue;
            } 
        }
        activities = selectedActivities;
        return null;   
    } 

    public PageReference serContact(){
        
        String searchSql = 'Select id,Code__c,Name From productSet__c ';
        String whereStr = 'Where  IsValid__c = true ';
        String whereSql = '';

        if(SearchName != null && SearchName != ''){
            whereSql += ' and Name Like ' + '\'%' + SearchName + '%\' ';
        }

        if(SearchCode != null && SearchCode != ''){
          whereSql += 'and Code__c like ' + '\'%' + SearchCode + '%\' ';
        }

        searchSql = searchSql + whereStr + whereSql;

        searchSql += ' order by Code__c limit 500';

        cl = Database.query(searchSql);

        Map<String,QELine> selectedQELineMap = new Map<String,QELine>();
        if (activities.size() >0 ) {
            for (QELine  q :activities) {
                if (q.isSelected == true) {
                    selectedQELineMap.put(q.setProductidFull, q);
                } 
            }
        }

        for(productSet__c psl : cl){
            
            if (!setNameAndSetQtyMap.containsKey(psl.Name)) {
                QELine a = new QELine(psl);
                if (selectedQELineMap.containsKey(a.setProductidFull)) {
                    continue;
                } else {
                    a.Quantity = 0 ;
                    selectedQELineMap.put(a.setProductidFull, a);
                }
                
            } else {
                continue;
            }
            
            
        }
        activities = selectedQELineMap.values();
        return null;
  }
  public void SelectDone(){
    setPFString = '';
    String idAndQuantity ;

    for (QELine arc :activities) {

        if(arc.isSelected==true){
            
            if (arc.Quantity > 0) {
                idAndQuantity = arc.setProductidFull +','+ arc.Quantity +','+ arc.Discount;
                
                if(String.isblank(setPFString)||setPFString==null){
                    
                    setPFString = idAndQuantity;
                }else{
                    
                    setPFString = setPFString +';'+ idAndQuantity;
                }
            }
            
        }
    }
    
  }

    public class QELine {
        public Boolean isSelected {get;set;}
        public String setProductidFull { get; set; }
        public Decimal Quantity { get; set; }
        public Decimal QuantityReference { get; set; }
        public Decimal Discount { get; set; }
        public String Name { get; set; }       
        public String Code { get; set; }

        public QELine(productSet__c psl) {
            isSelected = false;
	        setProductidFull = psl.id;
	        Name = psl.Name;
            Code = psl.Code__c;
	        
        }
    }
}