$('#google_auth').on('submit',function(e){
    e.preventDefault();
    console.log('entrou');
   $.ajax({
		url: base_url+"auth/autenticacao/save_key",
		dataType: 'json',
		type: 'POST',
		data: get_form('#google_auth'),
		success: function(retorno)
		{	
	        if(retorno.valid == true){
	            window.location.href = base_url+"/painel";
	        }else{
	            $(".result").html('<div class="alert alert-danger alert-dismissible show" role="alert"> '+retorno.msg+' <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div>');
	            
                $('input[name="code_auth"]').val('');
                
                if(retorno.code == '003'){
                    $('.cod_incorreto').show();
                    $('.qr_code_read_erro').hide();
                }   	            
	        }
		}
    });
});

$('.btn-copiar').on('click', function(){
     $('input[name="secret_auth"]').select();
    
    var successful = document.execCommand('copy');
    var result = successful ? 'successful' : 'unsuccessful';
    
    if(result == 'successful'){
        console.log('successful');
        $(this).attr('data-original-title',"Link copiado com sucesso!").tooltip('show');
    }else{
        console.log('unsuccessful');
    }
});

$('.btn-qrcode').on('click', function(e){
    e.preventDefault();
    //$('input[name="secret_auth"]').attr('type','text');
    $('input[name="secret_auth"]').attr('class','form-control');
    $('input[name="secret_auth"]').attr('readonly','readonly');
    $('#label').show();
    $('#label-code').show();
    $('.qr_code_read_erro').show();
});

$('#doisfatores').on('submit',function(e){
    e.preventDefault();
    console.log('entrou');
   $.ajax({
		url: base_url+"auth/autenticacao/verifica_key",
		dataType: 'json',
		type: 'POST',
		data: get_form('#doisfatores'),
		success: function(retorno)
		{	
		    console.log(retorno);

	        if(retorno.valid == true){
	           window.location.href = base_url+retorno.redirect;
	        }else{
	            $(".result").html('<div class="alert alert-danger alert-dismissible show" role="alert"> '+retorno.msg+' <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div>');
	            
	            $('input[name="code_auth"]').val('');
	        }
		}
    });
});

function get_form($form)
{
    
    console.log($form);
    data = [];
    var nameRadio = [];
    
    //var data = $("#form-imovel").find('input,textarea').serializeArray();
    $($form).find('input:not([type=radio]):input:not([type=checkbox]):input:not([type=file])').each(function(key,value){
        if(this.name != "")
            data.push({"name": this.name, "value": this.value});
    });
    $($form).find('textarea').each(function(key,value)    {
        if(this.name != "")
            data.push({"name": this.name, "value": CKEDITOR.instances[this.name].getData()});
    });
    
    $($form).find('input[type=radio]').each(function(key,value){
        if(nameRadio[this.name] == undefined)
            nameRadio[this.name] = '';
        if($(this).is(':checked'))
            nameRadio[this.name] = this.value;
    });
    
    for(var name in nameRadio){
        data.push({"name": name, "value": nameRadio[name]});
    };
    
    $($form).find('select option:selected').each(function(key,value)    {
        if(this.parentElement.localName == 'select')
            data.push({"name": this.parentElement.name, "value": this.value});
            
        if(this.parentElement.parentElement.localName == 'select')
            data.push({"name": this.parentElement.parentElement.name, "value": this.value});
    });
    
    $($form).find('input[type=checkbox]:checked').each(function(key,value)    {
        if(this.name != "")
            data.push({"name": this.name, "value": this.value});
    });
    
    $($form).find('input[type=checkbox]:not(:checked)').each(function(key,value)    {
        if(this.name != "")
            data.push({"name": this.name, "value": ""});
    });
    
    
    return data;
}