let options = {
    diapason_akk: [],
};

$('#parser_start').on('click', async function () {
    //{"chats":["@dfgggh",http....","t.me..."],"online":[0,48],"type":["admin","bots","alluser"]}
    //Получение данных из textarea

    clear_log = 0
    counter_send = 0
    let json = JSON.stringify(options);
    //Получили json обьект
    let this_class = $(this).hasClass("btn-success");
    if (this_class == true) {
        // =============
        counter_send = 0
        $("#num_send_user").text(counter_send);
        // =============
        $(this).removeClass('btn-success');
        $(this).addClass('btn-danger');
        $(this).text("Остановить");
        $("#get_user_num").addClass('d-none');
        $("#get_user_num").text('0');
        $('#log li').remove();
        console.log(json)
        await eel.uploading_addlist(json)();
    } else {
        $(this).removeClass('btn-danger');
        $(this).addClass('btn-success');
        $(this).text("Запустить");
        eel.stopped('uploading_addlist') //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ТУТ КНОПКА ОТМЕНИТЬ
    }

});

window.addEventListener("beforeunload", function (e) {        
    eel.stopped('uploading_addlist')
});

eel.expose(set_finished_btn);
function set_finished_btn(){
    $('#parser_start').removeClass('btn-danger');
    $('#parser_start').addClass('btn-success');
    $('#parser_start').text("Запустить");
}


let clear_log = 0
eel.expose(log);
function log(log_line) {
  if (clear_log == 250){
    $('#log li:first').remove();
  } else {clear_log = clear_log + 1}
  $('#log').append('<li>' + log_line + '</li>');
  $('#log_div').scrollTop($('#log_div')[0].scrollHeight);
}

eel.expose(get_addlist);
function get_addlist(number) {
    $("#get_user_num").removeClass('d-none');
    $("#get_user_num").text(number);// Вывод количества собраных username
}

$('#get_user').on('click', function () {
    var link = document.createElement('a');
    link.setAttribute('href', '/uploads/chats_addlist.txt');
    link.setAttribute('download', 'chats_addlist.txt');
    link.click();
    return false;
});


$('#select_accounts').on('click', async function () {
    options.diapason_akk = [];
    load_accounts();
});

$('#select_accounts_apply').on('click', async function () {
    $('#select_accounts_modal').modal('hide');
    let from = 1;
    let before = $('#table_users >tbody >tr').length;
    //Запускаем цикл по всей таблице
    for (let i = from; i <= before; i++) {
        let type_bg = $('#table_users tr').eq(i).attr("class");
        if (type_bg == "bg_selected") {
        //Получаем номер телефона
        phone_user = $('#table_users tr').eq(i).children('#phone').html();
        options.diapason_akk.push(phone_user);
        }
    }
    $("#account_num").removeClass('d-none');
    $("#account_num").text(options.diapason_akk.length);
});

// eel.expose(error);
function error(text) {
    Swal.fire({
        position: 'center',
        icon: 'error',
        title: 'Ошибка',
        text: text,
        showConfirmButton: false,
        timer: 2100
    })
}