<script type="text/javascript">
executeAjaxJson = function( s_URL, o_Param, function_pr )
{
    //$.getJSON
    $.post
    (
        s_URL, o_Param, function( data )
        {
            //pr( data );
            try 
            {
                var param = jQuery.parseJSON( data );
                data = param;
            }
            catch ( err ) 
            {
                var param = "";
            }
            
            if( typeof data != 'object' )
            {
                data = data.replace( "<pre>", "" );
                data = data.replace( "</pre>", "");
                data = { "error":true, "mensaje_error":data };
            }
            if( typeof( function_pr ) == "function" )
            {
                function_pr( data );
            }
        }
    );
}

saveForm = function()
{
    blockContent( "content_buttons" );
    var strFormSerialize = $( 'form' ).serialize();
    
    executeAjaxJson
    ( 
        "<?php echo buildUrl( 'caja/guardarAperturaCaja', "" ) ?>", 
        strFormSerialize,
        function( o_json )
        {
            //console.log( o_json );
            //ocultarCargaImg();
            if( o_json.error )
            {
                showAlertBootStrap( 'content_alert', 'alert-danger', o_json.mensaje_error );
                //mostrarMensajeError( o_json.mensaje_error );
                return false;
            }
            parent.recargarPagina();
        } 
    );
    return false;
}
</script>

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

<div class="card">
    <div class="container">
        <!--<form name="form1" id="form1" method="POST" action="" onsubmit="" class="needs-validation">-->
        <form class="needs-validation" name="form1" id="form1" method="POST" action="" onsubmit="return saveForm();">  <!--  -->
        <!--<form id="form1" name="form1" method="POST" onsubmit="return false;"  enctype="multipart/form-data">-->
            <fieldset <?=$s_DisabledFiel?> >
                <input type="hidden" name="Control" id="Control" value="1">
                <input type="hidden" name="id_cierre" id="id_cierre" value="<?=$s_IdCierre?>">

                <!--<div class="form-row mx-0">
                    <div class="col-md-4 mb-1">
                        <label class="col-form-label" for="FechaCierre">Fecha</label>
                        <input type="text" class="form-control form-control-sm text-center" id="FechaCierre" placeholder="" name="FechaCierre" value="<?=date("Y-m-d")?>" disabled >
                    </div>
                </div>-->

                <div class="form-group">
                    <label for="IdCaja" class="col-form-label">Caja:</label>
                    <select class="form-control form-control-sm custom-select my-0 mr-sm-2" id="IdCaja" name="IdCaja" <?=$s_DisabledInp?> required>
                        <?php foreach ( $a_FCO_Caja as $o_FCO_CajaBN ){ ?>
                        <option value="<?=$o_FCO_CajaBN->getIdCaja()?>" > <?=$o_FCO_CajaBN->getDescripcion()?> </option>
                        <?php } ?>
                    </select>
                </div>

                <div class="form-group">
                    <label for="Nombre" class="col-form-label">Turno:</label>
                    <select class="form-control form-control-sm custom-select my-0 mr-sm-2" id="IdTurno" name="IdTurno" required>
                        <!--<option value=""> - Seleccione - </option>-->
                        <option value="<?=$o_MST_TurnoBN->getIdTurno()?>" > <?=$o_MST_TurnoBN->getDescripcion()?> </option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="Nombre" class="col-form-label">Cajero:</label>
                    <select class="orm-control form-control-sm custom-select my-0 mr-sm-2 required" id="IdVendedor" name="IdVendedor" <?=$s_DisabledInp?>>
                        <option value=""> - Seleccione - </option>
                        <?php foreach ( VTA_VendedorFN::select_all() as $a_Vendedor ) { ?>
                            <?php if( MST_PersonaMastFN::buscarPersonaPorIdUsuario( myUser::getUser()->getUserId() )->getIdPersona() != $a_Vendedor['IdPersona'] ){ continue; } ?> <!-- ACTIVAR EN PRODUCCION -->
                        <!--<option value="<?=$a_Vendedor['IdUsuario']?>" <?php //if( MST_PersonaMastFN::buscarPersonaPorIdUsuario( myUser::getUser()->getUserId() )->getIdPersona() == $a_Vendedor['IdPersona'] ){ echo "SELECTED"; } ?> > <?=$a_Vendedor['NombreCompleto']?> </option>-->
                        <option value="<?=$a_Vendedor['IdUsuario']?>" <?php if( myUser::getUser()->getUserId() == $a_Vendedor['IdUsuario'] ){ echo "SELECTED"; } ?> > <?=$a_Vendedor['NombreCompleto']?> </option>
                        <?php } ?>
                    </select>
                </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" <?=$s_DisabledInp?> required>
                            <option value="A" > ABIERTO </option>
                        </select>
                    </div>
                </div>

                <div class="form-group p-1">
                    <label for="Observaciones" class="col-form-label">Observaciones:</label>
                    <textarea class="form-control" name="Observaciones" id="Observaciones" rows="2"><?=$o_FCO_CierreBN->getObservaciones()?></textarea>
                </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="parent.cerrarPopupBootstrap();">CANCELAR</button>
                    </div>
                    <div class="col-md-6 mb-1">
                        <button type="submit" id="myButton" class="btn btn-primary btn-lg btn-block" onclick="">GRABAR Y SALIR</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>
</div>