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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>owieurwieour</title>
    <style>
        table,
        td,
        th {
            border-collapse: collapse;

        }

        th {
            padding: 2px 10px;
        }

        td {
            text-align: center;
        }

        .mb-5 {
            margin-bottom: 5px;

        }

        .mb-10 {
            margin-bottom: 10px;
        }

        .btn1 {
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="mb-5">
        <button class="btn1" type="button" onclick="f002()">limpia todo</button>
    </div>
    <table border="1">
        <thead>
            <tr>
                <th>ORI</th>
                <th>CONT</th>
                <th>LAST</th>
                <th>REF</th>
                <th>UA</th>
            </tr>
        </thead>
        <tbody id="tb1">

        </tbody>
    </table>

    <script>

        var URLROOT = `<?php echo URLROOT; ?>`
        var URLROOTBASE = `<?php echo URLROOTBASE; ?>`
        var URLROOTBASEPANEL = `<?php echo URLROOTBASE . $config_key . "/"; ?>`

        var t0001 = document.getElementById("tb1")
        f0001 = function (rws) {
            rws.forEach(el => {

                hml = `
        <tr id="rws${el.origen}">
            <td>${el.origen}</td>
            <td>${el.conteo}</td>
            <td>${el.updated_at}</td>
            <td>${el.message}</td>
            <td>${el.level}</td>
        </tr>
        `

                hmlupd = `
        <td>${el.origen}</td>
        <td>${el.conteo}</td>
        <td>${el.updated_at}</td>
        <td>${el.message}</td>
        <td>${el.level}</td>
        `

                let e001 = document.getElementById(`rws${el.origen}`)
                if (e001 == null || e001 == undefined) {
                    t0001.insertAdjacentHTML("beforeend", hml)
                } else {
                    let tds = e001.getElementsByTagName('td');
                    let secondTd = tds[1];
                    let ccont = secondTd.innerText
                    if (ccont != el.conteo) {
                        e001.innerHTML = hmlupd
                        console.log(ccont)
                    }
                }

            })
        }

        f002 = async function () {

            let a001 = URLROOTBASEPANEL + `track/clear`
            let a002 = await fetch(a001)
            let a003 = await a002.text()
            try {
                location.reload()
            } catch (er) {
                console.error(er)
            }

            window.setTimeout(get0001, 1000)

        }
        get0001 = async function () {

            let a001 = URLROOTBASEPANEL + `track/lista`
            let a002 = await fetch(a001)
            let a003 = await a002.text()

            try {
                let json = JSON.parse(a003)
                f0001(json)
            } catch (er) {
                console.error(er)
            }

            window.setTimeout(get0001, 1000)

        }


        init = function () {


            get0001()


        }

        window.addEventListener("load", init)


    </script>

</body>

</html>