<?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()
{
    //mostrarCargaImg();
    var strFormSerialize = $( 'form' ).serialize();
    
    executeAjaxJson
    ( 
        "<?php echo buildUrl( 'unidad/saveAddEdit', "" ) ?>", 
        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;
            }
            //return false;
            switch ( o_json.type_sub )
            {
                case "0":
                    location.href = "<?=buildUrl( 'unidad/addedit', "s_TipoProc=upd&i_Id=")?>" + o_json.id;
                break;
                case "1":
                    location.href = "<?=buildUrl( 'unidad/list', "")?>";
                break;
            }
        } 
    );
    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_MST_UnidadMastBN->getIdUnidad()?>">
            <input type="hidden" name="s_TipoProc" id="s_TipoProc" value="<?=$s_TipoProc?>">
            <input type="hidden" name="TypeSub" id="TypeSub" value="0">

            <div class="form-row mx-0">
                <div class="col-md-6 mb-1">
                    <label class="col-form-label" for="CodigoSunat">C&oacute;digo Sunat</label>
                    <input type="text" class="form-control" name="CodigoSunat" id="CodigoSunat" placeholder="" value="<?=$o_MST_UnidadMastBN->getCodigoSunat();?>" onblur="this.value=this.value.toUpperCase();" required>
                </div>
                <div class="col-md-6 mb-1">
                    <label class="col-form-label" for="DescripcionExtranjera">C&oacute;digo Web</label>
                    <input type="text" class="form-control" name="DescripcionExtranjera" id="DescripcionExtranjera" placeholder="" value="<?=$o_MST_UnidadMastBN->getDescripcionExtranjera();?>" onblur="this.value=this.value.toUpperCase();" required>
                </div>
            </div>
            <div class="form-group">
                <label for="DescripcionCorta" class="col-form-label">Descripci&oacute;n:</label>
                <input type="text" class="form-control form-control-sm" id="DescripcionCorta" placeholder="" name="DescripcionCorta" value="<?=$o_MST_UnidadMastBN->getDescripcionCorta();?>" onblur="this.value=this.value.toUpperCase();" required>
                <!--<div class="valid-feedback">Valido.</div>
                <div class="invalid-feedback">Por favor ingrese valor.</div>-->
            </div>
            <div class="form-row mx-0">
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="UnidadTipo">Tipo de Unidad</label>
                    <select class="form-control form-control-sm custom-select my-0 mr-sm-2" name="UnidadTipo" id="UnidadTipo" required>
                        <option value=""> - Seleccione - </option>
                        <option value="L" <?php if( $o_MST_UnidadMastBN->getUnidadTipo()=="L" ){ echo "SELECTED"; } ?>> LITROS </option>
                        <option value="M" <?php if( $o_MST_UnidadMastBN->getUnidadTipo()=="M" ){ echo "SELECTED"; } ?>> MASA </option>
                        <option value="U" <?php if( $o_MST_UnidadMastBN->getUnidadTipo()=="U" ){ echo "SELECTED"; } ?>> UNIDAD </option>
                    </select>
                </div>
                <div class="col-md-4 mb-1">
                    <label class="col-form-label" for="NumeroDecimales">N&uacute;mero de decimales</label>
                    <input type="text" class="form-control" name="NumeroDecimales" id="NumeroDecimales" placeholder="" value="<?=$o_MST_UnidadMastBN->getNumeroDecimales();?>" onblur="this.value=this.value.toUpperCase();" required>
                </div>
                <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=""> - Seleccione - </option>
                        <option value="A" <?php if( $o_MST_UnidadMastBN->getEstado()=="A" ){ echo "SELECTED"; } ?>> ACTIVO </option>
                        <option value="I" <?php if( $o_MST_UnidadMastBN->getEstado()=="I" ){ echo "SELECTED"; } ?>> INACTIVO </option>
                    </select>
                </div>
            </div>

            <div class="dropdown-divider" style="border-top: 1px solid #8c8b8b;"></div>
            
            <script type="text/javascript">
            animation_show = function( elm )
            {
                if( $( "#UnidadEquivalenteFlag" ).is( ':checked' ) || $( "#UsoFraccionFlag" ).is( ':checked' ) )
                {
                    if( !$( "#content_inputs" ).is( ":visible" ) )
                    {
                        $( "#content_inputs" ).toggle( 'blind', { to: { width: 200, height: 60 } }, 500 );
                    }
                }
                if( !$( "#UnidadEquivalenteFlag" ).is( ':checked' ) && !$( "#UsoFraccionFlag" ).is( ':checked' ) )
                {
                    $( "#content_inputs" ).toggle( 'blind', { to: { width: 200, height: 60 } }, 500 );
                }
            }
            </script>
            <div class="container">
                <div class="form-row mx-0">
                    <div class="col-md-4 mb-1">
                        <div class="custom-control custom-checkbox mr-sm-2">
                            <input type="checkbox" class="custom-control-input" id="UnidadEquivalenteFlag" name="UnidadEquivalenteFlag" onclick="animation_show();" 
                                    <?php if( $o_MST_UnidadMastBN->getUnidadEquivalenteFlag() ){ echo "checked"; } ?> >
                            <label class="custom-control-label text-body" for="UnidadEquivalenteFlag">Unidad Equivelente</label>
                        </div>
                    </div>
                    <div class="col-md-4 mb-1">
                        <div class="custom-control custom-checkbox mr-sm-2">
                            <input type="checkbox" class="custom-control-input" id="UsoFraccionFlag" name="UsoFraccionFlag" onclick="animation_show();"
                                    <?php if( $o_MST_UnidadMastBN->getUsoFraccionFlag() ){ echo "checked"; } ?> >
                            <label class="custom-control-label text-body" for="UsoFraccionFlag">Uso con Fracci&oacuten</label>
                        </div>
                    </div>
                    <div class="col-md-4 mb-1">
                        <div class="custom-control custom-checkbox mr-sm-2">
                            <input type="checkbox" class="custom-control-input" id="EsUnidadMinima" name="EsUnidadMinima" onclick=""
                                <?php if( $o_MST_UnidadMastBN->getEsUnidadMinimaFlag() ){ echo "checked"; } ?> >
                            <label class="custom-control-label text-body" for="EsUnidadMinima">Unidad Medida - Minima</label>
                        </div>
                    </div>
                </div>
            </div>

