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

<style>
.grid .ui-jqgrid-htable th,
.grid .ui-jqgrid-btable .jqgrow td {
    height: 9em !important;
}
#jqGrid tr td {

}
</style>

<script>
findControl = function ()
{
    setCookie( 'FechaDocumento1', $( "#FechaDocumento1" ).val() );
    setCookie( 'FechaDocumento2', $( "#FechaDocumento2" ).val() );
    setCookie( 'OrdenEstado', $( "#OrdenEstado" ).val() );
    setCookie( 'ClienteNombre', $( "#ClienteNombre" ).val() );
    setCookie( 'VendedorOrden', $( "#VendedorOrden" ).val() );

    strFormSerialize = $( 'form' ).serialize();
    relaodGrid( strFormSerialize );

    cargarTotales( strFormSerialize );
}

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

showView = function( i_Id )
{
    location.href = "<?=buildUrl( 'pto/detailVta', 'i_Id=' )?>" + i_Id;
}

closeWindow = function( s_NameWin )
{
    $( '#' + s_NameWin ).dialog( 'close' );
}

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

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

facturarOrden = function( OrdenNum )
{
	location.href = "<?php echo buildUrlComplet( 'vta.php/pro/addedit', 's_TipoProc=nvo&ordenno=' ) ?>" + OrdenNum;
}
</script>

<script>
    function myformatter ( cellvalue, options, rowObject )
    {
        if( cellvalue )
        {
            return  '<span style="cursor:pointer;" >' + cellvalue.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, " | ") + '</span>';
        }
        return "";
    }

    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl( 'cot/index' )?>',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',
            colNames    : [ 'IdOrden', 'NroOrden', 'Fecha', 'Tipo', 'Clasifica', 'Condicion', 'Cliente', 'Mnd', 'Total', 'Estado', 'Cobranza', 'UsuReg', '' ],
            colModel    : 
                          [
                            { name: 'OrdenId',          width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { 
                                name: 'OrdenNum',         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.OrdenNum +"\");'>" + cellvalue + "</span>";
                                } 
                            },
                            { name: 'OrdenFecha',       width: 90, align: 'center', editable: true, sortable: false, formatter: 'date', formatoptions: { newformat: 'Y-m-d' } },
                            { name: 'TipoOrdenComercialDesc',    width: 90,  align: 'center', editable: true, sortable: false },
                            { name: 'TipoClasificacionDesc',    width: 90,  align: 'center', editable: true, sortable: false },
                            { name: 'FormaPagoDesc',    width: 90,  align: 'center', editable: true, sortable: false },
                            { name: 'ClienteNombre',    width: 280,  align: 'left', editable: true, sortable: false },
                            { name: 'OrdenMonedaDesc',  width: 90,  align: 'center', editable: true, sortable: false },
                            { name: 'OrdenTotalaPagar',        width: 90,  align: 'right', editable: true, sortable: false, formatter: 'number' },

                            {
                                name: 'OrdenEstadoDesc',  width: 90,  align: 'center', editable: true, sortable: false,
                                formatter: function( cellvalue, options, rowObject )
                                {
                                    if( rowObject.OrdenEstado == "F" )
                                    {
                                        var o_Data = arguments[2];
                                        return  "<span style='color:blue;text-decoration: underline;cursor:pointer;' originalValue='" + cellvalue + "' onclick='mostrarDocumentoReferencia(\""+ o_Data.DocReferenciaId +"\");'>" + cellvalue + "</span>";
                                    }
                                    else
                                    {
                                        return cellvalue;
                                    }
                                }
                            },
                            { name: 'CobranzaEstadoDesc',        width: 90,  align: 'center', editable: true, sortable: false },

                            { name: 'VendedorOrden',    width: 70,  align: 'center', editable: true, sortable: false },

                            { name: 'details', width: 50, align: 'center', sortable: false, search: false,
                                formatter:function()
                                {
                                    return '<?=addImgButton( 'btnReview54.gif', array( "style" => "cursor:pointer;", "onclick"=>"showView( $(this).parent().parent()[0].id );" ) )?>';
                                }
                            }
                          ],
			viewrecords : true,
			height      : $(window).height()-270,
			width       : $(window).width() - 20,
            //width       : 600,
            autowidth   : true,
            sortable    : true,
            multiSort   : true,
            gridview    : 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",
            shrinkToFit : false,
            //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
    (
        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 ) )?>

<script>
if( getCookie( 'FechaDocumento1' ) ){ $( "#FechaDocumento1" ).val( getCookie( 'FechaDocumento1' ) ); }
if( getCookie( 'FechaDocumento2' ) ){ $( "#FechaDocumento2" ).val( getCookie( 'FechaDocumento2' ) ); }
if( getCookie( 'OrdenEstado' ) ){ $( "#OrdenEstado" ).val( getCookie( 'OrdenEstado' ) ); }
if( getCookie( 'ClienteNombre' ) ){ $( "#ClienteNombre" ).val( getCookie( 'ClienteNombre' ) ); }
if( getCookie( 'VendedorOrden' ) ){ $( "#VendedorOrden" ).val( getCookie( 'VendedorOrden' ) ); }
</script>