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

    <script type="text/javascript">
        var expandSubGrid = '<?php echo $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("formulas/datagrid"); ?>',
                colModel: <?php echo Jqgrid::js_colmodel('formulas'); ?>,
                gridComplete: function() {
                    $.jgrid.defaults.gridComplete();
                    $('#btn_editar>div.btn').addClass('disabled'); // deshabilita boton editar
                    $('#btn_eliminar>div.btn').addClass('disabled'); // deshabilita boton eliminar
                    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_eliminar').val(row_data['ID']);
                },
                sortname: 'FECHA_CRE',
                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_formula/datagrid?NREGUIST="); ?>' + row_data['NREGUIST'],
                        colModel: <?php echo Jqgrid::js_colmodel('detalles_formula'); ?>,
                        rowList: false,
                        pager: pager_id,
                        sortorder: 'asc'
                    });
                    set_classes();
                }
            });

            $('#jqgrid').setColProp('FECHA_CRE', {searchoptions: {dataInit: dataInit}});
            $('#jqgrid').setColProp('FECHA_MOD', {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('formulas_agregar')): ?>
            //Botón agregar fórmula
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-plus',
                onClickButton: function() {
                    if(localStorage.getItem('formula') != null) {
                        if(!confirm('Existe un ingreso Pendiente!\nPresionar Cancelar para iniciar un nuevo ingreso.\nPresionar Aceptar para continuar con ingreso pendiente.')) {
                            localStorage.removeItem('formula');
                        }
                    }
                    window.location = "<?php echo url('formulas/agregar'); ?>";
                },
                position: 'last', title: 'Agregar Fórmula', cursor: 'pointer', id: 'btn_agregar'}
            );
            <?php endif; ?>

            <?php if(can('formulas_editar')): ?>
            //Botón editar fórmula
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-pencil',
                onClickButton: function() {
                    if($('#btn_editar>div.btn').hasClass('disabled')) return false;
                    if(localStorage.getItem('formula.'+selectedRow) != null) {
                        if(!confirm('Existe un ingreso Pendiente!\nPresionar Cancelar para iniciar un nuevo ingreso.\nPresionar Aceptar para continuar con ingreso pendiente.')) {
                            localStorage.removeItem('formula.'+selectedRow);
                        }
                    }
                    window.location = "<?php echo url('formulas/editar'); ?>/" + selectedRow;
                },
                position: 'last', title: 'Editar Fórmula', cursor: 'pointer', id: 'btn_editar'}
            );
            <?php endif; ?>

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

            // form eliminar
            $('#formulas_eliminar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#formulas_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', ['title' => 'Fórmulas'], array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

    <?php if(can('formulas_eliminar')): ?>
    <div class="modal fade warning" id="formulas_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 Fórmula</h4>
                </div>
                <form method="POST" action="<?php echo url('formulas/eliminar'); ?>" class="form-horizontal" id="formulas_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 esta Fórmula?</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(); ?>