var m_SystemQuickCache = [];                  //系统所有标签缓存

//用于注册系统快捷键的字典项 by liuyufei 20170527
window.send_shortcuts = 1713;               //发送快捷键key
window.autoreplay_shortcuts = {};           //快捷键回复快捷键字典
//获取快捷回复数据回执
function AsyncGetQuickTreeResult(jsonResult) {
    if (jsonResult.success) {
        m_SystemQuickCache = jsonResult.list;
        ChangeQuickTalkInput();
    } else {
        ShowError("myPageAlert", "获取快捷回复数据异常：" + jsonResult.error);
    }
    //$("#divSeachQuicktalk").hide();
}
//遍历快捷回复
function ChangeQuickTalkInput() {
    var objtxtQuickTalk = $("#txtQuickTalk");
    //清除之前设置的
    window.autoreplay_shortcuts = {};
    var strMyContent = "";//个人快捷回复
    var strTyContent = "";//企业下快捷回复··
    for (var i = 0; i < m_SystemQuickCache.length; i++) {
        var item = m_SystemQuickCache[i];
        var strContent = "";
        strContent += "<li>";
        var MySelfOrCpmpany = "";
        if (item.type == "1") {
        	MySelfOrCpmpany = "<span class=\"quick-tag open\">个人</span>";
        }
        strContent += "<a class=\"qt-tittle open\">" + item.qgname + MySelfOrCpmpany + "</a>";
        strContent += "<ul class=\"list-unstyled\">";
        var strSeachPoint = objtxtQuickTalk.val();
        var count = 0;
        for (var j = 0; j < item.quicks.length; j++) {
            var itemQ = item.quicks[j];
            if (itemQ.qname.indexOf(strSeachPoint) > -1 || itemQ.qcontent.indexOf(strSeachPoint) > -1) {
                count++;
                var hovercontent = itemQ.qcontent.length > 30 ? itemQ.qcontent.substring(0, 30) + "..." : itemQ.qcontent;
                strContent += "<li data-rel=\"popover\" data-trigger=\"hover\" data-placement=\"top\" data-content=\"" + hovercontent + "\" onclick=\"SetTalkContent('" + escape(itemQ.qcontent) + "');\">" + itemQ.qname + "</li>";

                //快捷键组合转为ascii码存入字典 by liuyufei 20170527
                if (itemQ.qkey1 == "ctrl") {
                    window.autoreplay_shortcuts["17" + itemQ.qkey2.charCodeAt()] = itemQ.qcontent;
                } else if (itemQ.qkey1 == "alt") {
                    window.autoreplay_shortcuts["18" + itemQ.qkey2.charCodeAt()] = itemQ.qcontent;
                }
            }
        }
        strContent += "</ul></li>";
        //组内没有快捷回复,清除本组html
        if (count == 0) {
            strContent = "";
        }

        if (item.type == "1") {
            strMyContent += strContent;

        }
        if (item.type == "0") {
            strTyContent += strContent;
        }
    }
    strMyContent += "<li class=\"add-quick-talk\">";
    strMyContent += "<a class=\"qt-tittle\" onclick=\"ShowMyQuickModel()\"><i class=\"icon iconfont icon-modify\"></i> 添加快捷回复</a>";
    strMyContent += "</li>";

    $("#divMyQuickReplay").html(strMyContent);
    $("#divQuickReply").html(strTyContent);
    $('#divMyQuickReplay li[data-rel=popover]').popover();
    $('#divQuickReply li[data-rel=popover]').popover();
    initTree();
    $(".quick-talk>ul>li>a").click(function () {
        $(this).toggleClass("open").next().animate({
            height: 'toggle'
        });
    })
}

////清空快捷回复查询条件
//function ClearSeachQuickTalk() {
//    $("#txtQuickTalk").val("");
//    ChangeQuickTalkInput();
//}

////检索快捷回复
//function ChangeQuickTalkInput() {
//    var objtxtQuickTalk = $("#txtQuickTalk");
//    objtxtQuickTalk.val($.trim(objtxtQuickTalk.val()));
//    if (objtxtQuickTalk.val().length == 0) {
//        $("#divSeachQuicktalk").hide();
//        $("#divQuickReply").show();
//        talkObj.queryquicktree();
//    } else {

//        var strContent = "";
//        var strSeachPoint = objtxtQuickTalk.val();
//        for (var i = 0; i < m_SystemQuickCache.length; i++) {
//            for (var j = 0; j < m_SystemQuickCache[i].quicks.length; j++) {
//                var itemQ = m_SystemQuickCache[i].quicks[j];
//                if (itemQ.qname.indexOf(strSeachPoint) > -1 || itemQ.qcontent.indexOf(strSeachPoint) > -1)
//                {
//                    strContent += "<a href=\"javascript:void(0);\" onclick=\"SetTalkContent('" + escape(itemQ.qcontent) + "');\">" + itemQ.qname + "</a>"
//                }
//            }
//        }
//        strContent = strContent.length == 0 ? "<a href=\"javascript:void(0);\">无匹配数据</a>" : strContent;
//        $("#liSearchQuickList").html(strContent);

//        $("#divSeachQuicktalk").show();
//        $("#divQuickReply").hide();
//    }
//}

function SetTalkContent(mark) {
    var unescapemark = "";
    if (mark != null && mark.length > 0) {
        unescapemark = unescape(mark).replace(/[\n]/ig, '<br/>');
    }

    if (m_EditorControl.getPlainTxt().length > 0) {
        m_EditorControl.setContent(m_EditorControl.getPlainTxt() + " " + unescapemark);
    } else {
        m_EditorControl.setContent(unescapemark);
    }

    $("#popquickreplyModel").modal("hide");
}



