<div class="modal" tabindex="-1">
    <div class="modal-dialog" style='width: 50%; max-width: 600px !important; min-width: 400px !important;'>
        <div class="modal-content overflow-auto">
            <div class="modal-header">
                <h5 class="modal-title text-primary">Configuración adicional para
                    <strong>{{ strtolower($analitoEP15->nom_analito) }}</strong></h5>
                <button class="btnEvent close" data-event='click' data-route='CancelModal' data-preguntaantes="true">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <form class="form formEvent formCreate" data-route="StoreNivelEP15" data-event="submit"
                  data-close_modal='true'>
                <div class="modal-body overflow-auto">
                    <input type="hidden" class='reference' value='{{ $analitoEP15->id_analito_ep15 }}'>

                    <ul class="nav nav-tabs" id="myTab" role="tablist">
                        @for($nivel=1; $nivel<=$analitoEP15->num_niveles; $nivel++)
                            <li class="nav-item">
                                <a class="nav-link
                            @if($nivel == 1)
                                active
                            @endif
                            "
                                   id="nivel-{{ $nivel }}-tab" data-toggle="tab" href="#modulo_nivel_{{ $nivel }}"
                                   role="tab" aria-selected="true"><h3 class='h5'>Nivel {{ $nivel }}</h3></a>
                            </li>
                        @endfor
                    </ul>

                    <div class="tab-content p-3 border bg-light" id="myTabContent">
                        @for($nivel=1; $nivel<=$analitoEP15->num_niveles; $nivel++)
                            @php
                                $nivel_bd_gl;
                                foreach($niveles as $nivel_bd){
                                    if($nivel_bd->nivel == $nivel){
                                        $nivel_bd_gl = $nivel_bd;
                                    }
                                }
                            @endphp

                            <div class="tab-pane consolidado_nivel fade
                            @if($nivel == 1)
                                show active
                            @endif
                        " id="modulo_nivel_{{ $nivel }}" role="tabpanel" aria-labelledby="nivel-{{ $nivel }}-tab">

                                <div class="row">
                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>Sigma<sub>R</sub></label>
                                        <input type="text" class='form-control sigma_r' id='sigma_r_{{ $nivel }}'
                                               value='{{ (isset($nivel_bd_gl->sigma_r) ? round($nivel_bd_gl->sigma_r,5) : "") }}'>
                                    </div>

                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>Sigma<sub>WL</sub></label>
                                        <input type="text" class='form-control sigma_wl' id='sigma_wl_{{ $nivel }}'
                                               value='{{ (isset($nivel_bd_gl->sigma_wl) ? round($nivel_bd_gl->sigma_wl,5) : "") }}'>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>S<sub>mp</sub>%</label>
                                        <input type="text" class='form-control smp_porcentaje' id='smp_porcentaje_{{ $nivel }}'
                                               value='{{ isset($nivel_bd_gl->smp_porcentaje)? round($nivel_bd_gl->smp_porcentaje,5) : (isset($analitoEP15->etmp) ? round(($analitoEP15->etmp/2),5) : "") }}'>
                                    </div>

                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>S<sub>mp</sub> Unidades</label>
                                        <input type="text" class='form-control smp_unidad' id='smp_unidad_{{ $nivel }}' disabled
                                               value='{{ (isset($nivel_bd_gl->smp_unidad) ? round($nivel_bd_gl->smp_unidad,5) : "") }}'>
                                    </div>
                                </div>
                                 <div class="row">
                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>Valor DIANA</label>
                                        <input type="text" class='form-control valor_diana' id='valor_diana_{{ $nivel }}'
                                               value='{{ (isset($nivel_bd_gl->valor_diana) ? round($nivel_bd_gl->valor_diana,5) : "") }}'>
                                    </div>
                                </div>

                                <div class="mt-3">

                                    <h3 class='h5 text-primary'>Inserto</h3>

                                    <div class="row">
                                        <div class="form-group col-6">
                                            <label class='font-weight-bold'>Media</label>
                                            <input type="text" class='form-control media_inserto'
                                                   placeholder='Media de referencia' id='media_inserto_{{ $nivel }}'
                                                   value='{{ (isset($nivel_bd_gl->media_inserto) ? round($nivel_bd_gl->media_inserto,5) : "") }}'>
                                        </div>

                                        <div class="form-group col-6">
                                            <label class='font-weight-bold'>D.E.</label>
                                            <input type="text" class='form-control de_inserto'
                                                   placeholder='Desviación estándar' id='de_inserto_{{ $nivel }}'
                                                   value='{{ (isset($nivel_bd_gl->de_inserto) ? round($nivel_bd_gl->de_inserto,5) : "") }}'>
                                        </div>
                                    </div>
                                    <!-- <div class="row">
                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>CV% Rep δ<sub>R</sub></label>
                                        <input type="text" class='form-control cvr' id='cvr_{{ $nivel }}'
                                               value='{{ (isset($nivel_bd_gl->cvr) ? round($nivel_bd_gl->cvr,5) : "") }}'>
                                    </div>

                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>CV% total δ<sub>WL</sub></label>
                                        <input type="text" class='form-control cvi' id='cvi_{{ $nivel }}'
                                               value='{{ (isset($nivel_bd_gl->cvi) ? round($nivel_bd_gl->cvi,5) : "") }}'>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>Límite superior</label>
                                        <input type="text" class='form-control limite_s' id='limite_s_{{ $nivel }}'
                                               value='{{ (isset($nivel_bd_gl->limite_s) ? round($nivel_bd_gl->limite_s,5) : "") }}'>
                                    </div>

                                    <div class="form-group col-6">
                                        <label class='font-weight-bold'>Límite inferior</label>
                                        <input type="text" class='form-control limite_i' id='limite_i_{{ $nivel }}'
                                               value='{{ (isset($nivel_bd_gl->limite_i) ? round($nivel_bd_gl->limite_i,5) : "") }}'>
                                    </div>
                                </div> -->
                                    <div class="row">
                                        <div class="form-group col-6">
                                            <label class='font-weight-bold'>U</label>
                                            <input type="text" class='form-control u_incertidumbre'
                                                   placeholder='Incertidumbre expandida' disabled
                                                   value='{{ (isset($nivel_bd_gl->u_incertidumbre) ? round($nivel_bd_gl->u_incertidumbre,5) : "") }}'>
                                            <small class='d-block'>La incertidumbre expandida en unidades (error total
                                                en unidades)</small>
                                        </div>

                                        <div class="form-group col-6">
                                            <label class='font-weight-bold'>k</label>
                                            <input type="text" class='form-control k' placeholder='Factor de cobertura'
                                                   value='{{ (isset($nivel_bd_gl->k) ? round($nivel_bd_gl->k,5) : "") }}'>
                                            <small class='d-block'>El factor de cobertura es una constante de
                                                distribución gaussiana (ej: 1.96 - constante Z)</small>
                                        </div>
                                    </div>

                                </div>


                                <div class="mt-3">

                                    <h3 class='h5 text-primary'>Grupo PAR</h3>

                                    <div class="row">
                                        <div class="form-group col-6">
                                            <label class='font-weight-bold'>Media</label>
                                            <input type="text" class='form-control media_gpar'
                                                   placeholder='Media de referencia'
                                                   value='{{ (isset($nivel_bd_gl->media_gpar) ? round($nivel_bd_gl->media_gpar,5) : "") }}'>
                                        </div>

                                        <div class="form-group col-6">
                                            <label class='font-weight-bold'>D.E.</label>
                                            <input type="text" class='form-control de_gpar'
                                                   placeholder='Desviación estándar'
                                                   value='{{ (isset($nivel_bd_gl->de_gpar) ? round($nivel_bd_gl->de_gpar,5) : "") }}'>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <div class="form-group col-6">
                                            <label class='font-weight-bold'>NLab</label>
                                            <input type="text" class='form-control n_lab_gpar'
                                                   placeholder='Número de laboratorios'
                                                   value='{{ (isset($nivel_bd_gl->n_lab_gpar) ? $nivel_bd_gl->n_lab_gpar : "") }}'>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @endfor
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary btnEvent" data-event='click' data-route='CancelModal'
                            data-preguntaantes="true">Cancelar
                    </button>
                    <button type="submit" class="btn btn-primary">Guardar</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- <script>
    function calcSigmaR(nivel){
        let cvr_level = document.getElementById("cvr_"+nivel).value;
        let media_inserto_level = document.getElementById("media_inserto_"+nivel).value;
        let sigma_r = ((cvr_level * media_inserto_level) / 100 ).toFixed(3);
        document.getElementById("sigma_r_"+nivel).value = sigma_r;
    }
    function calcSigmaWL(nivel){
        let cvi_level = document.getElementById("cvi_"+nivel).value;
        let media_inserto_level = document.getElementById("media_inserto_"+nivel).value;
        let sigma_wl = ((cvi_level * media_inserto_level) / 100 ).toFixed(3);
        document.getElementById("sigma_wl_"+nivel).value = sigma_wl;
    }
    function calcSmpUnidad(nivel){
        let aps_level = '{{ $analitoEP15->etmp }}';
        let media_inserto_level = document.getElementById("media_inserto_"+nivel).value;
        let smp_unidad = ((aps_level * media_inserto_level)/200).toFixed(3);
        document.getElementById("smp_unidad_"+nivel).value = smp_unidad;
    }
    function calcDeInserto(nivel){
        let limite_s_level = document.getElementById("limite_s_"+nivel).value;
        let limite_i_level = document.getElementById("limite_i_"+nivel).value;
        let de_inserto = ((limite_s_level - limite_i_level) / 6).toFixed(3);
        document.getElementById("de_inserto_"+nivel).value = de_inserto;
    }
    document.addEventListener("input", function(event){
        let id = event.target.id;
        if(id != ""){
            let level = id.split("_").reverse()[0];
            if(id.includes("media_inserto")){
                calcSigmaR(level);
                calcSigmaWL(level);
                calcSmpUnidad(level);
            }
            else if(id.includes("cvr")) calcSigmaR(level);
            else if(id.includes("cvi")) calcSigmaWL(level);
            else if(id.includes("limite_s")) calcDeInserto(level);
            else if(id.includes("limite_i")) calcDeInserto(level);
        }
    })
</script> -->

<script>
    document.addEventListener("input", function(event){
        let id = event.target.id;
        if(id != ""){
            let level = id.split("_").reverse()[0];
            if(id.includes("valor_diana") || id.includes("smp_porcentaje")){
                let smp_porcentaje = document.getElementById("smp_porcentaje_"+level).value;
                let valor_diana = document.getElementById("valor_diana_"+level).value;
                let smp_unidad = ((valor_diana * smp_porcentaje) / 100).toFixed(3);
                document.getElementById("smp_unidad_"+level).value = smp_unidad;
            }
        }
    })
</script>