var exhibitionId = getUrlParam("id");
var exhibitionStyle = getUrlParam("style")
$(function () {
    $.ajax({
        type: "POST",
        url: "/manage_exhibitions/edit_venue",
        data: JSON.stringify({
            id: Number(exhibitionId),
            room_index: 3
        }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            content = ''
            for (let i = 0; i < data.number_of_position; i++) {
                let number = i + 1;
                let exhibit = null;
                if (data.exhibits != null && data.exhibits.length > 0) {
                    for (let j = 0; j < data.exhibits.length; j++) {
                        if (data.exhibits[j].Number == number) {
                            exhibit = data.exhibits[j];
                        }
                    }
                }
                if (exhibit != null) {
                    content += getExhibitItem(exhibit, number)
                } else {
                    content +=
                        `<div class="exhibit-panel" id="exhibit-panel">
                        <div class="cover-frame">
                            <h1>空</h1>
                        </div>
                        <h2 class="number">#${number}</h2>
                        <input class="edit-btn" type="button" id="number=${number}" value="编辑" onclick="edit(this)">
                    </div>`
                }
            }
            $('#exhibits-panel').html(content)
        },
        error: function (xhr, textStatus, error) {
            var msg = JSON.parse(xhr.responseText);
            alert(msg.error);
        }
    });
})

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

function edit(btn) {
    window.location.assign("/view/html/edit_studio_exhibit.html?exhibition_id=" + exhibitionId + "&" + btn.id + "&style="+exhibitionStyle); // 展览id
}

function getExhibitItem(exhibit, number) {
    if (isImage(exhibit.Url)) {
        return `<div class="exhibit-panel" id="exhibit-panel">
        <div class="cover-frame">
            <img class="cover" id="cover" src="${exhibit.Url}">
        </div>
        <h2 class="number">#${number}</h2>
        <input class="edit-btn" type="button" id="exhibit_id=${exhibit.ID}&number=${number}" value="编辑" onclick="edit(this)">
    </div>`
    } else if (isVideo(exhibit.Url)) {
        return `<div class="exhibit-panel" id="exhibit-panel">
        <div class="cover-frame">
            <video class="cover" id="cover" src="${exhibit.Url}" controls></video>
        </div>
        <h2 class="number">#${number}</h2>
        <input class="edit-btn" type="button" id="exhibit_id=${exhibit.ID}&number=${number}" value="编辑" onclick="edit(this)">
    </div>`
    }
}