var userId = getUrlParam("id");
$(function () {
    $.ajax({
        type: "POST",
        url: "/manage/get_publisher_exhibitions",
        data: JSON.stringify({ user_id: Number(userId) }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (exhibitions) {
            let content = '';
            for (let i = 0; i < exhibitions.length; i++) {
                let ex = exhibitions[i]
                content +=
                    `<div class="exhibition-panel">
                        <div class="index-panel">
                            <h2>${i + 1}</h2>
                        </div>
                        <div class="cover-frame">
                            <img class="cover" id="cover" width="100" height="100" src="${ex.Cover}">
                        </div>
                        <div class="title-panel">
                            <p>${parseExStyle(ex.Style)}</p>
                            <p>${parseExClassification(ex.Classification)}</p>
                            <p class="ex-title">${ex.Title}</p>
                            <p class="ex-subtitle">${ex.Subtitle}</p>
                        </div>
                        <div class="time-panel">
                            <div>
                                <span>开始时间</span>
                                <span class="start-time" id="start-time">${conertToHtmlDate(ex.StartTime).replace('T', ' ')}</span>
                            </div>
                            <div>
                                <span>结束时间</span>
                                <span class="end-time" id="end-time">${conertToHtmlDate(ex.EndTime).replace('T', ' ')}</span>
                            </div>
                        </div>
                        <div class="btn-panel">
                            <div>
                                <input type="button" id="${ex.ID}" value="查看" onclick="view(this)">
                            </div>
                            <div>
                                <input type="button" id="${ex.ID}" value="编辑" onclick="edit(this)">
                            </div>
                        </div>
                        <div class="publish-panel">
                            <input type="button" class="cancel-publish-btn" id="${ex.ID}" value="取消发布" onclick="cancelPublish(this)" ${checkVisibleState(ex.Published)}>
                            <input type="button" class="publish-btn" id="${ex.ID}" value="发布" onclick="publish(this)" ${checkVisibleState(!ex.Published)}>
                        </div>
                    </div>
                    <hr class="split-line">`
            }
            $('ul').html(content)
        },
        error: function (xhr, textStatus, error) {
            var msg = JSON.parse(xhr.responseText);
            alert(msg.error);
        }
    });
})

function back() {
    history.back();
}

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

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

function publish(btn) {
    if (confirm("发布后，该展览将会被游客看到，确认发布吗？")) {
        $.ajax({
            type: "POST",
            url: "/manage_exhibitions/publish",
            data: JSON.stringify({ id: Number(btn.id) }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (user) {
                var btns = btn.parentNode.children
                for (let i = 0; i < btns.length; i++) {
                    if (btns[i].className == "cancel-publish-btn") {
                        btns[i].hidden = false
                    } else if (btns[i].className == "publish-btn") {
                        btns[i].hidden = true
                    }
                }
                alert("展览发布成功")
            },
            error: function (xhr, textStatus, error) {
                var msg = JSON.parse(xhr.responseText);
                alert(msg.error);
            }
        });
    }
}

function cancelPublish(btn) {
    if (confirm("取消发布后，游客将无法看到该展览，确认取消吗？")) {
        $.ajax({
            type: "POST",
            url: "/manage_exhibitions/cancel_publish",
            data: JSON.stringify({ id: Number(btn.id) }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (user) {
                var btns = btn.parentNode.children
                for (let i = 0; i < btns.length; i++) {
                    if (btns[i].className == "cancel-publish-btn") {
                        btns[i].hidden = true
                    } else if (btns[i].className == "publish-btn") {
                        btns[i].hidden = false
                    }
                }
                alert("展览已取消发布")
            },
            error: function (xhr, textStatus, error) {
                var msg = JSON.parse(xhr.responseText);
                alert(msg.error);
            }
        });
    }
}