/**
 * [OEUI] (C)2010-2099 oelove.com Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * $ Last update 2020/11/13 by CL [ chat func ] $
*/
//滚动条
function _scrollToFoot() {
    $('#oe_chat').animate({ scrollTop: $('#chat_content').height() }, 200);
}
//发送次数已满提示
function _noChatTimesTips(sid) {
    if (typeof(sid) == 'undefined') {
        sid = $("#msgchat_sid").val();
        if (typeof(sid) == 'undefined') {
            sid = '';
        }
    }
    OEUI.modal.warning({
        title: '温馨提示',
        text: "对不起，您今天的发信次数已超额<br />可以通过升级VIP解除限制。",
        confirmText: '立即升级',
        cancelText: '知道了',
        confirm: function () {
            
            //确定
            var back_url = _ROOT_PATH + "index.php?m=user&c=msgcontact&nofollow=1";
            window.top.location.href = _ROOT_PATH + "index.php?m=user&c=vip&forward="+ encodeURIComponent(back_url);

        },
        cancel: function () {
            //取消
        }
    });
}

//请求解锁
function _payFreeSid(sid) {
    if (typeof(sid) == 'undefined') {
        sid = $("#msgchat_sid").val();
        if (typeof(sid) == 'undefined') {
            sid = '';
        }
    }
    
    $.ajax({
        type: "POST",
        url: _ROOT_PATH+"index.php?m=user&c=msgchat",
        cache: false,
        data: {
            a: "freesid", sid: sid, datatype: "json"
        },
        dataType: "json",
        success: function (data) {
            var response = data.response;
            var result = data.result;
            if (response == "1") {
                OEUI.message.msg({
                    text: '解锁成功'
                });
                if ($("#help_freelock_box").length > 0) {
                    if ($("#help_freelock_box").css("display") == "block") {
                        $("#help_freelock_box").hide();
                    }
                }
                setTimeout(function () {
                    _getMsgchatPageList(1);
                }, 500);
            }
            else if (response == "2") {
                //在线支付...
                var back_url = _ROOT_PATH + "index.php?m=user&c=msgcontact&nofollow=1";
                window.top.location.href = _ROOT_PATH + "index.php?c=pay&paynum="+result+"&forward="+ encodeURIComponent(back_url);
            }
            else {
                if (result == '' || result == null) {
                    result = '解锁失败';
                }
                OEUI.message.msg({
                    text: result
                });
            }
        },
        error: function () {
            OEUI.message.msg({
                text: '系统繁忙，请稍后再试'
            });
        }
    });
}

//请求获取聊天记录
function _getMsgchatPageList(page, callBack) {
    callBack = callBack || function () { };
    if (typeof (page) == "undefined") {
        page = 1;
    }
    var sid = $("#msgchat_sid").val(); //SID
    var obj_box = $("#chat_log_data");
    
    $.ajax({
        type: "GET",
        url: _ROOT_PATH + "index.php?m=user&c=msgchat&a=list&datatype=json",
        cache: false,
        data: {
            sid: sid, page: page
        },
        dataType: "json",
        success: function (data) {
            var prepage = data.prepage; //上一页
            var nextpage = data.nextpage; //下一页
            var result = data.result;
            if (result != '') {
                if (page == "1") {
                    obj_box.html(result);
                }
                else {
                    obj_box.prepend(result);
                }
                if (nextpage > 0) { //还有下一页
                    $("[f='but_get_msgchatpage']").attr("data-page", nextpage);
                    $("[f='but_get_msgchatpage']").show();
                    $("[f='but_get_msgchatpage']").find("img").hide();
                }
                else {
                    $("[f='but_get_msgchatpage']").hide();
                }

                if (page == "1") {
                    _scrollToFoot();

                    //获取聊天记录最后一条信件ID 9.10.22
                    var lastid = data.lastid;
                    if (typeof (lastid) == "undefined") {
                        lastid = "0";
                    }
                    $("#lastid").val(lastid);

                    //时时获取最后一条消息 9.10.22
                    if (MSGCHAT_KEEP == "1") {
                        setTimeout(function () {
                            _getLastList();
                        }, MSGCHAT_TIMER);
                    }

                }
            }
        },
        error: function () {

        }
    });
}

//发送完毕，获取最后一条信息
function _getFinishSend(lastid) {
    $.ajax({
        type: "GET",
        url: _ROOT_PATH + "index.php?m=user&c=msgchat",
        cache: false,
        data: {
            a: "getlast", id: lastid
        },
        dataType: "json",
        success: function (data) {
            var response = data.response;
            var result = data.result;
            if (response == '1') {
                if (result != '') {
                    $("#chat_log_data").append(result);
                    _scrollToFoot();
                    //重置lastid 
                    var input_lastid = $("#lastid").val();
                    if (typeof (input_lastid) == "undefined") {
                        input_lastid = 0;
                    }
                    else {
                        input_lastid = parseInt(input_lastid);
                    }
                    if (lastid > input_lastid) {
                        $("#lastid").val(lastid);
                    }
                }
            }
        },
        error: function () {

        }
    });
}

//ajax 更新对方消息  
function _getLastList() {
    var sid = $("#msgchat_sid").val(); //SID
    var lastid = $("#lastid").val(); //last
    var obj_box = $("#chat_log_data");
    $.ajax({
        type: "POST",
        url: _ROOT_PATH + "index.php?m=user&c=msgchat",
        cache: false,
        data: {
            a: "lastlist", sid: sid, lastid: lastid
        },
        dataType: "json",
        success: function (data) {
            var result = data.result;
            var total = data.total;
            if (result != '' && total != "0") {
                obj_box.append(result);
                _scrollToFoot();

                //获取聊天记录最后一条信件ID 
                var lastid = data.lastid;
                if (typeof (lastid) == "undefined") {
                    lastid = "0";
                }
                if (lastid != "" && lastid != "0") {
                    $("#lastid").val(lastid);
                }

                if (MSGCHAT_KEEP == "1") {
                    setTimeout(function () {
                        _getLastList();
                    }, MSGCHAT_TIMER);
                }
            }
            else {

                if (MSGCHAT_KEEP == "1") {
                    setTimeout(function () {
                        _getLastList();
                    }, MSGCHAT_TIMER);
                }
            }
        },
        error: function () {

        }
    });
}

