function page_profile_supplies(form, panel) {
    if (form === undefined) {
        form = {
            '_csrf': yii.getCsrfToken()
        };

        if (panel !== undefined) {
            form.panel = panel;
        };
    } else {
        form = new FormData(form);

        if (panel !== undefined) {
            form.append('panel', panel);
        };
    };

    $.ajax({
        url: '/profile/supplies',
        type: 'post',
        dataType: 'json',
        data: form,
        success: page_profile_response_success,
        error: page_profile_response_error
    });

    return false;
};

function page_profile_supplies_import_excel(form, panel) {
    if (form === undefined) {
        form = {
            '_csrf': yii.getCsrfToken()
        };

        if (panel !== undefined) {
            form.panel = panel;
        };
    } else {
        form = new FormData(form);

        if (panel !== undefined) {
            form.append('panel', panel);
        };
    };

    $.ajax({
        url: '/profile/import',
        type: 'post',
        dataType: 'json',
        processData: false,
        contentType: false,
        data: form,
        success: page_profile_response_success,
        error: page_profile_response_error
    });

    return false;
};

function page_profile_settings(form, panel) {
    if (form == undefined) {
        form = {
            '_csrf': yii.getCsrfToken()
        };

        if (panel !== undefined) {
            form.panel = panel;
        };
    } else {
        form = $(form).serializeArray();

        form.push({
            name: "panel",
            value: panel
        });
    };

    $.ajax({
        url: '/profile',
        type: 'post',
        dataType: 'json',
        data: form,
        success: page_profile_response_success,
        error: page_profile_response_error
    });

    // Реинициализация
    initDropdownOem();

    return false;
};

function page_profile_monitoring(change = 1, panel) {
    // Инициализация
    let url = new URL(document.location);
    let search = url.searchParams.get('search');

    if (search === null) {
        search = 1;
    } else {
        search = +search + change;
    };

    // Предобработка
    url.searchParams.set('search', search);

    $.ajax({
        url: '/profile/monitoring',
        type: 'post',
        dataType: 'json',
        data: {
            '_csrf': yii.getCsrfToken(),
            'search': search,
            'panel': panel
        },
        success: function (data, status) {
            page_profile_response_success(data, status);

            // Ренициализация
            let url = new URL(document.location);

            // Запись параметра в URL
            url.searchParams.set('search', data.search);

            // Запись в историю
            history.pushState('', document.title, url.toString());
        },
        error: page_profile_response_error
    });

    return false;
};


function page_profile_response(data, status, xhr) {
    // Обработка ответов

    if (data !== undefined) {
        // Получены данные с сервера

        // Активная панель
        if (data.panel_selected !== undefined) {
            // Активация
            document.getElementById(data.panel_selected).click();
        };
    };

    main_response(data, status, xhr);
};

function page_profile_response_success(data, status, xhr) {
    // Обработка ответов от удавшихся запросов

    page_profile_response(data, status, xhr);
};

function page_profile_response_error(data, status, xhr) {
    // Обработка ответов от неудавшихся запросов

    // Инициализация
    data = data.responseJSON;

    page_profile_response(data, status, xhr);
};
