<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('tipodocumentoalmacen/grid','block=001')?>?' + s_url_param } );
        jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
    }
    
    showView = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'tipodocumentoalmacen/viewdel', 'block=001&s_TipoProc=shw&i_Id=' )?>' + i_Id );
        $( '#dialog' ).dialog( 'open' );
    }
    
    deleteRow = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'tipodocumentoalmacen/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 jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl('tipodocumentoalmacen/grid','block=001')?>',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',                        
            colNames    : [ 'IdTipoDocumento', 'Código', 'Código Sunat', 'Descripción', 'Estado','', '', '' ],
            colModel    : 
                          [
                            { name: 'IdTipoDocumento', width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'IdTipoDocumento', width: 60, align: 'center', editable: false, sortable: true },
                            { name: 'CodigoSunat',     width: 60, align: 'center', editable: false, sortable: true },
                            { name: 'Descripcion',     width: 210, align: 'left', editable: false, sortable: true },
                            { name: 'Estado',          width: 120, align: 'center', editable: false, 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' title='Editar Tipo de Documento' onclick='location.href=\"<?=buildUrl('tipodocumentoalmacen/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' title='Ver detalles del Tipo de Documento' 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' title='Eliminar Tipo de Documento' onclick='deleteRow( $(this).parent().parent()[0].id );'></span>"
                                }
                            }
                          ],
			viewrecords : true,
			height      : $(window).height()-202, 
			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()
            {
                // 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;
            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>
<div id="dialog"><iframe id="myIframe" src="" frameborder="0" height="100%" width="100%"></iframe></div>
<div id="dialog2"><iframe id="myIframe2" src="" frameborder="0" height="100%" width="100%"></iframe></div>

<script>
    objMainTab.modalUIDialog( "#dialog", { title : "Resumen", width : 700, height  : 350 } );
    objMainTab.modalUIDialog( "#dialog2", { title : "Eliminar", width : 700, height  : 350 } );
</script>