<?php $__env->startSection('scripts'); ?>
    <?php echo Jqgrid::scripts();; ?>

    <script type="text/javascript">
        var expandSubGrid = '<?php echo isset($rowid) ? $rowid : ''; ?>';
        var selectedRow = '';
        //Modificaciones antes de mostrar la grilla para celulares
        if($(window).width() <= 768) {
            $.jgrid.defaults.rowList = [];//elimina select de cantidad de registros en paginacion
            $.jgrid.defaults.viewrecords = false;//elimina infirmacion de cantidad de registros en paginacion
        }
        $(document).ready(function() {
            <?php if(Session::has('notificacion')): ?>
            var contenido = {
                type: '<?php echo Session::get("notificacion.type"); ?>',
                content: '<?php echo Session::get("notificacion.content"); ?>'
            };
            notification(contenido);
            <?php endif; ?>
            $('#jqgrid').jqGrid({
                url: '<?php echo url("NotasVentas/datagrid"); ?>',
                colModel: <?php echo Jqgrid::js_colmodel('Facturas'); ?>,
                gridComplete: function() {
                    $.jgrid.defaults.gridComplete();
                    $('#btn_editar>div.btn').addClass('disabled'); // deshabilita boton editar
                    $('#btn_eliminar>div.btn').addClass('disabled'); // deshabilita boton eliminar TODO
                    if(expandSubGrid) {
                        $('#jqgrid').expandSubGridRow(expandSubGrid);
                        expandSubGrid = false;
                    }
                },
                onSelectRow: function(row_id) {
                    row_data = $("#jqgrid").getRowData(row_id);
                    $('#btn_editar>div.btn').removeClass('disabled'); //habilita boton eliminar
                    $('#btn_eliminar>div.btn').removeClass('disabled'); //habilita boton eliminar
                    selectedRow = row_data['ID'];
                    $('#id_modificar').val(row_data['ID']);
                    $('#id_eliminar').val(row_data['ID']);
                },
				sortname: "NUMNOTA",
                sortorder: "desc",
                subGrid: true,
                subGridRowExpanded: function(subgrid_id, row_id) {
                    row_data = $("#jqgrid").getRowData(row_id);
                    subgrid_setup(subgrid_id);
                    $('#' + subgrid_table_id).jqGrid({
                        url: '<?php echo url("detalles_Factura/datagrid?NUMRECORGUIARECEPCION="); ?>' + row_data['NUMREG'],
                        colModel: <?php echo Jqgrid::js_colmodel('detalles_Factura'); ?>,
                        rowList: false,
                        pager: pager_id,
                        sortorder: 'asc',
                        sortname: 'CODIGO'
                    });
                    set_classes();
                }
            });

            $('#jqgrid').setColProp('FECHA', {searchoptions: {dataInit: dataInit}});

            $('#jqgrid').jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch: 'cn'});

            /*if($(window).width() <= 768) {
             $('#jqgrid').jqGrid('hideCol',["NREGUIST", "CODIGO_BARRAS", "VALPROM"]); //oculta columnas
             }*/
            $(window).trigger('resize.jqGrid');//redimensiona tamaño grilla

            //Botones
            $('#jqgrid').jqGrid('navGrid', '#jqgrid_pager', {add: false, edit: false, del: false, search: false});

            <?php if(can('NotasVentas_agregar')): ?>
               //Botón agregar Factura
               $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-plus',
                onClickButton: function() {
/*                    if(localStorage.getItem('Factura') != null) {
                        if(!confirm('Existe un Factura Pendiente!\nPresionar Cancelar para iniciar un nuevo Factura.\nPresionar Aceptar para continuar con Factura pendiente.')) {
                            localStorage.removeItem('Factura');
                        }
                    }

					var idDocto = 0;
                    if (localStorage.getItem('Factura') != null)
					{
                        var idDocto  = localStorage.getItem('Factura').ID;
						if (idDocto == null)
                        {
                            idDocto = 0;
                        }
                    }
                    */
                    localStorage.removeItem('Factura');
                    idDocto = 0;
                    window.location = "<?php echo url('notasventas/agregar'); ?>/" + idDocto;
/*
					else  
					{
						var TipDocID = 'FA';

                        $.get('<?php echo url("NotasVentas/autonumerar"); ?>/' + TipDocID, function(data) {	
                             localStorage.setItem('ID', selectedRow);
                             localStorage.setItem('Factura', data.numero[0].NroDocID);
							 window.location = "<?php echo url('NotasVentas/agregar'); ?>";
                        }, 'json');
                    }
					*/
                },
                position: 'last', title: 'Agregar Factura', cursor: 'pointer', id: 'btn_agregar'}
            );
            <?php endif; ?>

            <?php if(can('NotasVentas_editar')): ?>

            // botón editar
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-pencil',
                onClickButton: function() {
					if (selectedRow == null || selectedRow == 0)
					{
                        var message  = {content : 'Debe seleccionar un documento para modificar.', type : 'error'}
						notification(message, size='small');
					}
                    else 
					{
                        localStorage.removeItem('Factura');
                        window.location = "<?php echo url('notasnentas/agregar'); ?>/" + selectedRow;
                    }

                },
                position: 'last', title: 'Editar Facturas', cursor: 'pointer', id: 'btn_editar'}
            );

            // form editar
            $('#NotasVentas_editar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#NotasVentas_editar_modal').modal('hide');
                    success_notification(response);
                    //actualizo grid
                    $('#jqgrid').trigger('reloadGrid');
                },
                error: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    show_form_errors(response, $form);
                    error_notification(response);
                }
            });
            <?php endif; ?>




            <?php if(can('NotasVentas_eliminar')): ?>
            //Botón eliminar Factura
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-trash-o',
                onClickButton: function() {
                    if($('#btn_eliminar>div.btn').hasClass('disabled')) return false;
                    $('#NotasVentas_eliminar_modal').modal('show');
                },
                position: 'last', title: 'Eliminar Factura', cursor: 'pointer', id: 'btn_eliminar'}
            );

            // form eliminar
            $('#NotasVentas_eliminar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#NotasVentas_eliminar_modal').modal('hide');
                    success_notification(response);
                    //actualizo grid
                    $('#jqgrid').trigger('reloadGrid');
                },
                error: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    show_form_errors(response, $form);
                    error_notification(response);
                }
            });
            <?php endif; ?>
        });
    </script>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
    <?php echo $__env->make('subviews.jqgrid_widget', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

    <?php if(can('NotasVentas_eliminar')): ?>
        <div class="modal fade warning" id="NotasVentas_eliminar_modal" role="dialog" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" tabindex="-1" data-dismiss="modal" aria-hidden="true">
                            ×
                        </button>
                        <h4 class="modal-title">Eliminar Factura</h4>
                    </div>
                    <form method="POST" action="<?php echo url('NotasVentas/eliminar'); ?>" class="form-horizontal" id="NotasVentas_eliminar_form">
                        <input type="hidden" id="id_eliminar" name="id">
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group col-sm-12">
                                        <h4>¿Está seguro de eliminar este Factura?</h4>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                            <button type="submit" class="btn btn-danger">Eliminar</button>
                        </div>
                    </form>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
    <?php endif; ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.default', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>