<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('almacen/grid','block=001')?>?' + s_url_param } );
        jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
    }
    
    showView = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'almacen/viewdel', 'block=001&s_TipoProc=shw&i_Id=' )?>' + i_Id );
        $( '#dialog' ).dialog( 'open' );
    }
    
    deleteRow = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'almacen/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_Estado_Valores2 = { value: "S:SI;N:NO" };

    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl('almacen/grid','IdCompania='.myUser::getUser()->getIdCompania())?>',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',                        
            colNames    : [ 'Codigo',  'Alm Descripcion', 'Empresa', 'Descripcion Sucursal', 'Almacen Venta', 'Estado','', '', '' ],
            colModel    : 
                          [
                            { name: 'IdAlmacen',             width: 90, align: 'center', editrules : { required: true }, key: true, sortable: false },
                            { name: 'DescripcionLocal',      width: 210, align: 'left', editable: true, sortable: true },
                            { name: 'CompaniaDesc',          width: 210, align: 'left', editable: false, sortable: true },
                            { name: 'DescripcionSucursal',   width: 210, align: 'left', editable: true, sortable: true },
                            
                            { name: 'AlmacenVentaFlag',      width: 60, align: 'center', editable: true, sortable: true , formatter: 'select', edittype:"select", editoptions: s_Estado_Valores2},
                            /*{ name: 'AlmacenProduccionFlag', width: ($( window ).width())/10, align: 'center', editable: true, sortable: true ,  formatter: 'select',edittype:"select", editoptions: s_Estado_Valores2},*/
                            { name: 'Estado',                width: 90, 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; margin: 0 auto;' class='ui-icon ui-icon-pencil' title='Editar Almacén' onclick='location.href=\"<?=buildUrl('almacen/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; margin: 0 auto;' class='ui-icon ui-icon-document' title='Ver detalles de Almacén' 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; margin: 0 auto;' class='ui-icon ui-icon-trash' title='Eliminar Almacén' onclick='deleteRow( $(this).parent().parent()[0].id );'></span>"
                                }
                            }
                          ],
			viewrecords : true,
			height      : $(window).height()-210,
			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>
<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  : 420 } );
    objMainTab.modalUIDialog( "#dialog2", { title : "Eliminar", width : 700, height  : 350 } );
</script>