function messages(type) {
    if (!type) {
        type = $('.right-tab li.active').index();
    }
    var html = $('#tab-' + (type + 1)).find('.chat_box');
    html.html('');
    helper.init.isload = 0;
    $.ajax({
        type: "get",
        url: huayi.config.callcenter_url + "Index/GetChatLists",
        async: true,
        dataType: 'json',
        data: {
            token: $.cookie("token"),
            isread: type,
            top: 50,
            fromid: 0
        },
        success: function (data) {
            helper.init.isload = 1;
            $('#tab-1').find('.message_count').html(data.total);
            if (type == 0) {
                if (data.total == 0) {
                    $('.readed').hide();
                    $('.chat_count').hide();
                } else {
                    $('.chat_count').show();
                    $('.chat_count').html(data.total);
                    $('.readed').show();
                }
                $('.close_chat').show();
            }
            $(data.rows).each(function (i, n) {
                var str = '';
                str = '<div class="sidebar-message">' +
                        '<a href="#">' +
                            '<div class="pull-left text-center">' +
                                '<img alt="image" class="img-circle message-avatar" src="img/chat_pic.jpg">' +
                                '<div class="m-t-xs">' + n.F_UserName + '</div>' +
                            '</div>' +
                            '<div class="media-body">' +
                                '<div class="chat_content">' + GetCont(n.Content) + '</div>' +
                                '<small class="text-muted">' + n.CreateDate + '</small>' +
                               '<span class="reply"  data-FromUserId="' + n.FromUserId + '" data-ToUserId="' + n.ToUserId + '" data-roleId="' + n.F_RoleId + '" onclick="resply(this)">回复</span>'
                if (n.IsRead != 1) {
                    str += '<span class="reply close_chat" data-index="' + n.Id + '" onclick="closed(this)">关闭</span>'
                }
                '</div>' +
            '</a>' +
        '</div>'
                html.append(str);
            })
        }
    });
}
function closed(a) {
    var id = $(a).attr('data-index');
    $.ajax({
        type: "post",
        url: huayi.config.callcenter_url + "Index/CloseChat",
        async: true,
        dataType: 'json',
        data: {
            token: $.cookie("token"),
            id: id
        },
        success: function (data) {
            if (data.total > 0) {
                messages()
            } else {
                layer.msg("关闭消息失败！");
            }
        }
    });
}

$(document).ready(function(){
	
    $('.right-tab li').click(function(){
        messages($(this).index());
    })
	
    //全部已读
    $('.readed').click(function(){
        $.ajax({
            type:"get",
            url:huayi.config.callcenter_url+"Index/CloseChat",
            async:true,
            dataType:'json',
            data:{
                token:$.cookie("token"),
                id:0
            },
            success:function(data){
                if(data.total>0){
                    messages()
                }else{
                    layer.msg("设置失败！");
                }
            }
        });

    })

})
		//回复
function resply(a) {
    var userId = $(a).attr('data-FromUserId')
    var toId = $(a).attr('data-ToUserId')
    var roleId = $(a).attr('data-roleId')
    layer.open({
        type: 2,
        content: "replyChat.html?userId=" + userId + "&roleId=" + roleId + "&toId=" + toId, //iframe的url，no代表不显示滚动条
        title: '回复消息',
        area: ['50%', '60%'], //宽高
    });
}
//主动发消息
$('.sele_send').click(function () {
    layer.open({
        type: 2,
        content: "replyChat.html", //iframe的url，no代表不显示滚动条
        title: '发送消息',
        area: ['50%', '60%'], //宽高
    });
})
		//回复
function GetCont(val) {
    if (val) {
        var str = '<div '
        if (val.length > 10) {
            str = str + ' title="' + val + '" ';
            val = val.substr(0, 25) + "...";
        }
        return str + '>' + val + '</div>';
    } else {
        return '';
    }
}

