define([
    "dojo/_base/declare",
    "dijit/_WidgetBase",
    "dijit/_TemplatedMixin",
    "dijit/_WidgetsInTemplateMixin",
    "dojo/_base/lang",
    "dojo/_base/array",
    "dojo/dom-construct", 
	"dojo/request/xhr",
	"dojo/parser", 
	"dijit/registry",
    "dojo/text!./templates/GridRow.html"
], function(declare, _WidgetBase, _TemplatedMixin,_WidgetsInTemplateMixin, lang,array, domConstruct, xhr, parser, reg, template) {

    return declare("gridRow",[_WidgetBase, _TemplatedMixin,_WidgetsInTemplateMixin], {
        templateString: template,
        headerClass:"",
		readonly: true,
    
		constructor: function(instance,context_id,readonly,parentID,parent_context_id,formid){
			this.inherited(arguments);
			var _this=this;
			//console.log("gridRow construct ",this.id);
			this.instance_id = instance.instance_id;
			this.subject_id = instance.subject_id;
			this.contextId = context_id;
			this.readonly = readonly;
			this.parent_context_id = parent_context_id;
			this.formid = formid;
			var _this=this;
			this.xhrArgs = {
				url: "cheyenne?cddid=xam.instance_addedit&P_INSTANCE_ID="+this.instance_id+"&P_CONTEXT_ID="+this.contextId+"&P_READONLY="+this.readonly+"&cmode=xam_components/questionnaire_row&P_ROW_ID="+instance.row_id+"&P_FORMID="+this.formid, 
				handleAs:'text',
				sync:false,
				preventCache:true,
				headers:{'Content-encoding':'UTF-8'},
				load: function(data){
					var tds = domConstruct.toDom(data).childNodes;
					array.forEach(tds,function(node){
						if(typeof node != 'undefined'){
							domConstruct.place(lang.clone(node), _this.rowNode.lastChild, "before");	
						}
					});
					//in readonly momdus, remove delete butten and add a link to open instance in add/edit dialog
					if(_this.readonly){
						var link = domConstruct.toDom('<td><a class="icon fa fa-external-link" onclick="KPWindowShowOverlay(\'cheyenne?cddid=xam.instance_addedit&P_CONTEXT_ID='+_this.parent_context_id+'&P_INSTANCE_ID='+_this.instance_id+'&P_READONLY='+_this.readonly+'&cmode=kpwindow_dialogborder_overlay&OVERWRITE_DEFAULT_TARGET_DIAGCONTROLS=kpwindowoverlay\');" /></td>');
						domConstruct.place(link, _this.rowNode.lastChild, "before");
						domConstruct.destroy(_this.rowNode.lastChild);
					}
					
					var dot = domConstruct.toDom('<td><img src="images/block.gif" width="5px" height="5px" /></td>');
					domConstruct.place(dot, _this.rowNode.firstChild, "before");
					
					//do not run on kpwindow, but just parse the new rowNode for dojo widgets.
					//KPWindowRunDojo(KPWindowFindParent(_this.rowNode));
					parser.parse(_this.rowNode);
					KPWindowRunComponentJS(KPWindowFindParent(_this.rowNode));
				},
				error: function(error){
					console.error("Cthulhu",error);
				}
			};
		},
		
		startup:function(){
			//console.log("gridRow start ",this.id);
			this.inherited(arguments);
			var _this=this;
			var deferred = dojo.xhrGet(this.xhrArgs);
		},
	
    	removeMe:function(){
    		this.getParent().removeRow(this.id,this.instance_id);
    	},
		
		getDescendants: function(){
			//backward compatebillety for formmanager, use getChilderen from _widgetBase instead of getDescendants from _widget (deprecated).
			//this.getChilderen() returns empty array. return own gridRow array.
			return this.getChilderen();
		}
        
    });

});