public with sharing class LexSISearchSetProductController {


	public LexSISearchSetProductController(){

	}
	@AuraEnabled
	public static InitData init(){
		InitData data=new InitData();
		data.rc = new Product_Set__c();
		data.sc = new Product_Set__c();
		data.cl = new List<Product_Set__c>();
		data.baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
		data.SearchName='';
		data.SearchCode='';
		data.SearchPrice='';
		data.SearchQuantity='';
		data.reportid='';
		data.setProductidFullList=new list<String>();
		data.activities=new List<SSPLine>();
		data.setPFString='';
		data.DataStatus='';
		data.LabelI='';
		Report r = new Report();
		List<Report> rs = new List<Report>();
		rs=[Select id,DeveloperName,Name,NamespacePrefix from Report Where DeveloperName='Set_Product_Detail'];
		if (rs.size()>0){
			data.reportid = rs[0].id;
			data.reportid = data.reportid.substring(0,15);
		}
		data.LabelI=Product_Set__c.Quote_Select_Info__c.getDescribe().getLabel();
		System.debug('init: '+data);
		return data;
	}

	@AuraEnabled
	public static InitData serContact(String searchName,String searchCode){
		InitData data=new InitData();

		String searchSql = 'Select id, name, Price__c, Product_Set_CD__c, Quantity__c,Valid_Status__c,Quote_Select_Info__c From Product_Set__c ';
		String whereStr = 'Where id != null ';
		String whereSql = '';

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

		if(searchCode != null && searchCode != ''){
			whereSql += 'and Product_Set_CD__c like ' + '\'%' + searchCode + '%\' ';
		}
/*
*/
		searchSql = searchSql + whereStr + whereSql;

		searchSql += ' order by Product_Set_CD__c limit 500';

		data.cl = Database.query(searchSql);
		data.activities = New List<SSPLine>();
//    SSPLine a = new SSPLine();
		for(Product_Set__c psl : data.cl){
			SSPLine a = new SSPLine(psl);
			data.activities.add(a);
		}
		return data;
	}

	@AuraEnabled
	public static InitData SelectDone(List<String> activities,List<Boolean> isSelected){
		try{
			InitData data=new InitData();
			data.setPFString = '';
			data.setProductidFullList = New List<String>();
			if(activities.size()==1){
				data.setPFString = activities[0];
			}else{
				for(Integer i=0;i<activities.size();i++){
					if(isSelected[i]==true){
						if(String.isblank(data.setPFString)||data.setPFString==null){
							data.setPFString = activities[i];
						}else{
							data.setPFString = data.setPFString+','+activities[i];
						}
						
					}
				}
			}
			data.DataStatus = 'Fin';
			return data;
		}catch(Exception e){
			System.debug('SelectDone error:'+e);
		}
		
		return null;
	}

	@AuraEnabled
	public static String getFileName(){
       	Schema.DescribeFieldResult r = Product_Set__c.Quote_Select_Info__c.getDescribe();
		return r.getLabel();
	}

	public class InitData{
		@AuraEnabled
	    public Product_Set__c rc;
		@AuraEnabled
		public Product_Set__c sc;
		@AuraEnabled
		public List<Product_Set__c> cl;
		@AuraEnabled
		public String SearchName;
		@AuraEnabled
		public String SearchCode;
		@AuraEnabled
		public String SearchPrice;
		@AuraEnabled
		public String SearchQuantity;
		@AuraEnabled
		public String baseUrl;
		@AuraEnabled
		public String reportid;
		@AuraEnabled
		public List<String> setProductidFullList;
		@AuraEnabled
		public List<SSPLine> activities;
		@AuraEnabled
		public String setPFString;
		@AuraEnabled
		public String DataStatus;
		@AuraEnabled
		public String LabelI;
	}

	public class SSPLine {
		@AuraEnabled
		public Boolean isSelected;

		@AuraEnabled
		public String setProductid;

		@AuraEnabled
		public String setProductidFull;

		@AuraEnabled
		public Decimal Quantity;

		@AuraEnabled
		public Product_Set__c theObject;

		@AuraEnabled
		public String Name;

		@AuraEnabled      
		public String Product_Set_CD_c;

		@AuraEnabled
		public String Quote_Select_Info_c;

		@AuraEnabled
		public Boolean Valid_c;

		public SSPLine(Product_Set__c psl) {
			isSelected = false;
			setProductid = psl.id;
			setProductid = setProductid.substring(0,15);
			setProductidFull = psl.id;
			Name = psl.Name;
			Product_Set_CD_c= psl.Product_Set_CD__c;
			Quantity = psl.Quantity__c;
			Quote_Select_Info_c = psl.Quote_Select_Info__c;
			Valid_c = psl.Valid_Status__c;
		}
	}
}