<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( "pro/gridValorizacion" )?>?' + s_url_param } );
        jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
    }
    agregarDetalle = function( IdItem, Descripcion )
    {
        parent.asignarItem( IdItem, Descripcion );
        parent.closeWindow();
    }

    asignarValorizacion = function( DevolFactId, IdProyecto, Codigo, Descripcion, SubTotal, IGV, Total )
    {
        parent.asignarValorizacion( DevolFactId, IdProyecto, Codigo, Descripcion, SubTotal, IGV, Total );
    }
</script>
<script>
    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl( 'oc/nothing' )?>',
            mtype       : "POST",
            datatype    : "json",
            colNames    : [ 'DevolFactId', 'Proyecto', 'Estado', 'Codigo', 'Servicio', 'Fecha', 'Base Imponible', '' ],
            colModel    :
                [
                    { name: 'DevolFactId',      width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                    { name: 'ProyectoDesc',     width: 90, align: 'center', editable: false, sortable: false },
                    { name: 'EstadoDesc',       width: 90, align: 'center', editable: false, sortable: false },
                    { name: 'IdItem',           width: 60, align: 'center', editable: false, sortable: false },
                    { name: 'DescripcionItem',  width: 200, align: 'center', editable: false, sortable: false },
                    { name: 'FechaCorte',       width: 90, align: 'center', editable: false, sortable: false, formatter: 'date', formatoptions: { newformat: 'Y-m-d' } },
                    { name: 'SubTotal',       width: 90,  align: 'center', editable: true, sortable: false },
                    {
                        name: 'agree',          width: 30,  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 Referencia" onclick="asignarValorizacion( \''+dos.DevolFactId+'\', \''+dos.IdProyecto+'\', \''+dos.IdItem+'\', \''+dos.DescripcionItem+'\', \''+dos.SubTotal+'\', \''+dos.MontoIGV+'\', \''+dos.MontoTotal+'\' );"></span>';
                        }
                    }
                ],
            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 )
            {
                pr( arguments );
            },
            onCellSelect: function( rowid, iCol, cellcontent )
            {
                //pr( arguments );
            },
        }
    );

    $( "#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);
    }

    //pr( $( "#jqGrid" ).parents( 'div.ui-jqgrid-bdiv' ) );
    //pr( $(window).height() );
    //$( "#jqGrid" ).parents( 'div.ui-jqgrid-bdiv' ).css( "height", "auto" );

    setTimeout
    (
        function()
        {
            findControl();
            $( "#jqGrid" ).parents( 'div.ui-jqgrid-bdiv' ).css( "height", ($(window).height()-180) );
        },
        300
    );
</script>