<script type="text/javascript">
addUnitMeasure = function()
{
    var a_content_class = $( "#content_inputs" ).find( ".form-row" );

    var id_concat = "idx_" + a_content_class.length;

    var text_html = `
        <div class="form-row mx-0" style="background-color: gainsboro; margin-top: 10px;" >
            <div class="col-md-6 mb-1">
                <label class="col-form-label" for="chk_act_`+id_concat+`">Unidad Medida <input type="checkbox" id="chk_act_`+id_concat+`"></label>
                <select class="form-control form-control-sm custom-select my-0 mr-sm-2" id="IdUnidadEquivalente_`+id_concat+`" name="IdUnidadEquivalente[]" style="background-color: #fff;">
                    <option value=""> - Seleccione - </option>
                    <?php foreach ( MST_UnidadMastFN::selectActivos() as $i_Key => $o_UnidadVentaBN ){ ?>
                    <option value="<?=$o_UnidadVentaBN->getIdUnidad()?>" > <?=$o_UnidadVentaBN->getDescripcionCorta()?> </option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-md-6 mb-1">
                <label class="col-form-label" for="CantidadEquivalente_`+id_concat+`">Cantidad Equivelente</label>
                <input type="number" class="form-control" name="CantidadEquivalente[]" id="CantidadEquivalente_`+id_concat+`" value="" onkeypress="return NumCheckEntero( event, this );" placeholder="">
            </div>
        </div>
        `;

    $( "#content_inputs" ).append( text_html );
    //$( "#CantidadEquivalente_" + id_concat ).inputSpinner();
    //$( "#CantidadEquivalente_" + id_concat ).spinner();
}
deleteUnitMeasure = function()
{
    var a_LstChecked = $( "#content_inputs input:checked" );

    if( a_LstChecked )
    {
        a_LstChecked.each
        (
            function( key, elm )
            {
                var parent = $( elm ).closest( ".form-row" );
                parent.remove();
            }
        );
    }
}
</script>
            <?php
            $s_StyleVisible = "display: none;";
            if( $o_MST_UnidadMastBN->getUnidadEquivalenteFlag() || $o_MST_UnidadMastBN->getUsoFraccionFlag() )
            {
                $s_StyleVisible = "";
            }
            ?>
            <div class="container" style="<?=$s_StyleVisible?>" id="content_inputs">
                <button type="button" class="btn btn-outline-success" onclick="addUnitMeasure();"> + Agregar </button>
                <button type="button" class="btn btn-outline-danger" onclick="deleteUnitMeasure();"> - Eliminar </button>

                <?php if( $a_MST_UnidadConversionMast ){ ?>
                    <?php foreach ( $a_MST_UnidadConversionMast as $key => $o_MST_UnidadConversionMastBN ) { ?>
                        <?php
                        $s_Id = "idx_".$key;
                        ?>
                <div class="form-row mx-0" style="background-color: gainsboro; margin-top: 10px;" >
                    <div class="col-md-6 mb-1">
                        <label class="col-form-label" for="chk_act_<?=$s_Id?>">Unidad Medida <input type="checkbox" id="chk_act_<?=$s_Id?>"></label>
                        <select class="form-control form-control-sm custom-select my-0 mr-sm-2" id="IdUnidadEquivalente_<?=$s_Id?>" name="IdUnidadEquivalente[]" style="background-color: #fff;">
                            <option value=""> - Seleccione - </option>
                            <?php foreach ( MST_UnidadMastFN::selectActivos() as $i_Key => $o_UnidadVentaBN ){ ?>
                            <option value="<?=$o_UnidadVentaBN->getIdUnidad()?>" <?php if( $o_MST_UnidadConversionMastBN->getIdUnidadEquivalente()==$o_UnidadVentaBN->getIdUnidad() ){ echo "SELECTED"; } ?> > 
                                <?=$o_UnidadVentaBN->getDescripcionCorta()?> 
                            </option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="col-md-6 mb-1">
                        <label class="col-form-label" for="CantidadEquivalente_<?=$s_Id?>">Cantidad Equivelente</label>
                        <input type="number" class="form-control" name="CantidadEquivalente[]" id="CantidadEquivalente_<?=$s_Id?>" value="<?=$o_MST_UnidadConversionMastBN->getCantidadEquivalente();?>" 
                                onkeypress="return NumCheckEntero( event, this );" placeholder="">
                        <script>
                        //$( "#CantidadEquivalente_<?=$s_Id?>" ).inputSpinner();
                        </script>
                    </div>
                </div>
                    <?php } ?>
                <?php } ?>

            </div>

            <div class="form-group col-14"><hr style="border-top: 1px solid #8c8b8b;"></div>
            
            <?php if( $o_MST_UnidadMastBN->getIdUnidad() ){ ?>
            <button type="button" class="btn btn-secondary btn-lg mb-2 mt-2" id="btnAudi">Auditoria</button>
            <?php } ?>

            <div class="form-row mx-0">
                <div class="col-md-6 mb-1">
                    <button type="button" class="btn btn-primary btn-lg btn-block" onclick="location.href='<?php echo buildUrl( 'unidad/list' ) ?>'">CANCELAR</button>
                </div>
                <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>
            </div>
        </form>
    </div>
</div>

<script>
$( "#btnAudi" )
.click
(
    function()
    {
        $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'unidad/audi', 'i_Id='.$o_MST_UnidadMastBN->getIdUnidad() )?>' );
        $( '#dialog' ).dialog( 'open' );
    }
)
</script>
<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"AUDITORIA", "modal"=>true ) )?>