<table id="jqGrid"></table>
<div id="jqGridPager"></div>
<script>
findControl = function ()
{
    strFormSerialize = $( 'form' ).serialize();
    relaodGrid( strFormSerialize );
}

relaodGrid = function( s_url_param )
{
    jQuery( '#jqGrid' ).jqGrid( 'clearGridData' );
    jQuery( '#jqGrid' ).jqGrid( 'setGridParam', { url: '<?=buildUrl( "asoguias/docrefGrid" )?>?' + s_url_param } );
    jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
}
agregarDetalle = function( IdItem, Descripcion )
{
    parent.asignarItem( IdItem, Descripcion );
    parent.closeWindow();
}
</script>
<script>
    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl( 'oc/nothing' )?>',
            mtype       : "POST",
            datatype    : "json",
            colNames    : [ 'ObligacionesId', 'Proveedor', 'Tipo Doc', 'Serie-Num', 'Fecha', 'SubTotal', 'IGV', 'Total', '' ],
            colModel    : 
                          [
                            { name: 'ObligacionesId',          width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'ProveedorNombre',         width: 150, align: 'center', editable: false, sortable: false },
                            { name: 'TipoDocumentoDesc',    width: 100, align: 'center', editable: false, sortable: false },
                            { name: 'DocumentoSerieNumero',  width: 100, align: 'center', editable: false, sortable: false },
                            { name: 'FechaEmision',         width: 100, align: 'center', editable: false, sortable: false },
                            { name: 'MontoAfecto',         width: 90,  align: 'center', editable: true, sortable: false, formatter: 'number' },
                            { name: 'MontoIGV',           width: 100, align: 'center', editable: false, sortable: false, formatter: 'number' },
                            { name: 'MontoTotal',         width: 100, align: 'center', editable: false, sortable: false, formatter: 'number' },
                            {
                                name: 'agree',              width: 25,  align: 'center', sortable: false, search: false, 
                                formatter:function()
                                {
                                    var dos = arguments[2];
                                    //return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-circle-triangle-e' title='Agregar Item' onclick='agregarDetalle( $(this).parent().parent()[0].id, $(this).parent().parent()[0].cells[4].innerHTML );'></span>"
                                    return '<span style="cursor:pointer; margin: 0 auto;" class="ui-icon ui-icon-circle-triangle-e" title="Agregar Referencia" onclick="parent.asignarReferenciaParaNotaCred( \''+dos.ObligacionesId+'\', \''+dos.DocumentoSerieNumero+'\' );"></span>'; //\''+dos.IdLote+'\',
                                }
                            }
                          ],
            viewrecords : true,
            height      : $(window).height()-180, 
            width       : $(window).width() - 10,
            sortable    : true,
            multiSort   : true,
            emptyrecords: "No se encontro registros",
            rowNum      : 100,
            rownumbers  : true, // show row numbers
            pager       : "#jqGridPager",
            shrinkToFit : false,
            ondblClickRow: function( rowId, param2, param3, param4 ) 
            {
                
            },
            onCellSelect: function( rowid, iCol, cellcontent )
            {
                
            },
        }
    );
    
    $( "#jqGrid" ).jqGrid( 'bindKeys' );
    
    $( window ).on
    ( 
        "resize", 
        function () 
        {
            var newWidth = $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() - 20;
            if( newWidth <= 705 )
            {
                jqGridContent.jqGrid( "setGridWidth", newWidth, false );
            }
            else
            {
                jqGridContent.jqGrid( "setGridWidth", newWidth, true);
            }
        }
    );
    if( $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() <= 705 )
    {
        jqGridContent.jqGrid( "setGridWidth", $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() - 5, false );
        //jqGridContent.jqGrid( "setGridWidth", $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().height() - 270, false );
    }
    else
    {
        jqGridContent.jqGrid( "setGridWidth", $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() - 5, true);
    }
    
    setTimeout
    (
        function()
        {
            findControl();
        },
        300
    );
</script>
