$idImovel = $('#id_imovel').data('id_imovel');


$('body').on('click','.btnAtualizarImovel', function(){
    
    $data = get_form('.formAtualizaImovel');
    $data.push({"name": 'descricao', "value": 'Edit canais'});
    
    get_ajax('administrativo/ajax/imoveis_ajax/edit_imovel', 
        r =>{
            //location.reload();
        }, 
        erro =>{
            alert(erro.msg);
        }, $data)
    
    
});
$('body').on('click','.btnRestaurarImovel', function(){
    
    $data = {
        id_imovel: $idImovel
    }
    
    get_ajax('administrativo/ajax/imoveis_ajax/restaurar_imovel', 
        r =>{
            location.reload();
        }, 
        erro =>{
            alert(erro.msg);
        }, $data)
    
});
$('body').on('click','.btnBloquearImovel', function(){
    
    //formBloquearImovel
    $data = {
        id_imovel: $idImovel,
        motivo: $('#motivo').val(),
    }
    
    get_ajax('administrativo/ajax/imoveis_ajax/negar_imovel', 
        r =>{
            location.reload();
        }, 
        erro =>{
            alert(erro.msg);
        }, $data)
});
$('body').on('click','.btnPublicarImovel', function(){
    
    //formBloquearImovel
    $data = {
        id_imovel: $idImovel
    }
    
    get_ajax('administrativo/ajax/imoveis_ajax/aceitar_imovel', 
        r =>{
            location.reload();
        }, 
        erro =>{
            alert(erro.msg);
        }, $data)
    
    
});
$('body').on('click','.btnModerarImovel', function(){
    
    //formBloquearImovel
    $data = {
        id_imovel: $idImovel
    }
    
    get_ajax('administrativo/ajax/imoveis_ajax/moderar_imovel', 
        r =>{
            location.reload();
        }, 
        erro =>{
            alert(erro.msg);
        }, $data)
    
    
});


function get_form($form)
{
    data = [];
    var nameRadio = [];
    
    //$($form).find('input:not([type=radio]):input:not([type=checkbox]):input:not([type=file])').each(function(key,value){
    $($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('input[type=file]').each(function(key,value){
        if(this.name != ""){
            data.push({"name": this.name, "value": $(this).data('value')});
        }
    });
    
    $($form).find('textarea').each(function(key,value){
       
        if(this.name != "")
        {
            if(CKEDITOR.instances[this.name] != undefined){
                 //data.push({"name": this.name, "value": CKEDITOR.instances[this.name].getData()});
                 ckeditorValue = $($($($('#cke_'+this.id)[0])[0]).find('iframe').contents().find("body")).html();
                 data.push({"name": this.name, "value": ckeditorValue});
                 if(ckeditorValue == undefined)
                    data.push({"name": this.name, "value": this.value});
            }
            else
            {
                data.push({"name": this.name, "value": this.value});
            }
        }
    });
    
    $($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": ""});
    });
    
    data.push({"name": 'id_imovel', "value": $idImovel});
    
    return data;
}

//Callbeck erro ajax
$error = function error(r){console.log(r)};

/*
function get_ajax

$func = nome da funcao no php
$success = funcao Callbeck, exemplo = "retorno =>{alert('deu bom');}"
$error = funcao Callbeck
$data = objeto passado no post ajax
*/
function get_ajax($uri, $success, $error, $data = []){
    
    $.ajax({
		url: base_url+$uri, 
		dataType: 'json',
		type: 'POST',
		data: $data,
		success: function(retorno)
		{		
		    if(retorno.valid == true)
		    {   
		        $success(retorno);
		    }
		    else
		    {
		        $error(retorno);
		    }
		},
		error: function(error)
		{
		    $error(error);
		}
    });
    
 }