let textoAlternativo = 'Identificador';

$.fn.dataTable.moment( 'DD/MM/YYYY HH:mm:ss');
$('#tabelaIndicacoes').html('<table id="tbIndicacoes" style="width: 100%;" class="table table-striped table-bordered dataTable no-footer cell-border nowrap responsive compact"></table>');
$('#tbIndicacoes').DataTable({
	"data": [],
	"order": [[ 3, 'desc' ]],
	"columns": [{"title": "Nome do Responsável"}, {"title": "Mudança de Saldo"},{"title": "Motivo"},{"title": "Data da Alteração"}, {"title": textoAlternativo}, {"title": "Dia do Registro"},{"title": "Telefone"}, {"title": "Email"}],
	"lengthMenu": [[25, 50, 100, -1], ["25","50","100", "todas"]],
	responsive: true,
	"columnDefs": [ {"targets": [1], "type": "numeric-comma-real"}]
});

function carregar_ganhos(){
	$(document).ready(function() {
		jQuery.ajax({
			type: "POST",
			url: "action/atualizar_historico",
			data: [],
			success: function(data){
				data = JSON.parse(data);
				
				data.texto_alternativo ? textoAlternativo = data.texto_alternativo : textoAlternativo = 'CPF/CNPJ';

				if (data.dados.length === 0) {
					toastr.clear();
					toastr.error("Nenhum dado encontrado até o momento!");
				}

				$('.preloader').css({display:"none"});
				// if(data.dados == '-1') {window.location.href = "./"; return;}
				// if(data.dados.substring(0,2) == "-2") {toastr.clear(); toastr.error(data.dados.substring(2)); return;}
				var json = data.dados;
				var table_data = [];
				for (var i=1; i < json.length; i++){
					var date = Date.parse(json[i].data).toString('dd/MM/yyyy HH:mm:ss');
					var date2 = Date.parse(json[i].dataregistro).toString('dd/MM/yyyy HH:mm:ss');
					table_data[i-1] = ["",json[i].nome, json[i].comissao, json[i].motivo, date, json[i].cpf, date2, json[i].telefone, json[i].email];
				}

				$(document).ready(function() {
					$.fn.dataTable.moment( 'DD/MM/YYYY HH:mm:ss');
					$('#tabelaIndicacoes').html('<table id="tbIndicacoes" style="width: 100%;" class="table table-striped table-bordered dataTable no-footer cell-border nowrap responsive compact"></table>');
					$('#tbIndicacoes').DataTable({
						"data": table_data,
						"order": [[ 4, 'desc' ]],
						"columns": [{},{"title": "Nome do Responsável"}, {"title": "Mudança de Saldo"},{"title": "Motivo"},{"title": "Data da Alteração"}, {"title": textoAlternativo}, {"title": "Dia do Registro"},{"title": "Telefone"}, {"title": "Email"}],
						"lengthMenu": [[25, 50, 100, -1], ["25","50","100", "todas"]],
						responsive: {
						    details: {
						    	type: 'column',
						        display: $.fn.dataTable.Responsive.display.modal( {
						            header: function ( row ) {
						                return '<span style="margin-right: 10px"><b>Dados da Entrada</b></span>';
						            }
						        } ),
						        renderer: function ( api, rowIdx, columns ) {
						        	text = "";
						        	for(i = 0;i < columns.length;i++){
						        		column = columns[i];
						        		if(i > 0) text += "<br>";
						        		text += "<b>" + column.title + ": </b>" + column.data;
						        	}
						        	return text;
						        }
						    }
						},
						"deferRender": true,
						"columnDefs": [ {"targets": [2], "type": "numeric-comma-real"},{className: 'control', orderable: false, targets: 0, width: "5%"}]
					});
				});
			},
			beforeSend: function(){
				$('.preloader').css({display:"block"});
			},
			complete: function(){
				$('.preloader').css({display:"none"});
			},
			error: function(){
				$('.preloader').css({display:"none"});
				toastr.clear(); toastr.error("Houve um erro ao atualizar"); return;
			}
		});
	});
}

carregar_ganhos();