$(function () {
    $.ajax({
        type: "GET",
        url: "/manage/get_users",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (users) {
            let content = '';
            users.forEach(user => {
                content += 
                `<div class="user-panel" id="${user.Email}">
                    <h2 class="user-text">${user.DisplayName}</h2>
                    <h2 class="user-text">${user.Email}</h2>
                    <p class="p-role">${parseUserRole(user.Role)}</p>
                    <input class="input-view-btn" id="${user.ID}" type="button" value="查看" onclick="view(this)">
                </div>`
            })
            $('#users-panel').html(content)
        },
        error: function (xhr, textStatus, error) {
            var msg = JSON.parse(xhr.responseText);
            alert(msg.error);
        }
    });
})

function addUser() {
    var email = document.getElementById("email").value;
    if (email.length == 0) {
        alert("邮箱不可为空！");
        return;
    }
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    if (!reg.test(email)) {
        alert("邮箱格式错误！");
        return;
    }
    var role = document.getElementById("role").value;
    var displayName = document.getElementById("disPlay_name").value;
    $.ajax({
        type: "POST",
        url: "/manage_users/add",
        data: JSON.stringify({ email: email, role: role, display_name: displayName }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.error != null) {
                alert(data.error)
            } else {
                let content = 
                `<div class="user-panel" id="${data.Email}">
                    <h2 class="user-text">${data.DisplayName}</h2>
                    <h2 class="user-text">${data.Email}</h2>
                    <p class="p-role">${parseUserRole(data.Role)}</p>
                    <input class="input-view-btn" id="${data.ID}" type="button" value="查看" onclick="view(this)">
                </div>`
                $('#users-panel').prepend(content)
            }
        },
        error: function (xhr, textStatus, error) {
            var msg = JSON.parse(xhr.responseText);
            alert(msg.error);
        }
    });
}

function view(btn) {
    window.location.assign("/view/html/manage_publisher_exhibitions.html?id=" + btn.id);
}

function deleteUser(btn) {
    var email = btn.parentNode.id;
    if (email.length == 0) {
        alert("邮箱不可为空！");
        return;
    }
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    if (!reg.test(email)) {
        alert("邮箱格式错误！");
        return;
    }
    if (confirm("确定删除该用户吗?")) {
        $.ajax({
            type: "POST",
            url: "/manage_users/delete",
            data: JSON.stringify({ email: email }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (user) {
                var usersPanel = document.getElementById("users-panel");
                usersPanel.removeChild(btn.parentNode);
                alert("删除用户(" + user.email + ")成功")
            },
            error: function (xhr, textStatus, error) {
                var msg = JSON.parse(xhr.responseText);
                alert(msg.error);
            }
        });
    }
}