let options = {
    diapason_akk: [],
    type_time: 'day',
    time_pars: [0,20],
    what_status_pars: []
};

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


    options.what_status_pars = [];
    //Получение данных типа пользователей
    if ($('#UserStatusOnline').is(':checked')) {
        options.what_status_pars.push("UserStatusOnline");
    } else { }
    if ($('#UserStatusLastMonth').is(':checked')) {
        options.what_status_pars.push("UserStatusLastMonth");
    } else { }
    if ($('#UserStatusLastWeek').is(':checked')) {
        options.what_status_pars.push("UserStatusLastWeek");
    } else { }
    if ($('#UserStatusRecently').is(':checked')) {
        options.what_status_pars.push("UserStatusRecently");
    } else { }
    if ($('#UserStatusEmpty').is(':checked')) {
        options.what_status_pars.push("UserStatusEmpty");
    } else { }
    if ($('#UserStatusAll').is(':checked')) {
        options.what_status_pars.push("UserStatusAll");
    } else { }



    let time_from = $('#time_from').val();
    let time_before = $('#time_before').val();
    if(parseInt(time_from) >= parseInt(time_before)){
        error('Значение времени "до" не может быть меньше!')
        return
    } else {
        options.time_pars[0] = time_from;
        options.time_pars[1] = time_before;
    }
    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_username_num").addClass('d-none');
        $("#get_username_num").text('0');
        $("#get_phone_username_num").addClass('d-none');
        $("#get_phone_username_num").text('0');
        $("#get_phone_num").addClass('d-none');
        $("#get_phone_num").text('0');
        $('#log li').remove();
        console.log(json)
        await eel.parser_contacts(json)();
    } else {
        $(this).removeClass('btn-danger');
        $(this).addClass('btn-success');
        $(this).text("Запустить");
        eel.stopped('parser_contacts') //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ТУТ КНОПКА ОТМЕНИТЬ
    }

});

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

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

$('#type_time').on('click', function(){
    let this_mode = $(this).attr('type_time');
    if(this_mode == 'hours'){
        $(this).text("дней");
        $(this).attr('type_time','day');
        options.type_time = 'day'
    } else {
        $(this).text("часов");
        $(this).attr('type_time','hours');
        options.type_time = 'hours'
    }
});

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_phone);
function get_phone(number) {
    $("#get_phone_num").removeClass('d-none');
    $("#get_phone_num").text(number);// Вывод количества собраных username
}

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

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

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

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

$('#get_phone_username').on('click', function () {
    var link = document.createElement('a');
    link.setAttribute('href', '/uploads/phone_nologins.txt');
    link.setAttribute('download', 'phone_nologins.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
    })
}