<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/grid', '' )?>?' + s_url_param } );
        jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
    }
    
    editOrden = function( i_Id )
    {
    	location.href = "<?=buildUrl( 'orden/addedit', 'TipoProc=upd&IdOrdenCab=' )?>" + i_Id;
    }
    
    showOrdenPrd = function( i_Id )
    {
    	$("span.ui-dialog-title").text( 'Orden Produccion' ); 
    	$( '#myIframe' ).attr( 'src', '<?=buildUrl( 'orden/addedit', 'TipoProc=shw/IdOrdenCab=' )?>' + i_Id );
        $( '#dialog' ).dialog( 'open' );
    }
    
    closeWindow = function()
    {
        $( '#dialog' ).dialog( 'close' );
    }
</script>

<script>
    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl( 'orden/nothing' )?>',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',                        
            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: 'details',       width: 30, sortable: false, search: false, align: 'center',
                                formatter:function( o_cellvalue, o_Opt, o_Row )
                                {
                                    if( o_Row.Estado == "A" || o_Row.Estado == "C" )
                                    {
                                    	return "<span style='cursor:pointer;' class='ui-icon ui-icon-document' onclick='showOrdenPrd( \""+o_Row.IdOrdenCab+"\" );'></span>"
                                    }
                                    else
                                    {
                                        return "<span style='cursor:pointer;' class='ui-icon ui-icon-pencil' onclick='editOrden( $(this).parent().parent()[0].id );'></span>"
                                    }
                                }
                            }
                        ],
            viewrecords : true,
            height      : $(window).height()-200, 
            width       : $(window).width() - 20,
            //width       : 600,
            //autowidth   : true,
            sortable    : true,
            multiSort   : true,
            //width: null,
            shrinkToFit : false,
            emptyrecords: "Nothing to display",
            //height      : 480,
            rowNum      : 100,
            rownumbers  : true, // show row numbers
            //multiselect : true,//SELECCION MULTIPLE
            pager       : "#jqGridPager",
            //caption     : "Products / Category Primary Grid View"
            onCellSelect: function()
            {
            },
            beforeSelectRow: function ( rowid, e ) 
            {
            }
        }
    );

    $( "#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( "findControl();", 300 );
</script>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"Recibo", "modal"=>true ) )?>