$('table.display').DataTable();

$id_cadastro = $('#id_cadastro').val();
$dadosPerfil = $('#dados_perfil').data('dados_perfil')
$(".act-remover-img").click(function () {
    $(this).hide();
    $('.act-pergunta-img').removeClass('d-none');
    $('.act-pergunta-img').show();
});

$(".act-remover_img_perfil").click(function () {

    $('.act-remover-img').show();
    $(this).hide();
    
    $('.container-img-img').hide();
    $('.container-img-load').hide();
    $('.container-img-input').removeClass('d-none');
    
    $('.container-img-input').show('clip');
    $(".divImgPerfil").removeAttr("style");
     
    $.ajax(
    {
        url: base_url+'administrativo/ajax/painel_ajax/remove_imagem_perfil',
		type: 'POST',
		data: {id_cadastro: $id_cadastro},
        success: function(result){}         
    });
});
iniUploadImgPerfil(); 
function iniUploadImgPerfil()
{
    // Initialize the jQuery File Upload widget:
    $('#dn_img').fileupload({
        dropZone: $('#dn_img_div'),
        formData: {id_cadastro: $id_cadastro},
        url: base_url + 'administrativo/ajax/painel_ajax/upload_img_perfil',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpeg|jpg|png)$/i,
        maxFileSize: 1024 * 1024 * 3, // 3 Mb NA TEORIA
        // Enable image resizing, except for Android and Opera,
        // which actually support image resizing, but fail to
        // send Blob objects via XHR requests:
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 668,
        previewMaxHeight: 350,
        previewCrop: true,
        messages : {
          maxNumberOfFiles: 'Limite máximo atingido',
          acceptFileTypes: 'Extensão não permitida',
          maxFileSize: 'Limite máximo permitido de 3 Mb',
          minFileSize: 'Imagem menor que... '
        },
        uploadTemplateId: null,
        uploadTemplate: function (o) {
        var rows = $();
        
        return rows;
    },
        
    })
    .bind('fileuploadadded', function (e, data) {
        
        $('.container-img-input').hide();
        $('.container-img-img').hide();
        $('.container-img-load').removeClass('d-none');
        $('.container-img-load').show();
        
    })
    .fileupload().bind('fileuploaddone', function (e, data) 
    {
        if(data.result.valid == true)
        {
            //$(".divImgPerfil").html('<img src="'+data.result.imagem_path+'" class="img-fluid rounded">');
            $(".divImgPerfil").css('background-image','url(' + data.result.imagem_path + ')');
            $('.container-img-load').removeClass('d-block');
            $('.container-img-load').addClass('d-none');
            $('.container-img-input').hide();
            $('.container-img-img').removeClass('d-none');
            
            $('.container-img-img').show();
            
            $('.act-pergunta-img').hide();
            $('.act-remover-img').show('clip');
           
            $('#dn_img').data('value','img');
        } 
        else
        {
            $('.act-remover-img').show();
            $(".act-remover_img_perfil").hide();
            $('.container-img-img').hide();
            $('.container-img-load').removeClass('d-block');
            $('.container-img-load').hide();
            $('.container-img-input').removeClass('d-none');
            $('.container-img-input').show('clip');
            $(".divImgPerfil").removeAttr("style");
            $('#dn_img').data('value','');
            text = "Ops! "+data.result.msg;
            notifyErros(text);
        }
    })
    .bind('fileuploadprocessfail', function (e, data) {
      
        var error = data.files[0].error;
        
        text = "Ops! "+error;
        notifyErros(text)
        
    });
}

$('body').on('click','.addAtendimento', function(){
    
    
    $.ajax({
		url: base_url+"administrativo/ajax/atendimento_ajax/ini_atendimento",
		dataType: 'json',
		type: 'POST',
		data: $dadosPerfil,
		success: function(retorno)
		{		
		    if(retorno.valid == true)
		    {
		        //document.location.href = base_url+'administrativo/atendimento/atendimentos/'+retorno.id_atendimento+'/add';
		        window.open(base_url+'administrativo/atendimento/atendimentos/'+retorno.id_atendimento+'/add');
		    }
		    else
		    {
		        
		    }
		},
		error: function(error)
		{
			console.log(error);
		}
	});	
});

