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

<?php
if( !isset( $receipt ) ){ $receipt = 0; }
if( !isset( $approv ) ){ $approv = 0; }
if( !isset( $import ) ){ $import = 0; }
if( !isset( $roladmin ) ){ $roladmin = 0; }

if( !isset( $edit ) ){ $edit = 0; }
if( !isset( $view ) ){ $view = 0; }
if( !isset( $delete ) ){ $delete = 0; }
?>
?>

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

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

showView = function( i_Id )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'oc/viewdel', 's_TipoProc=shw&i_Id=' )?>' + i_Id );
    $( '#dialog' ).dialog( 'open' );
}

deleteRow = function( i_Id )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'oc/viewdel', 's_TipoProc=dlt&i_Id=' )?>' + i_Id );
    $( '#dialog' ).dialog( 'open' );
}

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

showViewAprob = function( i_Id )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'oc/viewdel', 's_TipoProc=apr&i_Id=' )?>' + i_Id );
    $( '#dialog' ).dialog( 'open' );
}

showViewImport = function( i_Id )
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'import/addeditImport', 'TipoProc=upd&Id=' )?>' + i_Id );
    $( '#dialog' ).dialog( 'open' );
}

cambiarEstadoAprovado = function( i_Id )
{
    x = confirm( "Se procedera a cambiar la OC al estado Aprobado, Esta Seguro?" );
    if( x )
    {
    	var s_param_ajax = "IdOrdenCompra=" + i_Id;
    
        unBlockContent( "jqGrid" );
        
        executeAjaxJson
        (
            "<?php echo buildUrl( 'oc/cambiarEstadoOC' ) ?>", 
            s_param_ajax,
            function( o_json )
            {
                console.log( o_json );
                unBlockContent( "jqGrid" );
                
                if( o_json.error )
                {
                    alert( o_json.mensaje_error );
                    return false;
                }
                findControl();
            } 
        );
    }
}

procesoImportacion = function( Id )
{
    location.href="<?=buildUrl('import/addeditImport','TipoProc=upd&Id=')?>" +  Id;
}
</script>

