@extends('layouts.default')

@section('scripts')
    {{ Jqgrid::scripts(); }}
    <script type="text/javascript">
        var expandSubGrid = '{{ $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() {
            @if(Session::has('notificacion'))
            var contenido = {
                type: '{{ Session::get("notificacion.type") }}',
                content: '{{ Session::get("notificacion.content") }}'
            };
            notification(contenido);
            @endif
            $('#jqgrid').jqGrid({
                url: '{{ url("envases/datagrid") }}',
                colModel: {{ Jqgrid::js_colmodel('formulas') }},
                gridComplete: function() {
                    $.jgrid.defaults.gridComplete();
                    $('#btn_agregar>div.btn').addClass('disabled'); // deshabilita boton agregar
                    $('#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: '{{ url("detalles_envases/datagrid?NREGUIST=") }}' + row_data['NREGUIST'],
                        colModel: {{ 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});

            @if(can('envases_agregar'))
            //Botón agregar fórmula
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-plus',
                onClickButton: function() {
                    localStorage.removeItem('formula');
                    window.location = "{{ url('envases/agregar') }}";
                },
                position: 'last', title: 'Agregar Envase', cursor: 'pointer', id: 'btn_agregar'}
            );
            @endif

            @if(can('envases_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;
                    localStorage.removeItem('formula.'+selectedRow);
                    window.location = "{{ url('envases/editar') }}/" + selectedRow;
                },
                position: 'last', title: 'Editar Envase', cursor: 'pointer', id: 'btn_editar'}
            );
            @endif

            @if(can('envases_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;
                    $('#envases_eliminar_modal').modal('show');
                },
                position: 'last', title: 'Eliminar Envase', cursor: 'pointer', id: 'btn_eliminar'}
            );

            // form eliminar
            $('#envases_eliminar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#envases_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);
                }
            });
            @endif
        });
    </script>
@stop
@section('content')
    @include('subviews.jqgrid_widget', ['title' => 'Envases'])

    @if(can('envases_eliminar'))
    <div class="modal fade warning" id="envases_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 Envase</h4>
                </div>
                <form method="POST" action="{{ url('envases/eliminar') }}" class="form-horizontal" id="envases_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 Envase?</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>
    @endif
@stop