/**
 * 消息通知相关js
 *
 * @author CaiAoLin
 * @date 2017/9/25
 * @version
 */
let isLoading = false;
$(document).ready(function() {

    // 消息框弹出
    $("#msg").on("shown.bs.modal", function() {
        let loadingElement = $("#message-loading");
        $.ajax({
            url: '/message/list',
            type: 'post',
            data: '',
            dataType: 'json',
            error: function() {
                loadingElement.text('加载数据失败');
            },
            beforeSend: function() {
                loadingElement.text('正在加载数据').show();
            },
            success: function(response) {
                loadingElement.text('').hide();
                $("#message-list").show();
                if (response.err === 1 || response.err === undefined) {
                    let msg = response.msg !== undefined ? response.msg : '读取数据失败!';
                    alert(msg);
                    return false;
                }
                let listData = response.data;
                if (listData.length <= 0) {
                    return false;
                }
                let listHtml = '';
                for(let tmp of listData) {
                    let unreadHtml = tmp.is_read === 0 ? '<i class="fa fa-circle-o text-danger unread" title="未读"></i> ' : '';
                    let dateString = moment(tmp.message.release_time).format('YYYY-MM-DD');
                    let dateTimeString = moment(tmp.message.release_time).format('YYYY-MM-DD HH:mm:ss');
                    listHtml += '<tr>' +
                        '<td>'+ unreadHtml +'<a href="javascript:void(0);" class="detail" data-id="'+ tmp._id +'">'+ tmp.message.title +'</a></td>' +
                        '<td><span title="'+ dateTimeString +'">'+ dateString +'</span></td>';
                }

                $("#message-list tbody").html(listHtml);
            }
        });
    });

    // 获取详细消息
    $("#msg").on("click", "a.detail", function() {
        let self = $(this);
        let id = self.data("id");
        if (id === undefined || id === '' || isLoading) {
            return false;
        }
        let isUnread = self.prev().is(".unread");
        let loadingElement = $("#message-loading");
        let listElement = $("#message-list");
        $.ajax({
            url: '/message/detail',
            type: 'post',
            data: 'id=' + id,
            dataType: 'json',
            error: function() {
                isLoading = false;
                listElement.show();
                loadingElement.text('').hide();
            },
            beforeSend: function() {
                isLoading = true;
                listElement.hide();
                loadingElement.text('正在加载详细内容').show();
            },
            success: function(response) {
                isLoading = false;
                loadingElement.text('').hide();
                let messageData = response.data;
                if (response.err === 1 || response.err === undefined || messageData === undefined) {
                    let msg = response.msg !== undefined ? response.msg : '读取数据失败!';
                    listElement.show();
                    loadingElement.text('').hide();
                    alert(msg);
                    return false;
                }
                // 移除未读标记
                if (isUnread) {
                    self.prev('i').remove();
                    changeNumByStep(-1);
                }
                $("#message-content .title").text(messageData.message.title);
                $("#message-content .time").text('发布：' + moment(messageData.message.release_time).format('YYYY-MM-DD HH:mm:ss'));
                $("#message-content .content-area").html(messageData.message.content);
                $("#message-content").show();
            }
        });
    });

    // 设置全部已读
    $("#set-all-read").click(function() {
        if (isLoading) {
            return false;
        }
        $.ajax({
            url: '/message/read',
            type: 'post',
            data: '',
            dataType: 'json',
            error: function() {
                isLoading = false;
            },
            beforeSend: function() {
                isLoading = true;
            },
            success: function(response) {
                isLoading = false;
                if (response.err === 0) {
                    $("#message-list tbody .unread").remove();
                    changeNumByStep(0);
                } else {
                    let msg = response.msg !== undefined ? response.msg : '未知错误!';
                    alert(msg);
                }
            }
        });
    });

    // 返回按钮
    $("#message-content .back").click(function() {
        $("#message-content").hide();
        $("#message-list").show();
    });

});

/**
 * 设置头部信封数量
 *
 * @param {Number} step
 * @return {void}
 */
function changeNumByStep(step) {
    let messageElement = $("#notify-info");
    let unreadElement = $("#unread-num");
    if (step === 0) {
        messageElement.removeClass('new-msg');
        unreadElement.text(0);
    } else {
        let currentNum = unreadElement.text();
        currentNum = parseInt(currentNum);
        let num = currentNum + step;
        if (num === 0) {
            messageElement.removeClass('new-msg');
        }
        unreadElement.text(num);
    }
}
