$(function(){
    getFreeboardList();
});
function getFreeboardList(){
    App.post(BASE_URL + "home/ajaxFreeboardList", {}, function (resp) {
        if(resp.status == App.SUCCESS) {
            $(".nav-board li").removeClass('active');
            $(".nav-freeboard").addClass('active');
            $("#board_content .tab-pane").removeClass("active");
            $("#board_content #freeboard").addClass("active in");
            var html = ``;
            var data = resp.data;
            for(var i = 0; i < data.length; i++){
                html += `
                    <tr style="cursor:pointer">
                        <td data-id="${data[i]['id']}" onclick="gotoFreeboardDetail(${data[i]['id']})" style="width:160px;"><img src="${BASE_URL + 'assets/img/community/icon_photo.jpg'}" width="12px" style="margin-right: 10px;"/>${data[i]['title_formatted']}</td>
                        <td data-id="${data[i]['user_id']}" onclick="gotoUserHome(${data[i]['user_id']})"><img class="avatar" src="${BASE_URL + 'uploads/level/' + data[i]['level_icon']}" onerror="javascript: this.src='/uploads/level/default.png'">${data[i]['nickname']}</td>
                        <td>${data[i]['created_at_formatted'].substring(0,5)}</td>
                    </tr>
                `;
            }

            $("#board_content #freeboard tbody").html(html);
        } else {
            App.notify('error', resp.msg);
        }
    }, 'json');
}

function gotoFreeboardDetail(id){    
    location.href = BASE_URL + 'freeboard/detail?id=' + id + '&page=1';
}

function getVideoList(){
    App.post(BASE_URL + "home/ajaxVideoList", {}, function (resp) {
        if(resp.status == App.SUCCESS) {
            $(".nav-board li").removeClass('active');
            $(".nav-video").addClass('active');
            $("#board_content .tab-pane").removeClass("active");
            $("#board_content #video").addClass("active in");
            var html = ``;
            var data = resp.data;
            for(var i = 0; i < data.length; i++){
                html += `
                    <tr style="cursor:pointer">
                        <td data-id="${data[i]['id']}" onclick="gotoVideoDetail(${data[i]['id']})" style="width:160px;"><img src="${BASE_URL + 'assets/img/community/icon_photo.jpg'}" width="12px" style="margin-right: 10px;"/>${data[i]['title_formatted']}</td>
                        <td data-id="${data[i]['user_id']}" onclick="gotoUserHome(${data[i]['user_id']})"><img class="avatar" src="${BASE_URL + 'uploads/level/' + data[i]['level_icon']}" onerror="javascript: this.src='/uploads/level/default.png'">${data[i]['nickname']}</td>
                        <td>${data[i]['created_at_formatted'].substring(0,5)}</td>
                    </tr>
                `;
            }

            $("#board_content #video tbody").html(html);
        } else {
            App.notify('error', resp.msg);
        }
    }, 'json');
}

function gotoVideoDetail(id){    
    location.href = BASE_URL + 'video/detail?id=' + id + '&page=1';
}

function getHumorList(){
    App.post(BASE_URL + "home/ajaxHumorList", {}, function (resp) {
        if(resp.status == App.SUCCESS) {
            $(".nav-board li").removeClass('active');
            $(".nav-humor").addClass('active');
            $("#board_content .tab-pane").removeClass("active");
            $("#board_content #humor").addClass("active in");
            var html = ``;
            var data = resp.data;
            for(var i = 0; i < data.length; i++){
                html += `
                    <tr style="cursor:pointer">
                        <td data-id="${data[i]['id']}" onclick="gotoHumorDetail(${data[i]['id']})" style="width:160px;"><img src="${BASE_URL + 'assets/img/community/icon_photo.jpg'}" width="12px" style="margin-right: 10px;"/>${data[i]['title_formatted']}</td>
                        <td data-id="${data[i]['user_id']}" onclick="gotoUserHome(${data[i]['user_id']})"><img class="avatar" src="${BASE_URL + 'uploads/level/' + data[i]['level_icon']}" onerror="javascript: this.src='/uploads/user/default.png'">${data[i]['nickname']}</td>
                        <td>${data[i]['created_at_formatted'].substring(0,5)}</td>
                    </tr>
                `;
            }

            $("#board_content #humor tbody").html(html);
        } else {
            App.notify('error', resp.msg);
        }
    }, 'json');
}

function gotoHumorDetail(id){    
    location.href = BASE_URL + 'humor/detail?id=' + id + '&page=1';
}

function getPhotoList(){
    App.post(BASE_URL + "home/ajaxPhotoList", {}, function (resp) {
        if(resp.status == App.SUCCESS) {
            $(".nav-board li").removeClass('active');
            $(".nav-photo").addClass('active');
            $("#board_content .tab-pane").removeClass("active");
            $("#board_content #photo").addClass("active in");
            var html = ``;
            var data = resp.data;
            for(var i = 0; i < data.length; i++){
                if(data[i]['exist_img'] == 1){
                    html += `<div class="top-photo-item">
                        <img src="${data[i]['main_img']}" data-id="${data[i]['id']}" 
                        onclick="gotoPhotoDetail(${data[i]['id']})"
                         onerror="javascript: this.src='/uploads/img/1.png'" style="cursor:pointer">
                    </div>
                    `;
                }
            }

            $("#board_content #photo").html(html);
        } else {
            App.notify('error', resp.msg);
        }
    }, 'json');
}

function gotoPhotoDetail(id){    
    location.href = BASE_URL + 'photo/detail?id=' + id + '&page=1';
}

function gotoFreeboards(){
    location.href = BASE_URL + 'freeboard';
}

function gotoPhoto(){
    location.href = BASE_URL + 'photo';
}

function gotoHumor(){
    location.href = BASE_URL + 'humor';
}

function gotoVideo(){
    location.href = BASE_URL + 'video';
}

