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

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

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

	        SubCuenta.sendSubCuenta(data);
	    }
	},
	sendSubCuenta:function(data){
		$.ajax({
			url: main_path+'/egreso/subCuentas',
			data: data,
			cache: false,
			contentType: false,
            processData: false,
            type: 'POST',
            dataType: "json",
            success: function( response ){
            	if(response.status == true ){
                         $.bootstrapGrowl('SubCuenta Guardado!', {
                            type: "success",
                            delay: 6500,
                            allow_dismiss: true
                        });
                    if(data.get('id') !== '') {
                        $('#modalSubCuenta').modal('hide');
                        // window.location.assign(main_path+'/catalogo/cuenta');
                    }
                     SubCuenta.resetFields();
                     SubCuenta.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(){
        $('#subcuenta-input').val('');
        $('#referencia-input').val('');
        $('#cuenta-select').val('');
	},
    tableFilter:function(e){
        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/getSubCuenta',
            data: {id: $(e).data('id')},
            cache:false,
            type: 'GET',
            dataType: "json",
            success: function( response ){
                if(response.status == true){
                    $('#subcuenta-input-modal').val(response.data.subcuenta);
                    $('#referencia-input-modal').val(response.data.referencia);
                    $('#cuenta-select-modal').val(response.data.cuenta_id);
                    $('#send-button-modal').attr('data-id',$(e).data('id'));

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

}