var DataTableSampleDSFactory = SuperWidget.extend({

	myTable : null,
	mydata : [],
	tableData : null,

	bindings : {
		local : {
			'datatable-selected' : [ 'click_selected' ]
		},
		global : {}
	},

	init : function() {
		var that = this;
		this.loadTable();
		setInterval(function(){
			that.eventDblClickItem();
		}, 500);
	},

	loadTable : function() {
		var that = this;
		var datasetReturned = DatasetFactory.getDataset("colleague", null, null, null);
		that.myTable = FLUIGC.datatable("#idtable" + "_" + this.instanceId, {
			dataRequest : datasetReturned.values,
			renderContent : [ ['colleaguePK.colleagueId'], 'colleagueName', 'mail' ],
			header : [ {
				'title' : 'ID',
				'size' : 'col-md-4'
			}, {
				'title' : 'Nome',
				'standard' : true,
				'size' : 'col-md-4'
			}, {
				'title' : 'Email',
				'size' : 'col-md-4'
			} ],
			search : {
				enabled : true,
				onlyEnterkey : true,
				searchAreaStyle : 'col-md-5',
				onSearch : function(res) {
					that.myTable.reload(that.tableData);
					if (res) {
						var data = that.myTable.getData();
						var search = data.filter(function(el) {
							return el.colleagueName.toUpperCase().indexOf(
									res.toUpperCase()) >= 0;
						});
						that.myTable.reload(search);
					}
				}
			},
			scroll : {
				target : ".target",
				enabled : true
			},
			actions : {
				enabled : true,
				template : '.mydatatable-template-row-area-buttons',
				actionAreaStyle : 'col-md-6'
			},
			navButtons : {
				enabled : false,
				forwardstyle : 'btn-warning',
				backwardstyle : 'btn-warning'
			},
			draggable : {
				enabled : false
			},
		}, function(err, data) {
			if (err) {
				FLUIGC.toast({
					message : err,
					type : 'danger'
				});
			}
		});

		that.myTable.on('fluig.datatable.loadcomplete', function() {
			that.eventDblClickItem();
			if (!that.tableData) {
				console.log(that.myTable);
				console.log(that.myTable.getData());
				that.tableData = that.myTable.getData();
			}
		});
	},

	selected : function(el, ev) {
		var index = this.myTable.selectedRows()[0];
		var selected = this.myTable.getRow(index);
		var myModal = FLUIGC.modal({
			title : "Informações",
			content : "<lable>Nome: </label> " + selected.colleagueName + "</br>" +
					"<lable>Email :</label> " + selected.mail + "</br>" +
					"<lable>Nº Empresa: </label> " + selected['colleaguePK.companyId'] + "</br>" +
					"<lable>Perfil: </label><a href='\social\\" + selected['colleaguePK.colleagueId'] + "'>Link</a></br>",
			id : 'fluig-modal',
			actions : [ {
				'label' : 'Fechar',
				'autoClose' : true
			} ]
		}, function(err, data) {
			if (err) {
				// do error handling
			} else {
				// do something with data
			}
		});

	},
	
	eventDblClickItem : function(){
		var that = this;
		$("#idtable" + "_" + that.instanceId + " tbody tr").dblclick (function(){
			that.selected();
		});
	}

});
