let options = {
    diapason_akk: [],
    delete_mode: 0,
    view_sms_mode: 0,
    time_pars: [1,3],
};

$('#activity_start').on('click', async function () {


    let time_from = $('#time_from').val();
    let time_before = $('#time_before').val();
    options.time_pars[0] = time_from;
    options.time_pars[1] = time_before;

    if ($('#delete_mode').is(':checked')){
        options.delete_mode = 1;
    } else {
        options.delete_mode = 0;
    }

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

});

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

eel.expose(set_finished_btn);
function set_finished_btn(){
    $('#activity_start').removeClass('btn-danger');
    $('#activity_start').addClass('btn-success');
    $('#activity_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(success_count);
function success_count(num) {
  $("#num_pinned_user").text(num); 
}


$('#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
    })
}