<table id="jqGrid"></table>
<div id="jqGridPager"></div>

<script>
findControl = function ()
{
    //strFormSerialize = $( 'form' ).serialize();
    strFormSerialize = $( '#accordion :input' ).serialize();
    relaodGrid( strFormSerialize );
}

relaodGrid = function( s_url_param )
{
    jQuery( '#jqGrid' ).jqGrid( 'clearGridData' );
    jQuery( '#jqGrid' ).jqGrid( 'setGridParam', { url: '<?=buildUrl( 'os/grid' )?>?' + s_url_param } );
    jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
}

showView = function( i_Id )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'oc/viewdel', 's_TipoProc=shw&i_Id=' )?>' + i_Id );
    $( '#dialog' ).dialog( 'open' );
}

deleteRow = function( i_Id )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'oc/viewdel', 's_TipoProc=dlt&i_Id=' )?>' + i_Id );
    $( '#dialog' ).dialog( 'open' );
}

closeWindow = function( s_NameWin )
{
    $( '#' + s_NameWin ).dialog( 'close' );
}
</script>

<script>
    var s_Estado_Valores = { value: "A:Activo;I:Inactivo" };

    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : 'nothing',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',                        
            colNames    : [ 'IdOrdenCompraConcat', 'Nro Orden', 'Proveedor', 'Moneda', 'Monto', 'Fecha Crea', 'Estado', '', '', '' ],
            colModel    : 
                          [
                            { name: 'IdOrdenCompraConcat',  width: 0,   align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'IdOrdenCompra',        width: 60,  align: 'center', editable: true, sortable: true },
                            { name: 'NombreProveedor',      width: 210, align: 'left', editable: true, sortable: true },
                            { name: 'MonedaDesc',           width: 90, align: 'center', editable: true, sortable: true },
                            { name: 'FormatMontoBruto',     width: 90,  align: 'right', editable: true, sortable: true },
                            { name: 'FechaCreacion',        width: 90, align: 'center', editable: false, sortable: true, formatter: function( cellvalue ){ return cellvalue.substring(0, 10); } },
                            { name: 'EstadoDesc',           width: 90, align: 'center', editable: true, sortable: true },
                            
                            { 
                                name: 'edit', width: 25, align: 'center', sortable: false, search: false,
                            	cellattr: function () { return ' title="Editar Orden de Servicio"'; },
                                formatter:function()
                                {
                                    var o_Data = arguments[2];
                                    if( o_Data.Estado == "PR" )
                                    {
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-pencil' title='Editar Orden de Servicio' onclick='location.href=\"<?=buildUrl('os/addedit','s_TipoProc=upd&i_Id=')?>\" + $(this).parent().parent()[0].id;'></span>";
                                    }
                                    return "";
                                }
                            },
                            { name: 'details', width: 25, sortable: false, search: false, align: 'center',
                                formatter:function()
                                {
                                	return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-document' title='Ver detalles de Orden de Servicio' onclick='showView( $(this).parent().parent()[0].id );'></span>";
                                }
                            },
                            { 
                                name: 'del', width: 20, align: 'center', sortable: false, search: false,
                            	cellattr: function () { return ' title="Eliminar"'; },
                                formatter:function()
                                {
                                    var o_Data = arguments[2];
                                    if( o_Data.Estado != "AN" && o_Data.Estado != "CO" )
                                    {
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-trash' title='Eliminar Orden de Compra' onclick='deleteRow( $(this).parent().parent()[0].id );'></span>";
                                    }
                                    return "";
                                }
                            },
                          ],
            viewrecords : true,
            height      : $(window).height() - 205, 
            width       : $(window).width()  - 20,
            //width       : 600,
            //autowidth   : true,
            sortable    : true,
            multiSort   : true,
            //width: null,
            shrinkToFit: false,
            emptyrecords: "No se encontro registro",
            //height      : 480,
            rowNum      : 15,
            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();", 500 );
</script>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog",  "iframe_id"=>"myIframe",  "popup_title"=>"Resumen", "modal"=>true ) )?>
<?=crearFuentePopupIframe( array( "div_id"=>"dialog2",  "iframe_id"=>"myIframe2",  "popup_title"=>"Eliminar", "modal"=>true ) )?>