<?php use_helper('UrlFunctions'); ?>
<?php use_helper( 'IncludePartial' ); ?>
<?php use_helper( 'UtilFunctions' ); ?>

<script type="text/javascript">
showAlertBootStrap = function( elm_id_content, class_default, message_alert )
{
    var html_alert = '<div class="alert ' + class_default + ' alert-dismissible fade show" role="alert"> ' +
            '' + message_alert +
            '<button type="button" class="close" data-dismiss="alert" aria-label="Close">' +
                '<span aria-hidden="true">&times;</span>' +
            '</button>' +
        '</div>';
    $( "#" + elm_id_content ).append( html_alert );
}
</script>

<script type="text/javascript">
activarInput = function( value )
{
    //una validacion solo para el tipo efecivo - el input esta en read-only
    if( value == 1 )
    {
        $( "#ReciboNumero" ).prop( 'readonly', true );
        $( "#ReciboNumero" ).prop( 'required', true );
    }
    else
    {
        $( "#ReciboNumero" ).prop( 'readonly', false );
        $( "#ReciboNumero" ).prop( 'required', false );
    }
}
</script>

<div id="content_alert" style=""></div>

<div class="card-body" style="padding-top: 0px; padding-bottom: 0px; padding-left: 1.25rem; padding-right: 1.25rem;">
    <form name="form1" id="form1" method="POST" action="" onsubmit="">
        <input type="hidden" name="tipoproc" value="<?=$s_TipoProc?>" >
        <input type="hidden" name="Control" value="1" >
        <input type="hidden" name="idnumdoc" value="<?=$s_NumeroDocumento?>" >

		<?php if( GSS_ParametroMastFN::getValorParam( "MONEDA", "MULTIMONEDA" ) ){ ?>
        <div class="form-row mx-0">
            <div class="col-md-6 mb-1">
                &nbsp;
            </div>
            <div class="col-md-6 mb-1">
                <label class="col-form-label" for="IdMoneda">Moneda</label>
                <select class="form-control form-control-sm custom-select my-0 mr-sm-2" name="IdMoneda" id="IdMoneda" onchange="calcularMontoCambio( this.value );" required>
                    <?php foreach ( $a_GSS_MonedaMast as $i_Key => $o_GSS_MonedaMastBN ){ ?>
                    <option value="<?=$o_GSS_MonedaMastBN->getIdMoneda()?>" <?php if( $s_DocMonedaCod==$o_GSS_MonedaMastBN->getIdMoneda() ){ echo "selected"; } ?>> <?=$o_GSS_MonedaMastBN->getDescripcion()?> </option>
                    <?php } ?>
                </select>
            </div>
        </div>
        <?php }else{ ?>
        <input type="hidden" name="IdMoneda" value="LO">
        <?php } ?>

        <div class="form-row mx-0">
            <div class="col-md-6 mb-1">
                <label class="col-form-label" for="IdTipoPago">Forma de Pago</label>
                <select class="form-control form-control-sm custom-select my-0 mr-sm-2" name="IdTipoPago" id="IdTipoPago" onchange="activarInput( this.value );" required>
                    <?php foreach ( FCO_TipoPagoFN::selectActivos() as $o_FCO_TipoPagoBN) { ?>
                    <option value="<?=$o_FCO_TipoPagoBN->getIdTipoPago()?>" <?php if( $IdTipoPago==$o_FCO_TipoPagoBN->getIdTipoPago() ){ echo "SELECTED"; } ?> > <?=$o_FCO_TipoPagoBN->getNombre()?> </option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-md-6 mb-1">
                <label class="col-form-label" for="Monto">Monto</label>
                <input type="text" autocomplete="off" class="form-control form-control-sm" onfocus="this.select();" name="Monto" id="Monto" placeholder="" value="<?=$f_MontoResta?>" aria-describedby="basic-addon2" required>
            </div>
        </div>

        <div class="form-row mx-0">
            <div class="col-md-6 mb-1">
                <label class="col-form-label" for="ReciboNumero">Nro de Operaci&oacute;n</label>
                <input type="text" class="form-control form-control-sm" name="ReciboNumero" id="ReciboNumero" placeholder="" value="<?=$ReciboNumero?>" aria-describedby="basic-addon2" required>
            </div>
            <div class="col-md-6 mb-1">
                <label class="col-form-label" for="ReciboFecha">Fecha de Pago</label>
                <div class="">
                    <label class="sr-only" for="FechaDocumento1">Fecha Desde</label>
                    <div class="input-group mb-2">
                        <div class="input-group-prepend">
                            <div class="input-group-text" style="padding: .1rem .75rem;"><i class="far fa-calendar-alt"></i></div>
                        </div>
                        <input type="text" class="form-control form-control-sm text-center" id="ReciboFecha" name="ReciboFecha" style="border-radius: .2rem;" value="<?=date( "Y-m-d" )?>" readonly>
                        <script type="text/javascript">
                        //$( "#FechaDocumento1" ).datepicker( { dateFormat: "yy-mm-dd", changeYear:true, yearRange: "-5:+0", changeMonth: true, onClose: function (){ this.focus(); } } );
                        </script>
                    </div>
                </div>
            </div>
        </div>

        <div class="form-row mx-0" style="padding: 10px;" id="content_buttons">
            <div class="col-md-6 mb-1">
                <button type="button" class="btn btn-danger btn-sm btn-block" onclick="parent.ocultarModal()">CANCELAR</button>
            </div>
            <div class="col-md-6 mb-1">
                <button type="submit" class="btn btn-primary btn-sm btn-block" onclick="">GRABAR Y SALIR</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
$( '#IdTipoPago' ).trigger( 'change' );
<?php if( $s_MensajeError ){ ?>
showAlertBootStrap( 'content_alert', 'alert-danger', '<?=str_replace( "'", "", $s_MensajeError )?>' );
<?php } ?>

<?php if( $b_Control ){ ?>
parent.ocultarModal( 1 );
<?php } ?>
</script>
<!--<div class="dropdown-divider"></div>-->

<script type="text/javascript">
calcularMontoCambio = function( moneda_select )
{
    
    var monto_pagar   = parseFloat( $( "#Monto" ).val() );
    //var moneda_select = $( "#IdMoneda" ).val();
    var moneda_cab    = "<?=$s_MonedaSistema?>";
    var moneda_ord    = "<?=$s_DocMonedaCod?>";
    var tipo_cambio_c = <?=$f_TipoCob?>;

    var monto_cambio  = 0;

    if( moneda_cab == "EX" )
    {
        if( moneda_select == "LO" )
        {
            monto_cambio = monto_pagar * tipo_cambio_c;
        }
        if( moneda_select == "EX" )
        {
            monto_cambio = redondear( monto_pagar / tipo_cambio_c, 10 );
        }
    }
    if( moneda_cab == "LO" )
    {
        if( moneda_select == "LO" )
        {
            monto_cambio = monto_pagar * tipo_cambio_c;
        }
        if( moneda_select == "EX" )
        {
            monto_cambio = redondear( monto_pagar / tipo_cambio_c, 10 );
        }
    }

    /*var a_data = explode( ".", monto_cambio );
    if( a_data[1].length > 6 )
    {
        pr( monto_cambio );
        pr( Math.ceil( monto_cambio ) );
        var redondeo = Math.ceil( monto_cambio ) - monto_cambio;
        redondeo = redondear( redondeo, 2 );
        if( redondeo <= 0.1 )
        {
            monto_cambio = Math.ceil( monto_cambio );
        }
    }*/

    $( "#Monto" ).val( monto_cambio );
    
}
</script>