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

<script>
    findControl = function ( IdSucursal )
    {
        strFormSerialize = $( '#form1' ).serialize();
        relaodGrid( strFormSerialize );
    }
    
    relaodGrid = function( s_url_param )
    {
        jQuery( '#jqGrid' ).jqGrid( 'clearGridData' );
        jQuery( '#jqGrid' ).jqGrid( 'setGridParam', { url: '<?=buildUrl('addemp/gridProd','block=001')?>?' + s_url_param } );
        jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
    }
    
    showEdit = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'addemp/nvoUniProd', 'block=001&s_TipoProc=upd&i_Id='.$i_Id.'&i_IdDet=' )?>' + i_Id );
        $( '#dialog' ).dialog( 'open' );
    }
    
    showView = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'addemp/showUniProd', 'block=001&s_TipoProc=shw&i_Id='.$i_Id.'&i_IdDet=' )?>' + i_Id );
        $( '#dialog' ).dialog( 'open' );
    }
    
    deleteRow = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'addemp/showUniProd', 'block=001&s_TipoProc=dlt&i_Id='.$i_Id.'&i_IdDet=' )?>' + 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: "EST:Estanque;LAG:Laguna" };

    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl( 'addemp/gridProd','block=001&IdCompania='.$i_Id )?>',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',                        
            colNames    : [ 'Id', 'Descripción', 'Exonerado', 'Telefono', 'Estado', '# Cant Alm', '', '', '' ],
            colModel    : 
                          [
                            { name: 'IdSucursal',            width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'Descripcion',           width: ( $( window ).width() ) / 3, align: 'left',   editable: true },
                            { name: 'ExoneradorIGVDesc',     width: ( $( window ).width() ) / 10, align: 'center',   editable: false },
                            { name: 'Telefono',              width: ( $( window ).width() ) / 10, align: 'center',   editable: false },
                            { name: 'Estado',                width: ( $( window ).width() ) / 10, align: 'center', editable: true, formatter: 'select', edittype:"select", editoptions: s_Estado_Valores },
                            { name: 'CantAlmacen',           width: ( $( window ).width() ) / 10, align: 'center', editable: true },
                            
                            { 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' onclick='showEdit( $(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' 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' 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      : 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;
            jqGridContent.jqGrid( "setGridWidth", newWidth, true);
        }
    );
</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 : "Sucursal", width : 890, height  : 550 } );
    objMainTab.modalUIDialog( "#dialog2", { title : "Eliminar Unidad de Producción", width : 650, height  : 400 } );
</script>