/home/dvjjulio/softtrash/public/js/egreso/cuenta.js
var Cuenta = {
	getCuenta: function(e){
        var id = $(e).data('id');
        var edit = (id !== '') ? '-modal' : '';
		var token	    = $('#csrf-token' + edit).val();
        var cuenta      = $('#cuenta-input' + edit).val();
        var referencia  = $('#referencia-input' + edit).val();
        var tipo        = $('#tipo-select' + edit).val();

		if (cuenta === '' || cuenta === null) {
            $('#cuenta-input'+edit).css('border','1px solid red');
        } else if(referencia === '' || referencia === null) {
			$('#referencia-input'+edit).css('border','1px solid red');
        } else if(tipo === '' || tipo === null) {
			$('#tipo-select'+edit).css('border','1px solid red');
		} else {
			var data = new FormData();

            if(id !== ''){
                data.append('id'     , id);
            }
	        data.append('_token', token);
            data.append('cuenta', cuenta);
            data.append('referencia', referencia);
            data.append('tipo', tipo);

	        Cuenta.sendCuenta(data);
	    }
	},
	sendCuenta:function(data){
		$.ajax({
			url:main_path+'/egreso/cuentas',
			data:data,
			cache:false,
			contentType: false,
            processData: false,
            type: 'POST',
            dataType: "json",
            success: function( response ){
            	if(response.status == true ){
                         $.bootstrapGrowl('Cuenta Guardado!', {
                            type: "success",
                            delay: 6500,
                            allow_dismiss: true
                        });
                    if(data.get('id') !== ''){
                        $('#modalCuenta').modal('hide');
                        // window.location.assign(main_path+'/catalogo/cuenta');
                    }
                     Cuenta.resetFields();
                     Cuenta.table();
                }else{
	                	$.bootstrapGrowl( 'Mensaje:'+response.message , {
	                        type: "danger",
	                        delay: 7500,
	                        allow_dismiss: true
	                    });
                	}
            },
        	fail: function( response ) {
              		$.bootstrapGrowl( 'Mensaje:'+response.message , {
                        type: "danger",
                        delay: 7500,
                        allow_dismiss: true
                    });
            }
		});
	},
	resetFields:function(){
        $('#cuenta-input').val('');
        $('#referencia-input').val('');
        $('#tipo-select').val('');
	},
    tableFilter:function(e){
        console.log('clickcc');
        var $panel = $(e).parents('.filterable'),
        $filters = $panel.find('.filters input'),
        $tbody = $panel.find('.table tbody');
        if ($filters.prop('disabled') == true) {
            $filters.prop('disabled', false);
            $filters.first().focus();
        } else {
            $filters.val('').prop('disabled', true);
            $tbody.find('.no-result').remove();
            $tbody.find('tr').show();
        }
    },
     edit: function(e){
    	$.ajax({
            url: main_path+'/egreso/getCuenta',
            data: {id: $(e).data('id')},
            cache:false,
            type: 'GET',
            dataType: "json",
            success: function( response ){
                console.log('response', response);
                if(response.status == true){
                    $('#cuenta-input-modal').val(response.data.cuenta);
                    $('#referencia-input-modal').val(response.data.referencia);
                    $('#tipo-select-modal').val(response.data.tipo);
                    $('#send-button-modal').attr('data-id',$(e).data('id'));

                    $('#modalCuenta').modal('show');
                }
            },
            fail: function( response ) {
                alert('Error no encontrado')
                Cuenta.table();
            }
        });
    },
    delete: function(e){
        $.get(main_path+'/egreso/deleteCuenta',{id: $(e).data('id')}, function(resp){
            $.bootstrapGrowl(resp.message, {
                type: resp.type,
                delay: 6500,
                allow_dismiss: true
        });
        Cuenta.table();
     });
    },
    table: () => {
        $.get(main_path+'/egreso/cuentaTable', {}, resp => {
            $('#cuenta-table').html('').html(resp);
         });
    }

}