<?php use_helper( 'UrlFunctions' ); ?>
<?php use_helper( 'IncludePartial' ); ?>
<?php use_helper( 'UtilFunctions' ); ?>
<style>
body {font-size:60%} 
.header { background-color: #E1E1E1; color: #444444; font-family: Arial,Helvetica,sans-serif; font-size: 11px; font-weight: bold; }
.pager { background-color: #F4F4F4; background-image: none; border-bottom: thin solid #999999; }
.backgAviso{ background-color: #F4F4BC }
</style>
<style>
.grid
{
    border: 1px solid #77d5f7;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
}
.grid th 
{
    border-color: #77d5f7;
    background-color: #0a7db1;
    font-weight: normal;
    font-size: 11px;
    font-family: Verdana,Arial,sans-serif;
    height: 24px;
    padding: 0 2px;
    border-bottom: 1px solid #77d5f7;
}
.grid td 
{
    border-left: 0 none;
    border-right-color: #77d5f7;
    border-right-style: solid;
    border-right-width: 1px;
    border-bottom-color: #77d5f7;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    padding: 0 2px;
    /*white-space: pre;*/
    height: 22px;
    /*border: 1px solid #77d5f7;*/
}
.buttonOn, .button:hover, .button:focus, .buttonOn:focus
{
    padding: 0;
}
.button
{
    padding: 0;
}
</style>
<script>
mostrarCargaImg = function(){ $('#s_ImagenLoad')[0].style.display = 'inline'; }
ocultarCargaImg = function(){ $('#s_ImagenLoad')[0].style.display = 'none'; }

mostrarMensajeError = function( s_MensajeError )
{
    $( "#resultContent" ).html( '<div id="mensaje_error_content" class="error">' + s_MensajeError + '</div>' );
    setTimeout( function(){ $( "#mensaje_error_content" ).fadeOut( 800 ).fadeIn( 800 ).fadeOut( 500 ).fadeIn( 500 ).fadeOut( 300 ); }, 4000 );
}
</script>
<script>
ejecutarEliminar = function()
{
	mostrarCargaImg();
	var strFormSerialize = $( 'form' ).serialize();
    executeAjaxJson( 
                        "<?php echo buildUrl( 'oc/deleteRow', "i_Id=".$i_Id ) ?>", 
                        strFormSerialize,
                        function( o_json )
                        {
                            if( o_json.error )
                            {
                                ocultarCargaImg();
                                mostrarMensajeError( o_json.mensaje_error );
                                return false;
                            }
                            parent.findControl();
                            parent.$( '#dialog' ).dialog( 'close' );
                            return true;
                        } 
                   );
}
executeProcessAprob = function( status )
{
	x = confirm( "Se procedera en actualizar el estado?" )
    if ( x )
    {
        mostrarCargaImg();
        var strFormSerialize = $( 'form' ).serialize();
        strFormSerialize += "&status=" + status;
        executeAjaxJson( 
                            "<?php echo buildUrl( 'aprobacion/saveProcess', "i_Id=".$i_Id ) ?>", 
                            strFormSerialize,
                            function( o_json )
                            {
                                if( o_json.error )
                                {
                                    ocultarCargaImg();
                                    mostrarMensajeError( o_json.mensaje_error );
                                    return false;
                                }
                                parent.findControl();
                                parent.closeWindow();
                                return true;
                            } 
                       );
    }
}
revertirRegistro = function()
{
	x = confirm( "Esta seguro de Revertir el Proceso?" );
    if( x )
    {
    	mostrarCargaImg();
    	var strFormSerialize = $( 'form' ).serialize();
        executeAjaxJson( 
                            "<?php echo buildUrl( 'oc/revertirOC', "i_Id=".$i_Id ) ?>", 
                            strFormSerialize,
                            function( o_json )
                            {
                                if( o_json.error )
                                {
                                    ocultarCargaImg();
                                    mostrarMensajeError( o_json.mensaje_error );
                                    return false;
                                }
                                parent.findControl();
                                parent.$( '#dialog' ).dialog( 'close' );
                                return true;
                            } 
                       );
    }
}
</script>
<div id="tabs">
    <ul>
        <li><a href="#tabs-1">General</a></li>
        <li><a href="#tabs-2">Detalle</a></li>
        <li><a href="#tabs-3">Recepciones</a></li>
    </ul>
    <div id="tabs-1">
        
            <?php 
            include_partial
            ( 
                "addedit", 
                array
                ( 
                    "o_COM_OrdenCompraCabeceraBN"  => $o_COM_OrdenCompraCabeceraBN, 
                    "a_COM_OrdenItemClasificacion" => $a_COM_OrdenItemClasificacion, 
                    "o_ALM_AlmacenMastBN"          => $o_ALM_AlmacenMastBN, 
                    "a_GSS_MonedaMast"             => $a_GSS_MonedaMast,
                    "a_ALM_AlmacenMast"            => $a_ALM_AlmacenMast,
                    "s_TipoProc"                   => $s_TipoProc
                ),
                $a_ModuloOrden
            ); 
            ?>
    </div>
    <div id="tabs-2">
        <?php 
        include_partial
        ( 
            "detail", 
            array
            ( 
                "o_COM_OrdenCompraCabeceraBN" => $o_COM_OrdenCompraCabeceraBN
            )
        ); 
        ?>
    </div>
    <div id="tabs-3">
        <?php 
        include_partial
        ( 
            "detailtrs", 
            array
            ( 
                "o_DefaultBN"  => $o_COM_OrdenCompraCabeceraBN,
                "b_VerFactura" => true
            )
        ); 
        ?>
    </div>
</div>

<div class="text-center mt-2">
        <button class="btn btn-primary text-center" type="button" onclick="parent.$( '#dialog' ).dialog( 'close' );">Cerrar</button>
        <?php if( $o_COM_OrdenCompraCabeceraBN->getEstado() ==  COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_APROBADO ){ ?>
            <?php if( $o_COM_OrdenCompraCabeceraBN->getIdOrdenClasificacion() == "CA00001" ){ ?>
        <button class="btn btn-primary text-center" type="button" onclick="window.open( '<?=buildUrl( 'oc/reporte', "&i_Id=".$i_Id )?>', '_blank' );">Imprirmir</button>
        <button class="btn btn-primary text-center" type="button" onclick="eviarCorreo();">Enviar Correo</button>
            <?php } ?>
        <?php } ?>

        <?php if( $s_TipoProc ==  "dlt" ){ ?>
            <?php if( $o_COM_OrdenCompraCabeceraBN->getEstado() ==  COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_PREPARACION ){ ?>
        <button class="btn btn-primary text-center" type="button" onclick="eliminarRegistro();">Anular</button>
            <?php } ?>
        <?php } ?>
        <?php if( $s_TipoProc ==  "dlt" ){ ?>
            <?php if( $o_COM_OrdenCompraCabeceraBN->getEstado() ==  COM_OrdenCompraCabeceraBN::ORDEN_COMPRA_APROBADO ){ ?>
        <button class="btn btn-primary text-center" type="button" onclick="revertirRegistro();">Revertir OC</button>
            <?php } ?>
        <?php } ?>
        <?php if( $s_TipoProc ==  "apr" ){ ?>
        <button class="btn btn-primary text-center" type="button" onclick="executeProcessAprob( 'AP' );">Aprobar</button>
        <button class="btn btn-primary text-center" type="button" onclick="executeProcessAprob( 'RE' );">Rechazar</button>
        <?php } ?>
        <?php if( $o_COM_OrdenCompraCabeceraBN->getIdOrdenClasificacion() == "CA00001" ){ ?>
        <button class="btn btn-primary text-center" type="button" onclick="verPDF();">Ver PDF</button>
        <?php } ?>
        <?php if( $o_COM_OrdenCompraCabeceraBN->getIdOrdenClasificacion() == "CA00002" ){ ?>
        <button class="btn btn-primary text-center" type="button" onclick="verServicioPDF();">Ver PDF</button>
        <?php } ?>

        <div id="s_ImagenLoad" style="display: none;"><img src="<?php echo Constantes::ROOT_DIR_APP ?>web/images/progress.gif" border="0"/>&nbsp;<label class="laber req">Procesando...</label></div>
        &nbsp;
</div>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"Enviar Correo", "modal"=>true, "popup_width"=>400, "popup_height"=>210 ) )?>
<?=crearFuentePopupIframe( array( "div_id"=>"dialog2", "iframe_id"=>"myIframe2", "popup_title"=>"", "modal"=>false, "popup_width"=>800, "popup_height"=>400 ) )?>

<script>
eviarCorreo = function()
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'oc/plnEnvCorreo', "Id=$i_Id" )?>' );
    $( '#dialog' ).dialog( 'open' );
}
verPDF = function()
{
    window.open( '<?=buildUrl( 'oc/printPDF', 'Id='.$i_Id.'&ordennum='.str_pad( $o_COM_OrdenCompraCabeceraBN->getIdOrdenCompra(), 10, "0", STR_PAD_LEFT ) )?>', '_BLANK' );
	//$( '#myIframe2' ).attr( 'src', '<?=buildUrl( 'oc/printPDF', "TipoProc=ver_pdf&Id=$i_Id" )?>' );
    //$( '#dialog2' ).dialog( 'open' );
}
verServicioPDF = function()
{
	window.open( '<?=buildUrl( 'os/printPDF', 'Id='.$i_Id.'&ordennum='.str_pad( $o_COM_OrdenCompraCabeceraBN->getIdOrdenCompra(), 10, "0", STR_PAD_LEFT ) )?>', '_BLANK' );
}
closeWindow = function()
{
    $( '#dialog' ).dialog( 'close' );
}
eliminarRegistro = function()
{
    x = confirm( "Esta seguro de eliminar el registro?" )
    if( x )
    {
        ejecutarEliminar();
    }
}
</script>
<script>
$( "#tabs" ).tabs();
</script>
<script type="text/javascript">
convertirTextoTodosLosInputs = function()
{
    pr( "Ingresa" );

    $.each
    (
        $('input, select, textarea, button'), function()
        {
            //pr( this );
            var nodeName = typeof $( this ).attr( 'nodeName' ) === 'undefined' ? $( this ).prop( 'nodeName' ) : $( this ).attr( 'nodeName' );
            var nodeType = $( this ).attr( 'type' );

            //console.log( nodeName + " - " + nodeType );

            if( $( this ).length > 0 )
            {
                switch( nodeName )
                {
                    case "SELECT":
                        $( this ).prop( "disabled", true );
                    break;
                    case "TEXTAREA":
                        pr( this );
                        $( this ).prop( "disabled", true );
                    break;
                    case "INPUT":
                        switch( nodeType )
                        {
                            case "textarea":
                                //pr( this );
                            break;
                            case "text":
                                //pr( this );
                                $( this ).prop( "disabled", true );
                            break;
                            case "button":
                                //$( this ).prop( "disabled", true );
                            break;
                            case "checkbox":
                                $( this ).prop( "disabled", true );
                            break;
                            case "hidden":
                                //$( this ).remove();
                            break;
                        }
                    break;
                    case "BUTTON":
                        pr( $( this ).attr('class') );
                        if( $( this ).attr('id') != "btnAudi" && $( this ).attr('class') == "index.js:128 ui-dialog-titlebar-close" )
                        {
                            $( this ).prop( "disabled", true );
                        }
                    break;
                }
            }
        }
    );
}
convertirTextoTodosLosInputs();
</script>