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

<script>
    findControl = function ()
    {
        strFormSerialize = $( 'form' ).serialize();
        relaodGrid( strFormSerialize );
    }
    
    relaodGrid = function( s_url_param )
    {
        jQuery( '#jqGrid' ).jqGrid( 'clearGridData' );
        jQuery( '#jqGrid' ).jqGrid( 'setGridParam', { url: '<?=buildUrl( 'listofertas/grid' )?>?' + s_url_param } );
        jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
    }
    
    showView = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'listofertas/viewdel', 's_TipoProc=shw&i_Id=' )?>' + i_Id );
        $( '#dialog' ).dialog( 'open' );
    }
    
    deleteRow = function( i_Id )
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'listofertas/viewdel', 's_TipoProc=dlt&i_Id=' )?>' + i_Id );
        $( '#dialog' ).dialog( 'open' );
    }

    publicPromo = function( i_Id )
    {
        x = confirm( "Esta seguro de Publicar la Promoción?" );
        if( x )
        {
            //blockContent( "content_nvo" );
            var strFormSerialize = "TipoProc=PublicarPromocion&IdPrGrupo=" + i_Id;
            executeAjaxJson( 
                                "<?php echo buildUrl( 'listofertas/request' ) ?>", 
                                strFormSerialize,
                                function( o_json )
                                {
                                    console.log( o_json );
                                    //unBlockContent( "content_nvo" );
                                    if( o_json.error )
                                    {
                                        alert( o_json.mensaje_error );
                                        return false;
                                    }
                                    findControl();
                                } 
                           );
        }
    }

    reAbrirPromo = function( i_Id )
    {
        x = confirm( "Esta seguro de Re-Abrir la Promoción?" );
        if( x )
        {
            //blockContent( "content_nvo" );
            var strFormSerialize = "TipoProc=ReAbrirPromocion&IdPrGrupo=" + i_Id;
            executeAjaxJson( 
                                "<?php echo buildUrl( 'listofertas/request' ) ?>", 
                                strFormSerialize,
                                function( o_json )
                                {
                                    console.log( o_json );
                                    //unBlockContent( "content_nvo" );
                                    if( o_json.error )
                                    {
                                        alert( o_json.mensaje_error );
                                        return false;
                                    }
                                    findControl();
                                } 
                           );
        }
    }
    
    closeWindow = function( s_NameWin )
    {
        $( '#' + s_NameWin ).dialog( 'close' );
    }
</script>

<script>
    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl( 'listofertas/grid' )?>',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',                        
            colNames    : [ 'IdConcat', 'Promoción', 'Descripción', 'Inicio', 'Expiración', 'Sucursal', 'Estado', '', '', '', '' ],
            colModel    : 
                          [
                            { name: 'IdPrGrupo',        width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'IdPrGrupo',        width: ( $( window ).width() )/15, align: 'center', editable: false, sortable: false },
                            { name: 'DescripcionPromo', width: ( $( window ).width() )/5,  align: 'left', editable: false },
                            { name: 'FechaInicio',      width: ( $( window ).width() )/15, align: 'center',   editable: false, sortable: false, formatter: 'date', formatoptions: { newformat: 'Y-m-d' } },
                            { name: 'FechaExpiracion',  width: ( $( window ).width() )/15, align: 'center', editable: false, sortable: false, formatter: 'date', formatoptions: { newformat: 'Y-m-d' } },
                            { name: 'SucursalDesc',     width: ( $( window ).width() )/15, align: 'center', editable: false, sortable: false },
                            { name: 'EstadoDesc',       width: ( $( window ).width() )/15, align: 'center', editable: false, sortable: false },
                            
                            { name: 'edit', width: 17, sortable: false, search: false,
                                formatter:function()
                                {
                                	var o_Data = arguments[2];
                                	if( o_Data.Estado == "P" )
                                	{
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-pencil' onclick='location.href=\"<?=buildUrl('listofertas/addedit', 's_TipoProc=upd&i_Id=' )?>\" + $(this).parent().parent()[0].id;'></span>"
                                	}
                                    return "";
                                }
                            },
                            { name: 'details', width: 17, sortable: false, search: false, 
                                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: 'public', width: 17, sortable: false, search: false, 
                                formatter:function()
                                {
                                	var o_Data = arguments[2];
                                	if( o_Data.Estado == "P" )
                                	{
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-check' onclick='publicPromo( $(this).parent().parent()[0].id );' title='Publicar Promoción'></span>"
                                	}
                                	if( o_Data.Estado == "B" )
                                	{
                                        //return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-arrowreturnthick-1-w' onclick='reAbrirPromo( $(this).parent().parent()[0].id );' title='Cancelar Publicación de la Promoción'></span>"
                                	}
                                    return "";
                                }
                            },
                            { name: 'del', width: 17, sortable: false, search: false,
                                formatter:function()
                                {
                                	var o_Data = arguments[2];
                                	if( o_Data.Estado == "P" || o_Data.Estado == "B" )
                                	{
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-trash' onclick='deleteRow( $(this).parent().parent()[0].id );' title='Anular Promoción'></span>"
                                	}
                                    return "";
                                }
                            }
                          ],
            viewrecords : true,
            height      : $(window).height()-150, 
            width       : $(window).width() - 20,
            //width       : 600,
            //autowidth   : true,
            sortable    : true,
            multiSort   : true,
            //width: null,
            //shrinkToFit: false,
            emptyrecords: "No se encontro registros",
            //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>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"", "popup_width"=>"700", "popup_height"=>"380", "modal"=>true ) )?>
