package flexa.ff;
import flexa.fb.*;
import java.util.Map;
import java.util.HashMap;
import javax.script.*;
public class combo extends FComponent {
    //
    // CONSTRUCTOR
    //
    public combo() {
        super("ff.combo");
    }
    
    // 
    // Methods
    //
    
    
    public String getCompleteName()  { 
    		if( getValue("relation") != null && !(((String)getValue("relation")).trim().equals("")) ) {
		    return ((String)getValue("relation")).trim();
		} else {
		    return "";
		    //return this.module + "." + this.name;
		}

    }
    


    // 
    // Events
    //
	public void event(String aAction, FContext aContext) throws Exception {
        
    
        // onopen EVENT
        if( LibText.stringToArray("" + this.name + ".refresh," + this.name + ".select,start,onopen,refresh").contains(aAction) ) {
        			String tds = (String)getValue("datasource");
			    String trel = (String)getValue("relation");
			    String tuds = (String)getValue("_datasource");
			    String ttype = (String)getValue("type");
			    Log.info("COMBO " + ttype + " #" + (String)getValue("id") + " name "  + (String)getValue("name") + " event " + aAction + " ds " + tds + " rel " + trel + " uds " + tuds);
			    if( ttype != null && ttype.indexOf("autocomplete") >= 0 && !aAction.endsWith(".select") ) {
			        setValue("datasource", "/flexa/c?_c=" + aContext.mainComponent.getModule() + "/" + aContext.mainComponent.getName() +  "&_x=" + (String)getValue("id") + ".select");
			        Log.info("COMBO autocomplete dont fill with event " + aAction);
			    } else {
			        if( (tds != null && tds.toLowerCase().startsWith("select")) || (tuds != null && tuds.toLowerCase().startsWith("select")) ) {
				        if( tuds != null && tuds.toLowerCase().startsWith("select") ) tds = tuds;
				        Log.getInstance().info("COMBO event " + aAction + " datasource : " + tds );
			            FResult result = Util.sqltoarrayddtotal(aContext, tds);
			            if( aAction.endsWith(".select") ) {
			                aContext.hash.put( ((String)getValue("name")) + ".select", result);
			            } else {
			                setValue("_datasource", tds);
			                setValue("datasource", result.toJsonMatrix());
			            }
				    } else if( trel != null && !trel.trim().equals("") ) {
				        String tRel = trel.trim();
				        String tRelFilter = ((String)getValue("relationfilter"));
				        FClass tClassRel = ORM.getInstance().getFClass(tRel);
				        String tSql = tClassRel.generateSql(FClass.QUERY_ONLIST); //generateSqlSelectList(tRelFilter);
				        Log.getInstance().info("COMBO event " + aAction + " sql : " + tSql);
				        FResult result = Util.sqltoarrayddtotal(aContext, tSql);
			            
			            // Json
			            if( aAction.endsWith(".select") ) {
			                aContext.hash.put( ((String)getValue("name")) + ".select", result);
			            } else {
			                setValue("datasource", result.toJsonMatrix());
			                setValue("_datasource", "[relation]");
			            }
				    }
			    }

        }
    


	    super.event(aAction, aContext);
	}
	
	
	
	
}