// Upload do Documento
    $(function () {
        $('#depoimento_file').fileupload({
            dropZone: $('#depoimento_file_div'),
            formData: {id_cadastro: $id_cadastro},
            url: base_url+'administrativo/ajax/painel_ajax/depoimento_file',
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(gif|jpeg|jpg|png|pdf|mp3|mp4)$/i,
            maxFileSize: 1024 * 1024 * 3, // 3 Mb NA TEORIA
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 668,
            previewMaxHeight: 350,
            previewCrop: true,
            messages : {
              maxNumberOfFiles: 'Limite máximo atingido',
              acceptFileTypes: 'Extensão não permitida',
              maxFileSize: 'Limite máximo permitido de 3 Mb',
              minFileSize: 'Imagem menor que... '
            },
            uploadTemplateId: null,
            uploadTemplate: function (o) {
            var rows = $();
            
            return rows;
        },
            
        })
        .bind('fileuploadadded', function (e, data) {
            //$('.container-doc-file').hide();
            $('.box-icons-doc').removeClass('d-block');
            $('.box-icons-doc').hide();
            $('.container-box-doc').hide();
            $('.container-doc-load').removeClass('d-none');
            $('.container-doc-load').addClass('d-block');
            
            ativaDivDoc('#depoimento_file_div_load');
        })
        .fileupload().bind('fileuploaddone', function (e, data) 
        {
            var error = undefined; //data.result.uploadDoc.error;
            
            if(error === undefined)
            {
                //var fileName = data.result.uploadDoc;
                //var file_ext = fileName.substring(fileName.lastIndexOf('.') + 1);
                
                $('.divDoc').html('<a href="'+data.result.imagem_path+'" target="_blank"><img src="https://dev.comdono.com/themes/comdono/img/ico-file-mp3.png"></a>');
                
                ativaDivDoc('#depoimento_file_div_doc');
                $(".link_divDoc").attr("href", data.result.imagem_path);
                
                $('.container-doc-load').removeClass('d-block');
                $('.container-doc-load').addClass('d-none');
                $('.box-icons-doc').removeClass('d-block');
                $('.box-icons-doc').addClass('d-none');
                $('.container-box-doc').removeClass('d-none');
                $('.container-box-doc').addClass('d-block');
                $('.box-act-remover-doc').removeClass('d-none');
                $('.box-act-remover-doc').addClass('d-block');
                $('.box-act-remover-doc .act-pergunta-doc').hide();
                $('.box-act-remover-doc .act-remover-doc').show();
                
                $('#depoimento_file').data('value', data.result.imagem_path);
            } 
            else
            {
                alert(error);
            }
        })
        .bind('fileuploadprocessfail', function (e, data) {
          
            var error = data.files[0].error;
            
            text = "Ops! "+error;
            notifyErros(text)
        
            $('.container-box-doc').removeClass('d-block');
            $('.container-box-doc').addClass('d-none');
            $('.container-doc-load').removeClass('d-block');
            $('.container-doc-load').addClass('d-none');
            $('.box-icons-doc').removeClass('d-none');
            $('.box-icons-doc').addClass('d-block');
            $('.box-act-remover-doc').removeClass('d-block');
            $('.box-act-remover-doc').addClass('d-none');
            $('#depoimento_file').data('value', '');
            
            ativaDivDoc('#depoimento_file_div');
        });
    });
    
    
function ativaDivDoc($div)
{
    $('#depoimento_file_div').removeClass('d-block');
    $('#depoimento_file_div_load').removeClass('d-block');
    $('#depoimento_file_div_doc').removeClass('d-block');
    
    $('#depoimento_file_div').addClass('d-none');
    $('#depoimento_file_div_load').addClass('d-none');
    $('#depoimento_file_div_doc').addClass('d-none');
    
    $($div).removeClass('d-none');
    $($div).addClass('d-block');
}

$(".act-remover_doc").click(function () {
    
    $('.container-box-doc').removeClass('d-block');
    $('.container-box-doc').addClass('d-none');
    $('.container-doc-load').removeClass('d-block');
    $('.container-doc-load').addClass('d-none');
    $('.box-icons-doc').removeClass('d-none');
    $('.box-icons-doc').addClass('d-block');
    $('.box-act-remover-doc').removeClass('d-block');
    $('.box-act-remover-doc').addClass('d-none');
    
    ativaDivDoc('#depoimento_file_div');
    
    $('#depoimento_file').data('value', '');
    
    $.ajax(
    {
        url: base_url+'administrativo/ajax/painel_ajax/remove_depoimento_file',
        type: 'POST',
        dataType: 'json',
        data: {id_cadastro: $id_cadastro},
        success: function(result){}         
    });
});

$(".act-remover-doc").click(function () {
    $(this).hide();
    $('.act-pergunta-doc').removeClass('d-none');
    $('.act-pergunta-doc').show();
});


//Auto Save
$('body').on('change','.autoSave', function(){
   
    var name =  $(this).data("name");
    var val  =  $(this).val();
    
    var form = [];
    form.push({"name": 'id_cadastro', "value": $id_cadastro});
    form.push({"name": name, "value": val});
    
    if(name == 'val_pretendido' || name == 'area_total')
    {
        $card = $(this).parents('.card-body');
        vtm = $card.find('.val_public_total_metro').val();
        vtt = $card.find('.val_public_total').val();
        form.push({"name": "val_public_total_metro", "value": vtm});
        form.push({"name": "val_public_total", "value": vtt});
    }
    
    $.ajax({
    		url: base_url+URI.atualiza_imovel,
    		/*secureuri		:false,
			fileElementId	:'doc_propriedade',*/
    		dataType: 'json',
    		type: 'POST',
    		data: form,
    		success: function(retorno)
    		{
    		    
    		},
    		error: function(error)
    		{
    		    console.log(error);
    		}
    	});
    	
});