/*******************************************************************************
 * This file is part of Educatio.
 *
 *     Educatio is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Educatio is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Educatio.  If not, see <http://www.gnu.org/licenses/>.
 *     
 *     Authors:
 *           Filipe Marinho de Brito - filipe.marinho.brito@gmail.com
 *           Rodrigo de Souza Ataides - rodrigoataides@gmail.com
 *******************************************************************************/
$(document).ready(function() {
	
	//propriedades do datatable
	var id = "listarBanco";
	var url = "Banco";
	var sInfoComplemento = "Banco";

	//sql
	var table = 	"public.banco";
	var where = " where 1=? and 2=? ";
	var argumento = "1|2";
	var tipoArgumento = "Integer|Integer";
	var order = " order by id desc ";
	
	//cria Banco buttonset
	$("#listarBanco_btnNovo").button({icons: {primary: 'ui-icon-plusthick'}, text: true});
	$("#listarBanco_btnFiltrosAvancados").button({icons: {primary: 'ui-icon-circle-zoomout'}, text: true});
	$("#listarBanco_btnRecarregar").button({icons: {primary: 'ui-icon-arrowreturnthick-1-w'}, text: true});
	$("#listarBanco_btnPDF").button({icons: {primary: 'ui-icon-pdf'}, text: true});
	$("#listarBanco_btnPDF").click(function(){
		openNewPagePost(url+"/bServerSide/pdf", montarParametrosServidor(id, colunas, table, where, argumento, tipoArgumento, order, "${encoded}"));
	});
	$("#listarBanco_btnXLS").button({icons: {primary: 'ui-icon-xls'}, text: true});
	$("#listarBanco_btnXLS").click(function(){
		openNewPagePost(url+"/bServerSide/xls", montarParametrosServidor(id, colunas, table, where, argumento, tipoArgumento, order, "${encoded}"));
	});
	
	
	var aoColumns = [
					{"sTitle": "-", "mDataProp": "htmlControl","bSortable": false, "sWidth": "20px", "bVisible": true, "sDefaultContent":"","sType": "html", 
					 	"fnRender": function ( oObj ) {
					 		if(  $("#Banco_listar_popUp").attr('innerObject') === 'true' ){
					 			return "<button type=\"button\" id=\"checkBtn_"+oObj.aData.id+"\" class=\"checkBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" onclick=\"selecionarPopUpListarBanco(event)\" role=\"button\" aria-disabled=\"false\" title=\"Selecionar\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-check\"></span><span class=\"ui-button-text\">Selecionar</span></button>";
					 		}else{
					 			return  "<a id=\"printBtn_"+oObj.aData.id+"\" class=\"printBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" href=\""+url+"/relatorio/"+oObj.aData.id+"?formato=pdf\" target=\"_blank\" title=\"Relatório\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-print\"></span><span class=\"ui-button-text\">Rel</span></a>" +
	                			 "<button type=\"button\" id=\"editarBtn_"+oObj.aData.id+"\" class=\"editarBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" onclick=\"editar(event,'"+url+"','"+id+"')\" title=\"Editar\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-pencil\"></span><span class=\"ui-button-text\">Edi</span></button>"+
	                			 "<button type=\"button\" id=\"shareBtn_"+oObj.aData.id+"\" class=\"shareBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" onclick=\"compartilhar(event,'"+url+"','"+id+"')\" title=\"Compartilhar\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-extlink\"></span><span class=\"ui-button-text\">Acl</span></button>"+
	                			 "<button type=\"button\" id=\"deletarBtn_"+oObj.aData.id+"\" class=\"deletarBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" onclick=\"deletarDoBanco(event,'"+url+"','"+id+"')\" title=\"Deletar\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-close\"></span><span class=\"ui-button-text\">Del</span></button>";
					 		}
					 	}
					 },
	                 {"sTitle": "Id", "mDataProp": "id", "bVisible": false, "sType": "numeric" },         	 
	                              {"sTitle": "Numero", "mDataProp": "numero", "bVisible": true, "sDefaultContent":"", "sWidth": "45px"},
             {"sTitle": "Nome", "mDataProp": "nome", "bVisible": true, "sDefaultContent":"", "sWidth": "45px"},
             {"sTitle": "Site", "mDataProp": "site", "bVisible": true, "sDefaultContent":"", "sWidth": "45px"},

	 ];
	    		
	var colunas = 	JSON.stringify(aoColumns, function(key, val) {if (typeof val === 'function') { return val + '';} return val;});
	listServerSide(id, colunas, sInfoComplemento, url, table, where, argumento, tipoArgumento, order, "${encoded}");

});

function selecionarPopUpListarBanco(event){
	var objetoSelecionado = selecionarObjeto(event, "listarBanco");
	buscar('listarBanco', 'Banco/'+objetoSelecionado.id, afterBuscaPopUpListarBanco);
}

function afterBuscaPopUpListarBanco(data){
	$("#listarBanco_filtrosAvancados").append('<input type="hidden" id="listarBanco_selecionado" />');
	$("#listarBanco_selecionado").val($.toJSON(data));
	try{$("#Banco_listar_popUp").dialog('close');
	$("#Banco_listar_popUp").dialog('destroy');
	$("#Banco_listar_popUp").remove();} catch(e){}
}

