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

<script>
    findControl = function ()
    {
    	strFormSerialize = $( '#accordion :input' ).serialize();
        relaodGrid( strFormSerialize );
    }
    
    relaodGrid = function( s_url_param )
    {
        jQuery( '#jqGrid' ).jqGrid( 'clearGridData' );
        jQuery( '#jqGrid' ).jqGrid( 'setGridParam', { url: '<?=buildUrl('unidad/grid','block=001')?>?' + s_url_param } );
        jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
    }
    
    showView = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'unidad/viewdel', 'block=001&s_TipoProc=shw&i_Id=' )?>' + i_Id );
        $( '#dialog' ).dialog( 'open' );
    }
    
    deleteRow = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'unidad/viewdel', 'block=001&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 s_Tipo_Valores = { value: "L:Litros;M:Masa;U:Unidad" };

    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl('unidad/nothing' )?>',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',                        
            colNames    : [ 'IdUnidad', 'Codigo', 'Cod Sunat', 'Cod Web', 'Descripcion', 'Tipo de Unidad', 'Numero de decimales', 'Estado','', '', '' ],
            colModel    : 
                          [
                            { name: 'IdUnidad',          width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'IdUnidad',          width: ($( window ).width())/15, align: 'center', editable: true, sortable: true },
                            { name: 'CodigoSunat',       width: ($( window ).width())/15, align: 'center', editable: true, sortable: true },
                            { name: 'DescripcionExtranjera',       width: ($( window ).width())/15, align: 'center', editable: true, sortable: true },
                            { name: 'DescripcionCorta',  width: ($( window ).width())/5, align: 'left', editable: true, sortable: true },
                            { name: 'UnidadTipo',        width: ($( window ).width())/10,  align: 'center',   editable: true, formatter: 'select', edittype:"select", editoptions: s_Tipo_Valores },
                            { name: 'NumeroDecimales',   width: ($( window ).width())/10,  align: 'center', editable: true },
                            { name: 'Estado',            width: ($( window ).width())/10, align: 'center', editable: true, formatter: 'select', edittype:"select", editoptions: s_Estado_Valores },
                            
                            { name: 'edit', width: 20, sortable: false, search: false, align: 'center',
                                formatter:function()
                                {
                                    return "<span style='cursor:pointer;' class='ui-icon ui-icon-pencil' onclick='location.href=\"<?=buildUrl('unidad/addedit', 'block=001&s_TipoProc=upd&i_Id=' )?>\" + $(this).parent().parent()[0].id;'></span>"
                                }
                            },
                            { name: 'details', width: 20, sortable: false, search: false, align: 'center',
                                formatter:function()
                                {
                                    return "<span style='cursor:pointer;' class='ui-icon ui-icon-document' onclick='showView( $(this).parent().parent()[0].id );'></span>"
                                }
                            },
                            { name: 'del', width: 20, sortable: false, search: false, align: 'center',
                                formatter:function()
                                {
                                    return "<span style='cursor:pointer;' class='ui-icon ui-icon-trash' onclick='deleteRow( $(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()
            {
                // rowid, iCol, cellcontent
                //console.log( arguments );
            },
            beforeSelectRow: function ( rowid, e ) 
            {
                /*
                console.log( arguments );
                var iCol = $.jgrid.getCellIndex( e.target );
                if ( iCol >= firstButtonColumnIndex ) 
                {
                    alert( "rowid=" + rowid + "\nButton name: " + buttonNames[iCol] );
                }
                // prevent row selection if one click on the button
                return ( iCol >= firstButtonColumnIndex ) ? false: true;
                */
            }
        }
    );

    $( "#jqGrid" ).jqGrid( 'bindKeys' );
    
    /*
    $('#jqGrid').navGrid
    ( '#jqGridPager',
        // the buttons to appear on the toolbar of the grid
        { edit: true, add: true, del: true, search: false, refresh: false, view: true, position: "left", cloneToTop: false },
        // options for the Edit Dialog
        {
            beforeShowForm: function( form ) 
            {
                console.log( arguments );
                var dlgDiv = $("#editmod" + jqGridContent[0].id);
                var parentDiv = dlgDiv.parent(); // div#gbox_list
                var dlgWidth = dlgDiv.width();
                var parentWidth = parentDiv.width();
                var dlgHeight = dlgDiv.height();
                var parentHeight = parentDiv.height();
                // change parentWidth and parentHeight in case of the grid
                //       is larger as the browser window
                dlgDiv[0].style.top = Math.round((parentHeight-dlgHeight)/2) + "px";
                dlgDiv[0].style.left = Math.round((parentWidth-dlgWidth)/2) + "px";
            },
            editCaption   : "The Edit Dialog",
            recreateForm  : false,
			checkOnUpdate : true,
			checkOnSubmit : true,
            closeAfterEdit: true,
            width         : 800,
            errorTextFormat: function ( data ) 
            {
                return 'Error: ' + data.responseText
            }
        },
        // options for the Add Dialog
        {
            closeAfterAdd: true,
            recreateForm: true,
            errorTextFormat: function (data) 
            {
                return 'Error: ' + data.responseText
            }
        },
        // options for the Delete Dailog
        {
            errorTextFormat: function (data) {
                return 'Error: ' + data.responseText
            }
        }
    );
    */
    
    $(window).on
    ( 
        "resize", 
        function () 
        {
            var newWidth = $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() - 20;
            jqGridContent.jqGrid( "setGridWidth", newWidth, true);
        }
    );
    setTimeout( "findControl();", 300 );
</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 ) )?>