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

    <script type="text/javascript">
        $(document).ready(function() {

            $('#jqgrid').jqGrid({
                url: '{{{ url("etiquetas/datagrid") }}}',
                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
                    $('#etiquetas_editar_id').val(row_data['Id']); // setear id familia
                    $('#btn_eliminar>div.btn').removeClass('disabled'); //habilita boton eliminar
                    $('#etiquetas_eliminar_id').val(row_data['Id']); // setear id familia
                },
                sortname: 'Id',
            });

            $('#jqgrid').setColProp('Fecha', {searchoptions: {dataInit: dataInit}});
            $('#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('familias_agregar'))
            // botón agregar
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-plus',
                onClickButton: function() {
                    $('#familias_agregar_modal').modal('show');
                },
                position: 'last', title: 'Agregar familia', cursor: 'pointer', id: 'btn_agregar'}
            );

            // form agregar
            $('#familias_agregar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#familias_agregar_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('familias_editar'))
            // botón editar
            $('#jqgrid').jqGrid('navButtonAdd', '#jqgrid_pager', {caption: '', buttonicon: 'fa fa-pencil',
                onClickButton: function() {
                    if($('#btn_editar>div.btn').hasClass('disabled')) return false;
                    $('#familias_editar_form').formPopulate('{{ url("familias/obtener") }}');
                    $('#familias_editar_modal').modal('show');
                },
                position: 'last', title: 'Editar familias', cursor: 'pointer', id: 'btn_editar'}
            );

            // form editar
            $('#familias_editar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#familias_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('familias_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;
                    $('#familias_eliminar_modal').modal('show');
                },
                position: 'last', title: 'Eliminar familias', cursor: 'pointer', id: 'btn_eliminar'}
            );

            // form eliminar
            $('#familias_eliminar_form').ajaxForm({
                resetForm: true,
                success: function(response, statusText, xhr, $form) {
                    clean_form_errors($form);
                    $('#familias_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
            $('#familias_agregar_modal, #familias_editar_modal, #familias_eliminar_modal').on('hidden.bs.modal', function () {
                form = $(this).find('form').trigger('reset');
                clean_form_errors(form);
            });

			$(document).on('click', '#barra-impresora', function() {
                //Actualizamos variable jsonData
				
                var id_mp          = $('#id_imprimir').val();
				var btype          = 'code128';

//				JsBarcode("#itf", value.toString(), {format: "CODE128", displayValue: false});

                var nreguist       = $('#nreguist2').html();
                var codigoProducto = $('#codigoProductoPantalla2').html();
                var nombreProducto = $('#nombreProductoPantalla2').html();
				var value          = $('#loteProductoPantalla2').html();
				var valueLote      = nreguist+'-'+value;

                const img = document.querySelector('img#itf');
				$('#pdf_imprimir').html('');

                var doc = new jsPDF("l", "mm", [70, 30]);
                doc.setFontSize(8);
                doc.addImage(img.src, 'JPEG', 5, 2, 60, 15);
                doc.text(5, 20, 'Código: '   + codigoProducto);
                doc.text(5, 23, 'Producto: ' + nombreProducto);
                doc.text(5, 26, 'Lote:  '    + valueLote);
				doc.autoPrint();

//                doc.save('my.pdf');
				$('#pdf_imprimir').html('<embed type="application/pdf" src="'+doc.output('datauristring')+'" id="pdfDocument" width="100%" height="100%" />');

            });

            $(document).on('click', 'button[name*="IMPRIMIR"]', function() {
 
                var nreguist       = $(this).closest('tr').children()[5].textContent;
                var codigo         = $(this).closest('tr').children()[6].textContent;
                var producto       = $(this).closest('tr').children()[7].textContent;
                var lote           = $(this).closest('tr').children()[8].textContent;

                //Actualizamos variable jsonData
				
                var itemString     = $(this).attr("name");
                var id_mp          = itemString.replace("IMPRIMIR_", "");
                var codigoProducto = $.trim(codigo);
                var nombreProducto = producto;
				var value          = lote;
				var valueLote      = nreguist+'-'+value;
				var btype          = 'code128';

				JsBarcode("#itf", valueLote.toString(), {format: "CODE128", displayValue: false});

                $('#nreguist2').html( nreguist);
                $('#codigoProductoPantalla2').html(codigoProducto);
                $('#nombreProductoPantalla2').html(nombreProducto);
                $('#loteProductoPantalla2').html(lote);

				$('#id_imprimir').val(id_mp);


                const img = document.querySelector('img#itf');

                var doc = new jsPDF("l", "mm", [70, 30]);
                doc.setFontSize(8);
                doc.addImage(img.src, 'JPEG', 5, 2, 60, 15);
                doc.text(5, 20, 'Código: '   + codigoProducto);
                doc.text(5, 23, 'Producto: ' + nombreProducto);
                doc.text(5, 26, 'Lote:  '    + valueLote);
				doc.autoPrint();
//                doc.save('my.pdf');
				$('#pdf_imprimir').html('<embed type="application/pdf" src="'+doc.output('datauristring')+'" id="pdfDocument" width="100%" height="100%" />');
				
				$('#codigo_barras_modal').modal('show');
		    });


        });
    </script>
    @endsection

    @section('content')
    @include('subviews.jqgrid_widget')
    @if(can('familias_agregar'))
    <!-- Modal Agregar -->
    <!-- Modal -->
    <div class="modal fade" id="familias_agregar_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">Agregar familia</h4>
                </div>
                <form method="POST" action="{{ url('familias/agregar') }}" class="form-horizontal" id="familias_agregar_form">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Descripción</label>
                                    <div class='col-sm-9'>
                                        {{ Form::text('DESCRIP', 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">Agregar</button>
                    </div>
                </form>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    <!-- Fin Modal Agregar -->
    @endif

    @if(can('familias_editar'))
    <!-- Modal Editar -->
    <!-- Modal -->
    <div class="modal fade" id="familias_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 familia</h4>
                </div>
                <form method="POST" action="{{ url('familias/editar') }}" class="form-horizontal" id="familias_editar_form">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <input type="hidden" name="id" value="" id="etiquetas_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',  'readonly' => true]) }}
                                    </div>
                                </div>
                                <div class='form-group form-group-sm'>
                                    <label class="control-label col-sm-3 text-align-left" for="roles">Descripción</label>
                                    <div class='col-sm-9'>
                                        {{ Form::text('DESCRIP', 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">Editar</button>
                    </div>
                </form>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    <!-- Fin Modal Editar -->
    @endif

    @if(can('familias_eliminar'))
    <!-- Modal Eliminar -->
    <!-- Modal -->
    <div class="modal fade warning" id="familias_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 familia</h4>
                </div>
                <form method="POST" action="{{ url('familias/eliminar') }}" id="etiquetas_eliminar_form">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <input type="hidden" name="id" value="" id="familia_eliminar_id">
                                <div class="form-group col-sm-12">
                                    <h4>¿Está seguro de eliminar esta familia?</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

        <div class="modal fade warning" id="codigo_barras_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">Imprimir Etiqueta</h4>
                    </div>

                    <form method="POST" class="form-horizontal" id="codigo_barra_form">
                        <input type="hidden" id="id_imprimir" name="id">
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-md-12">
                                    <div id='imprime' class="form-group col-sm-12">
										 <div id='miFoto' style='display:none;'>
                                              <img id='itf'>
										 </div>
										 <div class="row">
                                              <div class="col-md-2" style='display:none;'>
                                                   <div id='nreguist2' style='display:none'></div>
                                              </div>
                                              <div class="col-md-2" style='display:none;'>
                                                   Código:
                                              </div>
                                              <div class="col-md-7" style='display:none;'>
                                                   <div id='codigoProductoPantalla2'></div>
                                              </div>
										 </div>
										 <div class="row">
                                              <div class="col-md-2" style='display:none;'>
                                                   Producto:
                                              </div>
                                              <div class="col-md-7" style='display:none;'>
                                                   <div id='nombreProductoPantalla2'></div>
                                              </div>
										 </div>
										 <div class="row" style='display:none;'>
                                              <div class="col-md-7" style='display:none;'>
                                                   <div id='loteProductoPantalla2'></div>
                                              </div>
										 </div>
                                    </div>
                                    <div id='pdf_imprimir' class="form-group col-sm-12">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                            <button type="button" class="btn btn-danger" id='barra-impresora'>Imprimir</button>
                        </div>
                    </form>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>

@endsection