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

<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 );
}

saveForm = function()
{
    x = confirm( "Esta seguro de guardar el formulario?" );
    if( x )
    {
        blockContent( 'content_buttons' );
        var strFormSerialize = $( 'form' ).serialize();
        
        executeAjaxJson
        ( 
            "<?php echo buildUrl( 'cajachica/saveAddEdit', "" ) ?>", 
            strFormSerialize,
            function( o_json )
            {
                unBlockContent( 'content_buttons' );
                if( o_json.error )
                {
                    showAlertBootStrap( 'content_alert', 'alert-danger', o_json.mensaje_error );
                    //mostrarMensajeError( o_json.mensaje_error );
                    return false;
                }

                location.href = "<?=buildUrl( 'cajachica/list', "" )?>";
            } 
        );
    }
    return false;
}
</script>

<div class="shadow-sm p-3 mb-2 bg-white rounded card card-body">
    <div class="row">
        <div class="col mb-1">
            <h4 class="text-info text-nowrap mb-0"><?=$s_Titulo?></h4>
        </div>
    </div>
</div>

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

<div class="card">
    <div class="container">
        <form class="was-validated" name="form1" id="form1" method="POST" action="" onsubmit="return saveForm();">
            <input type="hidden" name="i_Id" id="i_Id" value="<?=$o_ALM_CajaChicaCabBN->getIdCaja()?>">
            <input type="hidden" name="s_TipoProc" id="s_TipoProc" value="<?=$s_TipoProc?>">
            <input type="hidden" name="TypeSub" id="TypeSub" value="0">

            <div class="form-group">
                <label class="col-form-label" for="FechaCaja">Fecha</label>
                <input type="text" class="form-control form-control-sm text-center" id="FechaCaja" name="FechaCaja" value="<?=substr( $o_ALM_CajaChicaCabBN->getFechaCaja(), 0, 10 )?>" readonly="" >
            </div>
            <div class="form-group">
                <label class="col-form-label" for="MontoFondo">Fondo</label>
                <input type="text" class="form-control form-control-sm text-center" id="MontoFondo" name="MontoFondo" value="<?=$o_ALM_CajaChicaCabBN->getMontoFondo()?>" readonly="">
            </div>
            <div class="form-group">
                <label for="MontoSaldo" class="col-form-label">Saldo:</label>
                <input type="text" class="form-control form-control-sm text-center" id="MontoSaldo" name="MontoSaldo" value="<?=$o_ALM_CajaChicaCabBN->getMontoSaldoAnt()?>" readonly="">
            </div>
            <div class="form-group">
                <label for="MontoIngreso" class="col-form-label">Monto a Ingresar:</label>
                <input type="text" class="form-control form-control-sm text-center" id="MontoIngreso" name="MontoIngreso" value="<?=$o_ALM_CajaChicaCabBN->getMontoIngreso()?>"
                         onfocus="this.select()" onblur="calcularTotal( this );" onkeypress="return NumCheckDecimal( event, this );" required
                         >
            </div>

            <div class="form-row mx-0">
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="Estado">Estado</label>
                    <select class="form-control form-control-sm custom-select my-0 mr-sm-2" id="Estado" name="Estado" required>
                        <option value="A"> ABIERTO </option>
                    </select>
                </div>
            </div>

            <div class="dropdown-divider" style="border-top: 1px solid #8c8b8b;"></div>

            <?php if( $o_ALM_CajaChicaCabBN->getIdCaja() ){ ?>
            <button type="button" class="btn btn-secondary btn-lg mb-2 mt-2" id="btnAudi">Auditoria</button>
            <?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"AUDITORIA", "modal"=>true ) )?>
            <script>
            $( "#btnAudi" )
            .click
            (
                function()
                {
                    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'cajachica/audi', 'i_Id='.$o_ALM_CajaChicaCabBN->getIdCaja() )?>' );
                    $( '#dialog' ).dialog( 'open' );
                }
            )
            </script>            
            <?php } ?>

            <div class="form-group col-14"><hr style="border-top: 1px solid #8c8b8b;"></div>

            <div class="form-row mx-0" id="content_buttons">
                <div class="col-md-6 mb-1">
                    <button type="button" class="btn btn-primary btn-lg btn-block" onclick="location.href='<?php echo buildUrl( 'cajachica/list', '' ) ?>'">CANCELAR</button>
                </div>
                <?php if( !$Error ){ ?>
                <div class="col-md-6 mb-1">
                    <button type="submit" class="btn btn-primary btn-lg btn-block" onclick="$( '#TypeSub' ).val( '1' );">GRABAR Y SALIR</button>
                </div>
                <?php } ?>
            </div>

            <?php if( $s_TipoProc != "nvo" ){ ?>
            <div class="form-group col-14"><hr style="border-top: 1px solid #8c8b8b;"></div>
    
            <br>
            <h2 style="background-color: #C9CACC; padding: 5px;">Log de cambios</h2>
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th scope="col">Tipo</th>
                            <th scope="col">Valor Ant</th>
                            <th scope="col">Valor Nuevo</th>
                            <th scope="col">Fecha</th>
                            <th scope="col">Usuario</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $o_ALM_CajaChicaCabBN->getArrayCajaChicaLog() as $o_ALM_CajaChicaLogBN ) { ?>
                        <tr>
                            <td>Fondo</td>
                            <td><?=$o_ALM_CajaChicaLogBN->getFechaCreacion()?></td>
                            <td><?=$o_ALM_CajaChicaLogBN->getValorAnterior()?></td>
                            <td><?=$o_ALM_CajaChicaLogBN->getValorNuevo()?></td>
                            <td><?=$o_ALM_CajaChicaLogBN->getUsuarioCreacion()?></td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
            <?php } ?>

        </form>
    </div>
</div>

<script type="text/javascript">
calcularTotal = function()
{
    var MontoIngreso = parseFloat( $( "#MontoIngreso" ).val() );
    var MontoSaldo   = trim( $( "#MontoSaldo" ).val() ) == "" ? 0 :  parseFloat( $( "#MontoSaldo" ).val() );
    var SumaTotal    = MontoIngreso + MontoSaldo;
    pr( SumaTotal );
    $( "#MontoFondo" ).val( SumaTotal );
}
$( "#MontoIngreso" )
.keypress
(
    function( e )
    {
        key = e.keyCode ? e.keyCode : e.which;
        if( key == 13 )
        {
            calcularTotal();
        }
    }
);
</script>

<script type="text/javascript">
<?php if( $Error ){ ?>
showAlertBootStrap( 'content_alert', 'alert-danger', '<?=$MensajeError?>' );
<?php } ?>
</script>