<script>
    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
        	url         : 'nothing',
            mtype       : "POST",
            datatype    : "json",
            editurl     : 'edicionData.php',                        
            colNames    : [ 
                            'IdOrdenCompraConcat', 'Nro Orden', 'Tipo', 'Clasificacion', 'Proveedor', 'Descripcion', 'Moneda', 'Fecha Creación', 'Fecha Entrega', 'Monto Local', 'Estado', '', '', '', '', '', '', ''
                          ],
            colModel    : 
                          [
                            { name: 'IdOrdenCompraConcat', width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'IdOrdenCompra',       width: 60, align: 'center', editable: false, sortable: true },
                            { name: 'TipoOCDesc',          width: 90, align: 'center', editable: false, sortable: false },
                            { name: 'OrdenClasificacionDesc',width: 90, align: 'center', editable: false, sortable: false },
                            { name: 'NombreProveedor',     width: 210,  align: 'left',   editable: false, sortable: true },
                            { name: 'Descripcion',         width: 210,  align: 'left',   editable: false, sortable: true },
                            { name: 'MonedaDesc',          width: 90, align: 'center', editable: false, sortable: false },
                            { name: 'FechaCreacion',       width: 90, align: 'center', editable: false, sortable: true, formatter: function( cellvalue ){ return cellvalue.substring(0, 10); } },
                            { name: 'FechaEntrega',        width: 90, align: 'center', editable: false, sortable: true, formatter: function( cellvalue ){ return cellvalue.substring(0, 10); } },
                            { name: 'FormatMontoBruto',    width: 90, align: 'right', editable: false, sortable: false },
                            { name: 'EstadoDesc',          width: 90, align: 'center', editable: false,  },
                            { 
                                name: 'edit', width: 20, align: 'center', sortable: false, search: false,
                            	cellattr: function () { return ' title="Editar Orden de Compra"'; },
                                formatter:function()
                                {
                                    var o_Data = arguments[2];
                                    if( o_Data.Estado == "PR" || o_Data.Estado == "AP" )
                                    {
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-pencil' title='Editar Orden de Compra' onclick='location.href=\"<?=buildUrl('oc/addedit','block=001&s_TipoProc=upd&i_Id=')?>\" + $(this).parent().parent()[0].id;'></span>";
                                    }
                                    return "";
                                }
                                <?php if( !$edit ){ ?> ,hidden: true <?php } ?>
                            },
                            { 
                                name: 'details', width: 20, align: 'center', sortable: false, search: false, 
                            	cellattr: function () { return ' title="Mostrar"'; },
                                formatter:function()
                                {
                                    return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-document' title='Ver detalles de Orden de Compra' onclick='showView( $(this).parent().parent()[0].id );'></span>";
                                }
                            },
                            { 
                                name: 'del', width: 20, align: 'center', sortable: false, search: false,
                            	cellattr: function () { return ' title="Eliminar"'; },
                                formatter:function()
                                {
                                    var o_Data = arguments[2];
                                    if( o_Data.Estado != "AN" && o_Data.Estado != "CO" )
                                    {
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-trash' title='Eliminar Orden de Compra' onclick='deleteRow( $(this).parent().parent()[0].id );'></span>";
                                    }
                                    return "";
                                }
                            	<?php if( !$delete ){ ?> ,hidden: true <?php } ?>
                            },
                            { 
                                name: 'approv', width: 20, align: 'center', sortable: false, search: false,
                                formatter:function()
                                {
                                	var o_Data = arguments[2];
                                    if( o_Data.Estado == "<?=COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_PREPARACION?>" )
                                    {
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-gear' title='Aprobar Orden de Compra' onclick='showViewAprob( $(this).parent().parent()[0].id );'></span>";
                                    }
                                    return "";
                                }
                                <?php if( !$approv ){ ?> ,hidden: true <?php } ?>
                            },
                            { 
                                name: 'edit', width: 20, align: 'center', sortable: false, search: false,
                                formatter:function()
                                {
                                	var o_Data = arguments[2];
                                    if( o_Data.Estado == "<?=COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_APROBADO?>" )
                                    {
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-pencil' title='Recepcionar Orden de Compra' onclick='location.href=\"<?=buildUrl('recepcion/process','s_TipoProc=upd&i_Id=')?>\" + $(this).parent().parent()[0].id;'></span>";
                                    }
                                    return "";
                                }
                                <?php if( !$receipt ){ ?> ,hidden: true <?php } ?>
                            },
                            {
                                name: 'open', width: 20, align: 'center', sortable: false, search: false,
                                formatter:function()
                                {
                                	var o_Data = arguments[2];
                                    if( o_Data.Estado == "<?=COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_COMPLETADO?>" )
                                    {
                                        return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-arrowreturnthick-1-n' title='Poner en Recepción' onclick='cambiarEstadoAprovado( $(this).parent().parent()[0].id )'></span>";
                                    }
                                    return "";
                                }
                                <?php if( !$roladmin ){ ?> ,hidden: true <?php } ?>
                            },
                            {
                                  name: 'open', width: 20, align: 'center', sortable: false, search: false,
                                  formatter:function()
                                  {
                                      var o_Data = arguments[2];
                                      if( o_Data.Estado == "<?=COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_APROBADO?>" )
                                      {
                                          return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-gear' title='Poner en Recepción' onclick='procesoImportacion( $(this).parent().parent()[0].id )'></span>";
                                      }
                                      if( o_Data.Estado == "<?=COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_COMPLETADO?>" )
                                      {
                                          return "<span style='cursor:pointer; margin: 0 auto;' class='ui-icon ui-icon-check' title='Poner en Recepción' onclick='showViewImport( $(this).parent().parent()[0].id )'></span>";
                                      }
                                      return "";
                                  }
                                  <?php if( !$import ){ ?> ,hidden: true <?php } ?>
                            }
                          ],
			viewrecords : true,
			height      : $( window ).height()-200, 
			width       : $( window ).width() - 20,
            //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()
            {
            },
            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>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"Resumen", "modal"=>true ) )?>