/home/dvjjulio/softtrash/public/js/egreso/poliza.js
const Poliza = {
    polizaOpen: () => {
        $('#modalPoliza').modal('show');
        $('#action-span').text('Nueva');
    },
    table: () => {
        $.get(main_path+'/egreso/polizaTable', {}, resp => {
            $('#poliza-table').html('').html(resp);
         });
    },
    getPoliza: function(e){
        var id              = $(e).data('id');
		var token	        = $('#csrf-token').val();
        var cuenta_id       = $('#cuenta-select').val();
        var subcuenta_id    = $('#subcuenta-select').val();
        var nombre          = $('#nombre-input').val();
        var referencia      = $('#referencia-input').val();
        var estado          = $('#estado-select').val();
        var vencimiento     = $('#vencimiento-input').val();
        var importe         = $('#importe-input').val();
        var iva             = $('#iva-input').val();
        var total           = $('#total-input').val();

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

            if(id !== ''){
                data.append('id'     , id);
            }
	        data.append('_token'        , token);
            data.append('cuenta_id'     , cuenta_id);
            data.append('subcuenta_id'  , subcuenta_id);
            data.append('nombre'        , nombre);
            data.append('referencia'    , referencia);
            data.append('estado'        , estado);
            data.append('vencimiento'   , vencimiento);
            data.append('importe'       , importe);
            data.append('iva'           , iva);
            data.append('total'         , total);

	        Poliza.sendPoliza(data);
	    }
	},
	sendPoliza:function(data){
		$.ajax({
			url: main_path+'/egreso/polizas',
			data: data,
			cache: false,
			contentType: false,
            processData: false,
            type: 'POST',
            dataType: 'json',
            success: function( response ){
            	if(response.status == true ){
                         $.bootstrapGrowl('Poliza Guardado!', {
                            type: "success",
                            delay: 6500,
                            allow_dismiss: true
                        });
                     $('#modalPoliza').modal('hide');
                     Poliza.resetFields();
                     Poliza.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-select').val('');
        $('#subcuenta-select').val('');
        $('#nombre-input').val('');
        $('#referencia-input').val('');
        $('#estado-select').val('');
        $('#vencimiento-input').val('');
        $('#importe-input').val('');
        $('#iva-input').val('');
        $('#total-input').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/getPoliza',
            data: {id: $(e).data('id')},
            cache:false,
            type: 'GET',
            dataType: "json",
            success: function( response ){
                console.log('response', response);
                if(response.status == true){
                    $('#cuenta-select').val(response.data.cuenta_id);
                    $('#subcuenta-select').val(response.data.subcuenta_id);
                    $('#nombre-input').val(response.data.nombre);
                    $('#referencia-input').val(response.data.referencia);
                    $('#estado-select').val(response.data.status);
                    $('#vencimiento-input').val(response.data.fecha_vencimiento);
                    $('#importe-input').val(response.data.importe);
                    $('#iva-input').val(response.data.iva);
                    $('#total-input').val(response.data.total);

                    $('#modalPoliza').modal('show');
                    $('#action-span').text('Editar');
                    $('#send-button').attr('data-id',$(e).data('id'));
                }
            },
            fail: function( response ) {
                alert('Error no encontrado')
                Poliza.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
        });
        Poliza.table();
     });
    },
    addIvaTotal: (e) => {
        const importe = parseInt($(e).val());
        const iva = (importe * .16);
        const total = (importe) + (iva);

        $('#iva-input').val(iva);
        $('#total-input').val(total);
    },
    getSubCuenta: (e) => {
        $.get(main_path+'/egreso/getSubCuenta', {id: $(e).val()}, (resp) => {
            console.log('resp', resp);
            const div = $('#subcuenta_select_filter_chosen').find('ul');
            console.log('div', div)
            $(div).html('').append(resp);
        });
    },
    filter: () => {
        const cuenta = $('#cuenta-select-filter').val();
        const subcuenta = $('#subcuenta-select-filter').val();
        const tipo = $('#tipo-select-filter').val();
        const pagado = $('#pagado-select-filter').val();
        const fecha = $('#date-range-input').datepicker({ dateFormat: 'YYYY-MM-DD' }).val();
        const fechaArray = fecha.split(' - ');
        const fechaI = fechaArray[0] || '';
        const fechaF = fechaArray[1] || '';

        const data = {
            cuenta,
            subcuenta,
            tipo,
            pagado,
            fechaI,
            fechaF
        };

        $.get(main_path+'/egreso/filterEgreso', {data}, (resp) => {
            $('#poliza-table').html('').html(resp);
        });
    },
    reset: () => {
        console.log('reset');
        $('#cuenta-select-filter').val(0);
        $('#cuenta-select-filter').trigger("chosen:updated");

        $('#subcuenta-select-filter').val(0);
        $('#subcuenta-select-filter').trigger("chosen:updated");
        $('#tipo-select-filter').val('');
        $('#pagado-select-filter').val('');

        $('#date-range-input').val('');

    }
}