var table = document.getElementById('table');
var filter = document.getElementById('filter');
var limit = document.getElementById('limit');
var view = document.getElementById('view');
var user = document.getElementById('user');
var code = document.getElementById('code');
var frame_api = document.getElementById('frame_api');
var button_view = document.getElementById('button_view');
var type_form = document.getElementById('type_form');
var add;
var show;
var edit;
var search;

function links_show(links, value) {
    version_form();
    $.ajax({
        type: 'get',
        url: 'modules/api/ajax.php?link=' + links + "&user=" + user.value + "&id=" + value,
        response: JSON,
        success: function (data) {
            data = JSON.parse(data);
            if (links === 'table') {
                text = '<option></option>';
                data.forEach(function (item, i, data) {
                    text += "<option value='" + item.id + "'>" + item.name_table + "</option>"
                })
                table.innerHTML = text;
            } else {
                text = '<option></option>';
                data.forEach(function (item, i, data) {
                    text += "<option value='" + item.id + "'>" + item.name + "</option>"
                })
                filter.innerHTML = text;
            }
        }
    });
}

function generate_code(root_site) {
    if (user.value === '') {
        alert('Выберите пользователя');
    } else if (table.value === '') {
        alert('Выберите таблицу');
    } else if (filter.value === '' && $('#filter').find('option').length > 1) {
        alert('Выберите фильтр');
    } else if (limit.value === '') {
        alert('Задайте лимит');
    } else if (view.value === '') {
        alert('Задайте количество строк');
    } else {
        if (show.checked) {
            show_view = show.value;
        } else {
            show_view = 0;
        }
        if (add.checked) {
            add_view = add.value;
        } else {
            add_view = 0;
        }
        if (edit.checked) {
            edit_view = edit.value;
        } else {
            edit_view = 0;
        }
        if (search.checked) {
            search_view = search.value;
        } else {
            search_view = 0;
        }
        $.post(
            "modules/api/ajax.php",
            {
                link: "generate",
                table: table.value,
                filter: filter.value || 0,
                limit: limit.value,
                view: view.value,
                user: user.value,
                show: show_view,
                add: add_view,
                edit: edit_view,
                search: search_view
            },
            onAjaxSuccess
        );

        function onAjaxSuccess(data) {
            code.innerHTML = data;
            button_view.style.display = "inline-block";
        }

    }
}

function view_code(root_site) {
    if (user.value !== '' && table.value !== '' && filter.value !== '' && limit.value !== '' && view.value !== '') {
        frame_api.style.display = 'block';
        frame_api.src = root_site + "/modules/api/view.php";
    }
}

function version_form() {
    full_version = document.getElementById('full_version');
    full_version.style.display = '';
    add = document.getElementById('add');
    show = document.getElementById('show');
    edit = document.getElementById('edit');
    search = document.getElementById('search');
}
