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

<script>
buscarRutaAsignada = function()
{
	findControl();
}
findControl = function ()
{
    strFormSerialize = $( 'form' ).serialize();
    relaodGrid( strFormSerialize );
}

relaodGrid = function( s_url_param )
{
    jQuery( '#jqGrid' ).jqGrid( 'clearGridData' );
    jQuery( '#jqGrid' ).jqGrid( 'setGridParam', { url: '<?=buildUrl( 'transporte/gridAtte' )?>?' + s_url_param } );
    jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
}

showView = function( Id )
{
	var s_param_ajax = "TipoProceso/DespachoInmediato/IdMovimientoSerie/" + Id;
	$( '#myIframe' ).attr( 'src', '<?=buildUrl( 'transporte/summaryAtte' )?>' + s_param_ajax );
    $( '#dialog' ).dialog( 'open' );
}

imprimirGuiaRemision = function( Id )
{
    var s_param_ajax = "TipoProceso/DE/IdMovimientoSerie/" + Id;
    $( '#myIframe4' ).attr( 'src', '<?=buildUrl( 'transporte/printGuiaRem' )?>' + s_param_ajax );
    $( '#dialog4' ).dialog( 'open' );
}

mostrarDocumentoReferencia = function( Id )
{
	var s_param_ajax = "s_TipoProc/shw/IdNumDoc/" + Id;
    $( '#myIframe4' ).attr( 'src', '<?=buildUrlComplet( 'vta.php/pro/detailVta' )?>' + s_param_ajax );
    $( '#dialog4' ).dialog( 'open' );
}

mostrarDocumentoCotizacion = function( Id )
{
    var s_param_ajax = "readonly/1/OrdNum/" + Id;
    $( '#myIframe4' ).attr( 'src', '<?=buildUrlComplet( 'vta.php/cot/detailVta' )?>' + s_param_ajax );
    $( '#dialog4' ).dialog( 'open' );
}

closeWindow = function()
{
    $( '#dialog' ).dialog( 'close' );
    $( '#dialog4' ).dialog( 'close' );
}
</script>

<script>
    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : 'nothing',
            mtype       : "POST",
            datatype    : "json",
            //editurl     : 'edicionData.php',                        
            colNames    : [ 'Id', 'Doc Cliente', 'Cliente', 'Doc Ref', 'Comprobante', 'Fecha', 'Tipo Comp', 'UsuarioAprob', 'FechaAprob', '', '' ],
            colModel    : 
                          [
                            { name: 'MovimientoId', width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'DocumentoCliente', width: 90, align: 'center', editable: true, sortable: false },
                            { name: 'NombreCliente', width: 200, align: 'center', editable: true, sortable: false },
                            { 
                                name: 'ReferenciaNumeroSecuencia',  width: 90, align: 'center', editable: true, sortable:  false,
                                formatter: function( cellvalue, options, rowObject )
                                {
                                	var o_Data = arguments[2];
                                	return  "<span style='color:blue;text-decoration: underline;cursor:pointer;' originalValue='" + cellvalue + "' onclick='mostrarDocumentoCotizacion(\""+ o_Data.ReferenciaNumeroSecuencia +"\");'>" + cellvalue + "</span>";
                                }
                            },
                            { 
                                name: 'DocumentoReferencia',        width: 90, align: 'center', editable: true, sortable:  false,
                                formatter: function( cellvalue, options, rowObject )
                                {
                                	var o_Data = arguments[2];
                                	return  "<span style='color:blue;text-decoration: underline;cursor:pointer;' originalValue='" + cellvalue + "' onclick='mostrarDocumentoReferencia(\""+o_Data.ReferenciaId+"\");'>" + cellvalue + "</span>";
                                } 
                            },
                            { name: 'ReferenciaFecha',              width: 90, align: 'center', editable: true, sortable:  false, formatter: 'date', formatoptions: { newformat: 'Y-m-d' } },
                            { name: 'ConcatComercialClasificacion', width: 90, align: 'center', editable: true, sortable:  false },
                            
                            { name: 'UsuarioAprob',                   width: 90,  align: 'center', editable: true, sortable: false },
                            { name: 'FechaAprob',          width: 90, align: 'center', editable: true, sortable: false, formatter: 'date', formatoptions: { newformat: 'Y-m-d' } },

                            { 
                                name: 'details', width: 25, sortable: false, search: false, align: 'center',
                                formatter:function()
                                {
                                	var o_Data = arguments[2];
                                    return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-document' title='' onclick='showView(\""+o_Data.MovimientoId+"\");'></span>"
                                }
                            },
                            { 
                                name: 'print', width: 25, sortable: false, search: false, align: 'center',
                                formatter:function()
                                {
                                    var o_Data = arguments[2];
                                    if( o_Data.ImpresionEstado != "C" )
                                    {
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-print' title='' onclick='imprimirGuiaRemision(\"" + o_Data.MovimientoId + "\");'></span>"
                                    }
                                    else
                                    {
                                        return "";
                                    }
                                }
                            }

                            //AQUI SE TIENE Q AGREGAR EL CONTRO DE SERIE, SE QUE ESCANEAR SI ES SOPORTADO

                          ],
			viewrecords : true,
			height      : $( window ).height()-200, 
			width       : $( window ).width() - 17,
            //width       : 600,
            //autowidth   : true,
            sortable    : true,
            multiSort   : true,
            //width: null,
            shrinkToFit : false,
            emptyrecords: "No se Encontro registros",
            //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 )
            {
                //otra manera de realizar un evento por columnas
            	if( iCol == 2 ) //Codigo del Item
                {
                    var a_ids = explode( "_", rowid );
                    console.log( arguments );
                    console.log( a_ids[0] );
            		//showView( a_ids[0], a_ids[1], '1' );
                }
            },
            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
    (
        function()
        {
            findControl();
            //$( "#jqGrid" ).parents( 'div.ui-jqgrid-bdiv' ).css( "height", ($(window).height()-180) );
        },
        300
    );
</script>
<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"", "modal"=>true ) )?>
<?=crearFuentePopupIframe( array( "div_id"=>"dialog4", "iframe_id"=>"myIframe4", "popup_title"=>"Imprimir Guia", "modal"=>true ) )?>