/**
 * [OEUI] (C)2010-2099 oelove.com Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * $ Last update 2020/11/13 by CL $
*/
$(function () {

    function OEMsgchat() {
        this._this_dialog = null;
        this.tips_dialog = null;
        this.obj_id = '';
        this.chat_uid = '';
        this.chat_sid = '';
        
        this.initChat();
        this.clickGetPage();
        this.clickEm();
        this.clickSend();
        this.clickTips();
        this.clickFreeSid();
    }

    OEMsgchat.prototype = {
        
        //初始化聊天
        initChat:function() {
            var _this = this;
            
            _this.chat_uid = $('#chat_uid').val();
            _this.chat_sid = $('#msgchat_sid').val();
            
            $.ajax({
                type: "POST",
                url: _ROOT_PATH+"index.php?m=user&c=msgchat",
                cache: false,
                data: {
                  a:"initcheck", buid:_this.chat_uid, datatype:"json"
                },
                dataType: "json",
                success: function(data) {
                    var response = data.response;
                    var result = data.result;
                    if (response == '1') {

                    }
                    else {
                      if (result == "1") {
                          _this.openGuide("buyvip");
                      }
                      else if (result == "2") {
                          _this.openGuide("both");
                      }
                    }
                },
                error: function() {
                    
                }
            });
           
        },
        openGuide:function(box) {
            var _this = this;
            _this.tips_dialog = OEUI.modal.dialog({
                el:'#'+box+'_guide',
                type: 'custom'
            });
            
        },
        
        //加载下一页聊天记录 
        clickGetPage:function() {
            var _this = this;
            $(document).on("click", "[f='but_get_msgchatpage']", function () {
                var page = $(this).attr("data-page");
                if (typeof(page) == 'undefined') {
                    page = 1;
                }
                else {
                    page = parseInt(page);
                }
                if (page > 1) {
                    $("[f='but_get_msgchatpage']").find("img").show();
                }
                _getMsgchatPageList(page);
            });
        },
        
        //表情处理
        clickEm:function() {
            var _this = this;
            //选择表情页数
            var dynBans = document.getElementById('ulWrap');
            var uls = $(dynBans).find('ul.fl').length * 680;
            $(document).on('mouseover', '[f="btn_amint"]', function () {
                var goid = $(this).index();
                $(this).addClass('on').siblings('li').removeClass('on');
                dynBans.style = `transition-duration:.5s;transform:translateX(-${680 * goid}px);width:${uls}px;`
            });
            //切换表情
            $(document).on("click", "[f='but_show_face']", function () {
                if ($("#face_box").css("display") == "block") {
                    $("#face_box").hide();
                }
                else {
                    $("#face_box").show();
                }
            });
            //选择表情
            $(document).on("click", "[f='but_select_face']", function () {
                var id = $(this).attr("data-id");
                var tag_em = "{emf_"+id+"}";
                var myField = document.getElementById("msgchat_content");
                if (document.selection) {
                    //myField.focus();
                    sel = document.selection.createRange();
                    sel.text = tag_em;
                    //myField.focus();
                }
                else if (myField.selectionStart || myField.selectionStart == '0') {
                    var startPos = myField.selectionStart;
                    var endPos = myField.selectionEnd;
                    var cursorPos = endPos;
                    myField.value = myField.value.substring(0, startPos)
                        + tag_em
                        + myField.value.substring(endPos, myField.value.length);
                    cursorPos += tag_em.length;
                    //myField.focus();
                    myField.selectionStart = cursorPos;
                    myField.selectionEnd = cursorPos;
                }
                else {
                    myField.value += tag_em;
                    //myField.focus();
                }
                $("#face_box").hide();

                if ($("#msgchat_content").val() != '') {
                    if (!$("#but_send_msgchat").hasClass("success")) {
                        $("#but_send_msgchat").addClass("success");
                        $("#but_send_msgchat").removeClass("gray");
                    }
                }
            });
            //内容框聚焦
            $(document).on("keydown", "#msgchat_content", function () {
                if ($(this).val() != '') {
                    if (!$("#but_send_msgchat").hasClass("success")) {
                        $("#but_send_msgchat").addClass("success");
                        $("#but_send_msgchat").removeClass("gray");
                    }
                }
                else {
                    if ($("#but_send_msgchat").hasClass("success")) {
                        $("#but_send_msgchat").removeClass("success");
                        $("#but_send_msgchat").addClass("gray");
                    }
                }
            });
            $(document).on("keyup", "#msgchat_content", function () {
                if ($(this).val() != '') {
                    if (!$("#but_send_msgchat").hasClass("success")) {
                        $("#but_send_msgchat").addClass("success");
                        $("#but_send_msgchat").removeClass("gray");
                    }
                }
                else {
                    if ($("#but_send_msgchat").hasClass("success")) {
                        $("#but_send_msgchat").removeClass("success");
                        $("#but_send_msgchat").addClass("gray");
                    }
                }
            });
        },
        
        //发送消息
        clickSend:function() {
            var _this = this;
            
            $(document).on("click", "[f='but_send_msgchat']", function () {
                _this.obj_id = $(this).attr('id');
                
                var sid = $(this).attr("data-sid");
                if (typeof(sid) == 'undefined') {
                    sid = $("#msgchat_sid").val();
                    if (typeof(sid) == 'undefined') {
                        sid = '';
                    }
                }
                var touid = $(this).attr("data-touid");
                var content = $("#msgchat_content").val();
                
                if (content != '') {

                    var loading = OEUI.loading({
                        el: '#'+_this.obj_id,
                        width: 30,
                        height: 30
                    });
                    loading.show();
                    $("#"+_this.obj_id).attr('f', '');

                    $.ajax({
                        type: "POST",
                        url: _ROOT_PATH+"index.php?m=user&c=msgchat",
                        cache: false,
                        data: {
                            a: "savewrite", touid: touid, content: content
                        },
                        dataType: "json",
                        success: function (data) {
                            var response = data.response;
                            var result = data.result;
                            
                            $("#"+_this.obj_id).attr('f', 'but_send_msgchat');
                            loading.remove();
                            $("#msgchat_content").val("");
                            if (response == '1') {
                                //发送成功
                                //载入最新发送记录
                                _getFinishSend(result);
                            }
                            else if (response == '2') {
                                //次数超额
                                _noChatTimesTips(sid);
                            }
                            else {
                                if (result == '') {
                                    result = '发送失败，请检查';
                                }
                                OEUI.message.msg({
                                    text: result
                                });
                            }
                        },
                        error: function () {
                            OEUI.message.msg({
                                text: '系统繁忙，请稍后再试'
                            });
                            $("#"+_this.obj_id).attr('f', 'but_send_msgchat');
                            loading.remove();
                        }
                    });
                }

            }); 
        },
        
        //解锁
        clickFreeSid:function() {
            var _this = this;
            
            
            $(document).on("click", "[f='but_freesid_tips']", function () {
                var sid = $(this).attr("data-sid");
                if (typeof(sid) == 'undefined') {
                    sid = $("#msgchat_sid").val();
                }
                var fee = $(this).attr("data-fee");
                var moneyname = $(this).attr("data-moneyname");
                if (typeof (fee) == "undefined") {
                    fee = 0;
                }
                var layer_lock = false;
                var layer_confirm = OEUI.modal.warning({
                    title: '温馨提示',
                    text: "你与对方的聊天通道还没打开<br />可以通过以下方式解锁通信<br />1、支付 <font color='red'>" + fee + "</font> " + moneyname + "解锁<br />2、升级VIP会员",
                    confirmText: '立即解锁',
                    cancelText: '升级VIP',
                    confirm: function () {
                        //点击解锁
                        if (!layer_lock) {
                            layer_lock = true;
                            _payFreeSid(sid);
                        }
                    },
                    cancel: function () {
                        //点击升级.
                        if (!layer_lock) {
                            layer_lock = true;
                            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);
                        }
                    }
                });

            });
            
            //解锁
            $(document).on("click", '[f="but_post_freelock"]', function () {
                var sid = $(this).attr("data-sid");
                _payFreeSid(sid);
            });
            $(document).on("click", '[f="but_post_buyvip"]', 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);
            });
        },
        
        //弹窗提示
        clickTips:function() {
            var _this = this;
            //次数提醒
            $(document).on("click", "[f='but_notimes_tips']", function () {
                var sid = $(this).attr("data-sid");
                if (typeof(sid) == 'undefined') {
                    sid = $("#msgchat_sid").val();
                }
                _noChatTimesTips(sid);
            });
            
            //关闭guide
            $(document).on("click", "[f='but_close_pupop']", function(){
                if (_this.tips_dialog) {
                    _this.tips_dialog.remove();
                }
            });
            
            //切换语音
            $(document).on("click", "[f='but_change_voice']", function(){
                _this.openGuide('wxvoice');
            });
              //关闭举报提示
              $(document).on('click', '[f="close_blank_tip"]', function() {
                  $(this).closest('.oe_blank_list').fadeOut(400);
              });
        }
    }

    window.OEChat = new OEMsgchat();

});

