@extends('layouts.default')
@section('scripts')
    {{ Jqgrid::scripts() }}

    <script type="text/javascript">

        var tipoprod = '{{{ $tipoprod }}}';
        var TIPOPROD = '{{{ $TIPOPROD }}}';
        $(document).ready(function() {

            $('#jqgrid').jqGrid({
                url: '{{{ url("productos/datagrid") }}}' + '/' + tipoprod,
                colModel: {{ Jqgrid::js_colmodel() }},
                gridComplete: function() {
                    $.jgrid.defaults.gridComplete();
                    $('#btn_editar>div.btn').addClass('disabled'); // deshabilita boton editar
                    $('#btn_eliminar>div.btn').addClass('disabled'); // deshabilita boton eliminar
                },
                onSelectRow: function(row_id) {
                    row_data = $("#jqgrid").getRowData(row_id);
                    $('#btn_editar>div.btn').removeClass('disabled'); //habilita boton editar
                    $('#producto_editar_id').val(row_data['NREGUIST']); // setear id producto
                    $('#btn_eliminar>div.btn').removeClass('disabled'); //habilita boton eliminar
                    $('#producto_eliminar_id').val(row_data['NREGUIST']); // setear id producto
                },
                sortname: 'NREGUIST',
            });

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

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

            @if(can('productos_agregar'))
            // botón agregar
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-plus',
                onClickButton: function() {
                    $('#divssfamilia').hide();
                    $('#producto_editar_id').val(0);
                    if (tipoprod == 2)
                    {
                        $('#divsfamilia').show();
                        $('#CLASE2').val(TIPOPROD);
                        $("#CLASE2").attr('readonly', true); 
                    }
                    else
                    {
                        $('#divDensidad').hide();
                        $("#CLASE2").attr('readonly', false); 
                    }

                    if (tipoprod != 1)
                    {
                        $('#divprecios').hide();
                    }
                    else
                    {
                        $('#divprecios').show();
                    }
                    
                    $('#productos_editar_modal').modal('show');
                },
                position: 'last', title: 'Agregar Producto', cursor: 'pointer', id: 'btn_agregar'}
            );

            // form agregar
            $('#productos_agregar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#productos_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);
                }
            });
            @endif

            @if(can('productos_editar'))
            // botón editar
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-pencil',
                onClickButton: function() {

//                    $("#CODIGO").attr('readonly', true); 
                    $('#CODIGO').prop('readonly', true);
                    $('#divsfamilia').hide();
                    $('#divssfamilia').hide();

                    if (tipoprod == 2)
                    {
                        $('#CLASE2').val(TIPOPROD);
                        $("#CLASE2").prop('readonly', true); 
                    }
                    else
                    {
                        $('#divDensidad').hide();
                        $("#CLASE2").attr('readonly', false); 
                    }

                    if (tipoprod != 1)
                    {
                        $('#divprecios').hide();
                    }
                    else
                    {
                        $('#divprecios').show();
                    }

                    if($('#btn_editar>div.btn').hasClass('disabled')) return false;
                    $('#productos_editar_form').formPopulate('{{ url("productos/obtener") }}');
                    $('#productos_editar_modal').modal('show');
                },
                position: 'last', title: 'Editar Productos', cursor: 'pointer', id: 'btn_editar'}
            );

            // form editar
            $('#productos_editar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#productos_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);
                }
            });
            @endif

            @if(can('productos_eliminar'))
            // botón eliminar
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-trash-o',
                onClickButton: function() {
                    if($('#btn_eliminar>div.btn').hasClass('disabled')) return false;
                    $('#productos_eliminar_modal').modal('show');
                },
                position: 'last', title: 'Eliminar Productos', cursor: 'pointer', id: 'btn_eliminar'}
            );

            // form eliminar
            $('#productos_eliminar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#productos_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

            // al cerrar modals, resetear form
            $('#productos_agregar_modal, #productos_editar_modal, #productos_eliminar_modal').on('hidden.bs.modal', function () {
                form = $(this).find('form').trigger('reset');
                clean_form_errors(form);
            });

        });
    </script>
    @endsection

    @section('content')
    @include('subviews.jqgrid_widget')
    <!-- Modal Editar -->
    <!-- Modal -->
    <div class="modal fade" id="productos_editar_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">
                        &times;
                    </button>
                    <h4 class="modal-title">Editar Producto</h4>
                </div>
                <form method="POST" action="{{ url('productos/editar') }}" class="form-horizontal" id="productos_editar_form">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <input type="hidden" name="id" value="" id="producto_editar_id">
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Código</label>
                                    <div class='col-sm-9'>
                                        {{ Form::text('CODIGO', NULL, ['class' => 'form-control', 'id' => 'CODIGO']) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Nombre</label>
                                    <div class='col-sm-9'>
                                        {{ Form::text('NOMBRE', NULL, ['class' => 'form-control']) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Tipo</label>
                                    <div class='col-sm-9' style="display: none;">
                                        {{ Form::text('TIPO', $tipoprod, ['class' => 'form-control', 'id' => 'TIPO']) }}
                                    </div>
                                    <div class='col-sm-9'>
                                        {{ Form::text('TIPOPROD', $TIPOPROD, ['class' => 'form-control', 'id' => 'TIPOPROD', 'readonly'=>true]) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm' ID='divDensidad'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Densidad</label>
                                    <div class='col-sm-9'>
                                        {{ Form::text('DENSIDAD', NULL, ['class' => 'form-control']) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Nombre Proveedor</label>
                                    <div class='col-sm-9'>
                                        {{ Form::text('NOM_PROV', NULL, ['class' => 'form-control']) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Solución</label>
                                    <div class='col-sm-9'>
                                        {{ Form::hidden('CLASE1', NULL, ['class' => 'form-control', 'id' => 'CLASE1']) }}
                                        {{ Form::text('TIPOPROD1', 'QUIMICOS', ['class' => 'form-control', 'id' => 'TIPOPROD1', 'readonly'=>true]) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Familia</label>
                                    <div class='col-sm-9'>
                                        {{ Form::select('CLASE2', ['' => 'Seleccione Familia'] + $clase2, NULL, ['class' => 'form-control', 'id' => 'CLASE2']) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm' ID='divsfamilia'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Sub Familia</label>
                                    <div class='col-sm-9'>
                                        {{ Form::select('CLASE3', ['' => 'Seleccione Sub Familia'] + $clase3, NULL, ['class' => 'form-control']) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm' ID='divssfamilia'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">S. Sub Familia</label>
                                    <div class='col-sm-9'>
                                        {{ Form::text('CLASE4', NULL, ['class' => 'form-control']) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm' id='divprecios'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Precio Venta</label>
                                    <div class='col-sm-9'>
                                        {{ Form::text('PRECVTA', NULL, ['class' => 'form-control', 'id' => 'PRECVTA']) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Estado</label>
                                    <div class='col-sm-9'>
                                        {{ Form::select('ELIMINADO', $estado, NULL, ['class' => 'form-control']) }}
                                    </div>
                                </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-primary">Grabar</button>
                    </div>
                </form>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    <!-- Fin Modal Editar -->

    @if(can('productos_eliminar'))
    <!-- Modal Eliminar -->
    <!-- Modal -->
    <div class="modal fade warning" id="productos_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">
                        &times;
                    </button>
                    <h4 class="modal-title">Eliminar Producto</h4>
                </div>
                <form method="POST" action="{{ url('productos/eliminar') }}" id="productos_eliminar_form">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <input type="hidden" name="id" value="" id="producto_eliminar_id">
                                <div class="form-group col-sm-12">
                                    <h4>¿Está seguro de eliminar este Producto?</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><!-- /.modal -->
    <!-- Fin Modal Eliminar -->
    @endif
@endsection