<%- include('./static/start.ejs',{name:"admin",async: true}) %>
<%- include('./header.ejs') %>
<style>
    section{
        width: 80vw;
        margin: auto;
    }
    body{
        background-color: #F0F8FF;
    }
</style>

<!-- <h1 style="text-align: center;">привет админ</h1> <br> -->

    <section class="admin_edits" style="display: flex; justify-content: space-evenly;">
        <button onclick="goto('/admin/objects')" class="btn_white">Панель управления объектами</button>
        <button onclick="goto('/admin/users')" class="btn_white">Панель управления пользователями</button>
        <div class="logs_div" style="margin: auto 0px;display: flex; margin-left: 20vw;">
            <button onclick="load_logs();" class="btn_blue">Скачать логи</button>
        </div>
    </section>
    <br>
    <section style="display: flex;">
        <div style="width: 7.5vw;">
            <h1>Логи</h1>
            <button onclick="get_logs();" class="btn_blue" style="margin-block: 0.5vh;">Обновить</button>
            <button onclick="del_logs()" class="btn_blue" style="margin-block: 0.5vh; font-size: calc(var(--main-font-size)/1.4);">Удалить логи</button>
            <label for="logs_check" style="font-size: calc(var(--main-font-size)/1.5);" style="margin-block: 0.5vh;">Автозагрузка</label>
            <input type="checkbox" id="logs_check" oninput="logs_check(event)" style="margin-block: 0.5vh;">
        </div>
        <div id="logs" style="background-color: white;width: 70vw;font-size: calc(var(--main-font-size)/1.4);overflow: auto;height: 80vh;"></div>
    </section>

<%- include('./static/end.ejs') %>
<script>
    log_check();
    function log_check(){
        if($.cookie("auto_log") != null){
            if(JSON.parse($.cookie("auto_log")) == true){
                get_logs();
            }
        document.getElementById("logs_check").checked = JSON.parse($.cookie("auto_log"));
        }else{
            $.cookie("auto_log",true)
            log_check()
        }
    }

    function get_logs() {
        let wait_msg = msg("Загрузка логов",{type:"wait"});
        $.get("/logs/get")
        .done(function( res ) {
            document.getElementById("logs").innerText = res["body"];
            msg_del(wait_msg.id);
            msg("Логи загружены")
        })
    }

    function del_logs() {
        msg("date",{type:"date",res:(date)=>{if(date != false){
            let wait_msg = msg(`Удаление логов за ${date}`,{type:"wait"});
            $.get("/logs/del",{date:date})
            .done(function( res ) {
                document.getElementById("logs").innerText = res["body"];
                msg_del(wait_msg.id);
                msg("Логи удалены")
            })
        }
        }})
    }

    function load_logs() {
        $.get("/logs/get")
        .done(function( res ) {
            downloadTextFile(res["body"],"logs.txt");
        })
    }

    function logs_check(event){
        let log_inp = event.target;
        let check = log_inp.checked;
        $.cookie("auto_log",check);
    }

    setTimeout(()=>{
        loaded()
    },1000);
</script>