<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( "orden/gridProduccion" )?>?' + s_url_param } );
    jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
}
agregarDetalle = function( IdOrdenProd )
{
    parent.buscarDetPorPrd( IdItem, Descripcion );
    parent.closeWindow();
}
</script>
<script>
    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl( 'oc/nothing' )?>',
            mtype       : "POST",
            datatype    : "json",
            colNames    : [ 'IdOrdenCab', 'Nro Orden', 'Fecha', 'Proyecto', 'Doc Ref', 'Tipo', 'Estado', '' ],
            colModel    : 
                          [
                              { name: 'IdOrdenCab',    width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                              { name: 'IdOrdenCab',    width: 50, align: 'center', editable: true, sortable: true },
                              { name: 'FechaOrden',    width: 70, align: 'center', editable: true, sortable: true, formatter:'date', formatoptions: {srcformat: 'Y-m-d', newformat:'Y-m-d'} },
                              { name: 'ProyectoDesc',  width: 150, align: 'center', editable: true, sortable: true },
                              { name: 'DocNumRef',     width: 150, align: 'center', editable: true, sortable: true },
                              { name: 'TipoOrdenDesc', width: 100, align: 'center', editable: true, sortable: true },
                              { name: 'EstadoDesc',    width: 70, align: 'center', editable: true, sortable: true },
                            {
                                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="parent.buscarDetPorPrd( \''+dos.IdOrdenCab+'\' );"></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>
