
var m_QrGroupId = "";//快捷回复组ID
var m_QrId = "";//快捷回复ID
var m_JsonResult = [];//快捷回复缓存
var m_groupname = "";//储存当前选中快捷回复组的名称
var m_qname = "";//储存当前选中快捷回复的名称
var m_frist = 1;//初始化界面时，第一个标签组展开（1为初次加载页面）
var m_isGroupSubmit = true;//判断组是否提交过顺序，提交过true，否则为false
var isSelectIndex = -1;//移动组内回答时，当前所在的组的下标,用于设置改组展开状态
var m_Parentid = "";//当前选中的回复的ID，异动后赋值用
$(function () {
    $("#myPageAlert").hide();
    charout();
    manageObj.getquicktreelist();
});

//获取快捷回复树型菜单结果回调方法
function AsyncGetQuickTreeListResult(jsonTree) {
    if (jsonTree.success) {
        $("#divTree").html(ResultShow(jsonTree));
        if (isSelectIndex != -1) {
            $("#quick" + isSelectIndex).attr("style", "display: block;");
            $("#group" + isSelectIndex).removeClass().addClass("inactive-m inactives-m");
        }
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonTree.error);
    }
    $(".loadingcover").hide();
    $(".loading").hide();
    //判断复选框是否显示
    if ($(".BtnBachDelShow").attr("style") == "width: 180px;display:none;") {
        $(".BatchCheckbox").show();
    }
    PageLoadTrees();
    if (m_Parentid != "") {
        $("#selectGroup").val(m_Parentid);
    }
}

function ResultShow(jsonTree) {
    var objselectGroup = $("#selectGroup");
    objselectGroup.empty();//清空
    m_JsonResult = jsonTree;
    var strTreeContent = "";//树型菜单内容
    strTreeContent += " <div class=\"input-group\"><input id=\"txttag\"  name=\"txttag\" type=\"text\" class=\"form-control input-txt-l\" placeholder=\"内容\"><span class=\"input-group-btn\"><button onclick=\"ChangeReplyInput()\" class=\"btn btn-default\" type=\"button\" style=\"border-radius:0\"><i class=\"glyphicon glyphicon-search\"></i></button></span><span class=\"input-group-btn\"><button class=\"btn btn-default\"  onclick=\"resetTag()\" type=\"button\"><i class=\"glyphicon glyphicon-trash\"></i></button></span></div>";
    var strDeptCss = "";
    for (var i = 0; i < jsonTree.result.list.length; i++) {
        //当前没有字段sort的时候，给默认值，解决老版本兼容问题
        if (m_JsonResult.result.list[i].sort == "" || m_JsonResult.result.list[i].sort == undefined || m_JsonResult.result.list[i].sort == null) {
            m_isGroupSubmit = false;//没提交过是false
        } else {
            m_isGroupSubmit = true;//提交过是true
        }
        var jsonItem = jsonTree.result.list[i];
        if (m_frist == 1) {
            strDeptCss = i == 0 ? "inactive-m inactives-m" : "inactive-m";
        } else {
            strDeptCss = "inactive-m";
        }
        strTreeContent += "<ul class=\"yiji-m\">";
        strTreeContent += "<li>";

        var strGroupName = jsonItem.qgname.length > 8 ? jsonItem.qgname.substr(0, 7) + ".." : jsonItem.qgname;
        var strInput = jsonItem.quick.length > 0 ? "<input id=\"SelectAllQuick" + i + "\" onclick=\"SelectAllQuick(event," + i + ");\" class=\"BatchCheckbox\" style=\"display:none\" type=\"checkbox\" style=\"  vertical-align: middle; margin:0;\">" : "";
        strTreeContent += "<a id=\"group" + i + "\" href=\"#divEditDept\" class=\"" + strDeptCss + "\" data-toggle=\"tab\" onclick=\"SetEditGroupBind('" + jsonItem.qgid + "','" + jsonItem.qgname + "');\"><span class=\"pull-left\"> " + strInput + "" + strGroupName + "</span>";
        //判断组内标签能否向上向下移动
        if (i != 0 && i != m_JsonResult.result.list.length - 1) {
            //所选分组内既不是第一个，也不是最后一个（分组数大于1个）
            strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-up\" style=\"margin-right:5px;\" onclick=\"QuickGroupTop(" + i + ")\"></i>";
            strTreeContent += "<i class=\"fa fa-angle-down\" onclick=\"QuickGroupBottom(" + i + ")\"></i></div>";
        } else if (i == 0 && i != m_JsonResult.result.list.length - 1) {
            //所选分组内标签是当前分组的第一个标签是第一个（分组数大于1个）
            strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-down\" style=\"margin-top: 10px;\" onclick=\"QuickGroupBottom(" + i + ")\"></i></div>";
        } else if (i != 0 && i == m_JsonResult.result.list.length - 1) {
            //所选分组内标签是当前分组内的最后一个标签（分组数大于1个）
            strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-up\"  style=\"margin-top: 10px;\" onclick=\"QuickGroupTop(" + i + ")\"></i></div>";
        } else if (i == 0 && i == m_JsonResult.result.list.length - 1) {
            //所选分组内仅有一个标签
        }
        strTreeContent += "<span class=\"label pull-left badge-color\">" + jsonItem.quick.length + "</span>";
        strTreeContent += "</a>";
        if (m_frist == 1) {
            if (i == 0) {
                strTreeContent += "<ul id=\"quick" + i + "\" style=\"display: block;\">";
            } else {
                strTreeContent += "<ul id=\"quick" + i + "\" style=\"display: none;\">";
            }
            m_frist = 2;
        } else {
            strTreeContent += "<ul id=\"quick" + i + "\" style=\"display: none;\">";
        }


        for (var j = 0; j < jsonItem.quick.length; j++) {
            var tagItem = jsonItem.quick[j];
            var checked = "";
            for (var k = 0; k < m_SelectQuick.length; k++) {
                if (m_SelectQuick[k] == tagItem.qid) {
                    checked = "checked";
                    break;
                }
            }
            //截取快捷回复名称长度
            var strTagName = tagItem.qname.length > 8 ? tagItem.qname.substr(0, 7) + ".." : tagItem.qname;
            strTreeContent += "<li><a href=\"#divEditPerson\" data-toggle=\"tab\" onclick=\"SetEditTagBind('" + tagItem.qid + "','" + tagItem.qgid + "','" + tagItem.qname + "','" + tagItem.qkey1 + "','" + tagItem.qkey2 + "','" + encodeURI(tagItem.qcontent) + "','" + jsonItem.qgname + "')\"><span class=\"pull-left\"><input " + checked + " qid='" + tagItem.qid + "' onclick=\"OneSelectQuick(event,'" + tagItem.qid + "'," + j + "," + i + ");\" id='OneSelectQuick" + j + "' class=\"BatchCheckbox BatchCheckbox" + i + "\"  style=\"display:none\"  type=\"checkbox\" style=\"  vertical-align: middle; margin:0;\"></span>" + strTagName;
            //判断组内标签能否向上向下移动
            if (j != 0 && j != jsonItem.quick.length - 1) {
                //所选分组内既不是第一个，也不是最后一个（分组数大于1个）
                strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-up\" style=\"margin-right:5px;\" onclick=\"QuickTop(" + i + "," + j + ")\"></i>";
                strTreeContent += "<i class=\"fa fa-angle-down\" onclick=\"QuickBottom(" + i + "," + j + ")\"></i></div>";
            } else if (j == 0 && j != jsonItem.quick.length - 1) {
                //所选分组内标签是当前分组的第一个标签是第一个（分组数大于1个）
                strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-down\" style=\"margin-top: 10px;\" onclick=\"QuickBottom(" + i + "," + j + ")\"></i></div>";
            } else if (j != 0 && j == jsonItem.quick.length - 1) {
                //所选分组内标签是当前分组内的最后一个标签（分组数大于1个）
                strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-up\"  style=\"margin-top: 10px;\" onclick=\"QuickTop(" + i + "," + j + ")\"></i></div>";
            } else if (j == 0 && j == jsonItem.quick.length - 1) {
                //所选分组内仅有一个标签
            }
            strTreeContent += "</a></li>";
        }
        strTreeContent += "</ul>";
        strTreeContent += "</li>";
        strTreeContent += "</ul>";

        //DDL标签组
        objselectGroup.append("<option value='" + jsonItem.qgid + "'>" + jsonItem.qgname + "</option>");
    }
    return strTreeContent;
}

//树形菜单高度设置
function PageLoadTrees() {
    $(".left-list-l").css({
        height: $(window).height() - 40
    });

    $("#right").css({
        height: $(window).height() - 40
    });
    $("#left").css({
        height: $(window).height() - 40
    });
    $(".talklist-m").css({
        height: $(window).height() - 100
    });
    initTree();
}

//选中组时
function SetEditGroupBind(value, name, type) {
    m_QrGroupId = value;//标签组ID
    m_groupname = name;
    if (m_QrGroupId.length > 0) {
        $("#BtnDleteQuickGroup").show();
    } else {
        $("#BtnDleteQuickGroup").hide();
    }

    $("#txtGroupName").val(name);
    $("#spDeleteGroupName").html(name);

    $("#txtGroupName").focus();
    //if (type == "0") {
    //    $("#cbxGroupIsAll").prop("checked", true);
    //} else {
    //    $("#cbxGroupIsAll").prop("checked", false);
    //}

    $("#divEditGroup").removeClass("tab-pane fade").addClass("tab-pane fade active in");//显示
    $("#divEditTag").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏\
    $("#liCreatQuickTalkGroup").removeClass("active");
    $("#liCreatQuickTalk").removeClass("active");
}

//添加、修改组
function EditGroup() {
    var objtxtGroupName = $("#txtGroupName");
    //var strIsAll = $("#cbxGroupIsAll").is(":checked") ? "0" : "";//0全局，空个人
    var strIsAll = "0";//0全局，空个人

    objtxtGroupName.val($.trim(objtxtGroupName.val().replace(/["]/g, '').replace(/[']/g, '')));

    if ($.trim(objtxtGroupName.val()).length == 0) {
        ShowError("myPageAlert", "组名称不能为空！");
        objtxtGroupName.focus();
        return;
    }

    var jsonResult;
    if (m_QrGroupId.length == 0) {
        var number = 10001;
        if (m_JsonResult.result.list.length > 0) {
            //添加查重
            number = m_isGroupSubmit ? Number(m_JsonResult.result.list[m_JsonResult.result.list.length - 1].sort) + 1 : "";
            for (var i = 0; i < m_JsonResult.result.list.length; i++) {
                if ($.trim($("#txtGroupName").val()) == m_JsonResult.result.list[i].qgname) {
                    ShowError("myPageAlert", "该快捷回复组已存在！");
                    return;
                }
            }
        }
        var strParaJson = $.toJSON({
            qgname: objtxtGroupName.val(),
            sort: number
        });
        manageObj.addquickreplygroup(strParaJson);
    }
    else {
        //修改
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            if ($.trim($("#txtGroupName").val()) == m_JsonResult.result.list[i].qgname && $.trim($("#txtGroupName").val()) != m_groupname) {
                ShowError("myPageAlert", "该快捷回复组已存在！");
                return;
            }
        }
        var strParaJson = $.toJSON({
            qgid: m_QrGroupId, qgname: objtxtGroupName.val(),
        });
        manageObj.updatequickreplygroup(strParaJson);
    }

    $(".loading").show();
    $(".loadingcover").show();
}


//显示删除组对话框
function ShowDeleteGroup() {
    if (m_QrGroupId.length == 0) {
        return;
    }
    for (var i = 0; i < m_JsonResult.result.list.length; i++) {
        if (m_JsonResult.result.list[i].qgid == m_QrGroupId) {
            if (m_JsonResult.result.list[i].quick.length > 0) {
                ShowError("myPageAlert", "此组下存在快捷回复，不能进行删除操作!");
                return;
            }
        }
    }
    $("#DeleteGroupModal").modal("show");
}

//提交删除组
function CommitGroupDelete() {
    $(".loading").show();
    $(".loadingcover").show();
    var strParaJson = $.toJSON({
        qgid: m_QrGroupId,
    });
    manageObj.deletequickreplygroup(strParaJson);
}

//删除快捷回复组回调
function AsyncDeleteQuickReplyGroupResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtGroupName").val("");
        $("#cbxIsAll").attr("checked", false);

        $("#DeleteGroupModal").modal("hide");
        $("#divEditGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏

        ShowMsg("myPageAlert", "操作成功！");

        manageObj.getquicktreelist();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}

//变更快捷回复组结果回调方法
function AsyncAddQuickReplyGroupResult(jsonResult) {

    if (jsonResult.success) {
        $("#txtGroupName").val("");
        $("#cbxIsAll").attr("checked", false);

        $("#DeleteGroupModal").modal("hide");
        $("#divEditGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏

        ShowMsg("myPageAlert", "操作成功！");

        manageObj.getquicktreelist();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}
//添加快捷回复回调
function AsyncUpdateQuickReplyGroupResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtGroupName").val("");
        $("#cbxIsAll").attr("checked", false);

        $("#DeleteGroupModal").modal("hide");
        $("#divEditGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏

        ShowMsg("myPageAlert", "操作成功！");

        manageObj.getquicktreelist();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}

//选中快捷回复时
function SetEditTagBind(qrid, parentid, name, qkey1, qkey2, qrcontent, zname) {
    m_Parentid = parentid;
    m_QrId = qrid;//ID
    m_qname = name;//标签
    m_groupname = zname;//标签组
    var content = decodeURIComponent(qrcontent);
    if (m_QrId.length > 0) {
        $("#DeleteQuick").show();

        $("#spDeleteTagName").html(name);

        $("#txtTagName").val(name);
        $("#selectGroup").val(parentid);
        $("#txtTagRemark").val(content);
        $("#selectQKey1").val(qkey1);
        $("#selectQKey2").val(qkey2);
    } else {
        $("#DeleteQuick").hide();
        $("#txtTagName").val("");
        $('#selectGroup option:eq(0)').attr('selected', 'selected');
        $("#txtTagRemark").val("");
        $("#selectQKey1").val(qkey1);
        $("#selectQKey2").val(qkey2);
    }

    $("#txtTagName").focus();

    $("#divEditTag").removeClass("tab-pane fade").addClass("tab-pane fade active in");//显示
    $("#divEditGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
    $("#liCreatQuickTalkGroup").removeClass("active");
    $("#liCreatQuickTalk").removeClass("active");
}

//添加快捷回复
function EditTag() {
    var objtxtTagName = $("#txtTagName");
    var objselectGroup = $("#selectGroup");
    var objtxtTagRemark = $("#txtTagRemark");
    var objselectQKey1 = $("#selectQKey1");
    var objselectQKey2 = $("#selectQKey2");

    objtxtTagName.val($.trim(objtxtTagName.val().replace(/["]/g, '').replace(/[']/g, '')));
    objselectGroup.val($.trim(objselectGroup.val()));
    objtxtTagRemark.val($.trim(objtxtTagRemark.val().replace(/["]/g, '').replace(/[']/g, '')));
    objselectQKey1.val($.trim(objselectQKey1.val()));
    objselectQKey2.val($.trim(objselectQKey2.val()));

    if (m_JsonResult.result.list.length == 0) {
        ShowError("myPageAlert", "请先创建快捷回复组!");
        return;
    }
    if (objtxtTagName.val().length == 0) {
        ShowError("myPageAlert", "名称不能为空！");
        objtxtTagName.focus();
        return;
    }
    if (objselectGroup.val() == null) {
        ShowError("myPageAlert", "未选择所属分组,若没有请先创建组！");
        objselectGroup.focus();
        return;
    }
    if (objselectGroup.val().length == 0) {
        ShowError("myPageAlert", "未选择所属分组,若没有请先创建组！");
        objselectGroup.focus();
        return;
    }
    if (objtxtTagRemark.val().length == 0) {
        ShowError("myPageAlert", "回复内容不能为空！");
        objtxtTagRemark.focus();
        return;
    }
    if (objtxtTagRemark.val().length == 0) {
        ShowError("myPageAlert", "回复内容不能为空！");
        objtxtTagRemark.focus();
        return;
    }

    var jsonResult;


    var number = "";//当前组内标签的sort值
    for (var i = 0; i < m_JsonResult.result.list.length; i++) {
        if (m_JsonResult.result.list[i].qgid == objselectGroup.val()) {
            for (var j = 0; j < m_JsonResult.result.list[i].quick.length; j++) {
                if (m_JsonResult.result.list[i].quick[j].sort != "" && m_JsonResult.result.list[i].quick[j].sort != undefined && m_JsonResult.result.list[i].quick[j].sort != null) {
                    number = Number(m_JsonResult.result.list[i].quick[m_JsonResult.result.list[i].quick.length - 1].sort) + 1;
                    break;
                }
            }
            break;
        }
    }

    if (m_QrId.length == 0) {
        //添加
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            var qlist = m_JsonResult.result.list[i];
            if (qlist.qgid == $.trim($("#selectGroup").val())) {
                for (var j = 0; j < qlist.quick.length; j++) {
                    if ($.trim($("#txtTagName").val()) == qlist.quick[j].qname) {
                        ShowError("myPageAlert", "该快捷回复名称已存在！");
                        return;
                    }
                }
            }
        }
        $(".loading").show();
        $(".loadingcover").show();
        var strParaJson = $.toJSON({
            qgid: objselectGroup.val(), qname: objtxtTagName.val(),
            qkey1: objselectQKey1.val(), qkey2: objselectQKey2.val(),
            qcontent: objtxtTagRemark.val().replace(/\n/g, '\r\n'),
            sort: number
        });
        manageObj.addquickreply(strParaJson);
    }
    else {
        //修改
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            var qlist = m_JsonResult.result.list[i];
            if ($("#selectGroup option:selected").text() == m_groupname && m_groupname == qlist.qgname) {
                for (var j = 0; j < qlist.quick.length; j++) {
                    if ($.trim($("#txtTagName").val()) == qlist.quick[j].qname && $.trim($("#txtTagName").val()) != m_qname) {
                        ShowError("myPageAlert", "该快捷回复名称已存在！");
                        return;
                    }
                }
            } else if ($("#selectGroup option:selected").text() == qlist.qgname && $("#selectGroup option:selected").text() != m_groupname) {
                for (var j = 0; j < qlist.quick.length; j++) {
                    if ($.trim($("#txtTagName").val()) == qlist.quick[j].qname) {
                        ShowError("myPageAlert", "该快捷回复名称已存在！");
                        return;
                    }
                }
            }
        }
        $(".loading").show();
        $(".loadingcover").show();
        var strParaJson = $.toJSON({
            qid: m_QrId,
            qgid: objselectGroup.val(), qname: objtxtTagName.val(),
            qkey1: objselectQKey1.val(), qkey2: objselectQKey2.val(),
            qcontent: objtxtTagRemark.val().replace(/\n/g, '\r\n'),
            sort: number
        });
        manageObj.updatequickreply(strParaJson);
    }
}
//输出快捷键2
function charout() {
    $("#selectQKey2").html("");
    var vhtml = "<option   value=\"\">----------</option>";
    var arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
    if ($("#selectQKey1").val() == "ctrl") {
        arr = ["B", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "T", "U"];
    }

    for (var i = 0; i < arr.length; i++) {
        vhtml += "<option ";
        vhtml += " value=\"";
        vhtml += arr[i];
        vhtml += "\"";
        vhtml += ">";
        vhtml += arr[i];
        vhtml += "</option>";
    }
    $("#selectQKey2").html(vhtml);
}

//显示删除快捷回复对话框
function ShowDeleteTag() {
    if (m_QrId.length == 0) {
        return;
    }
    $("#DeleteTagModal").modal("show");
}

//提交删除部门
function CommitTagDelete() {
    $(".loading").show();
    $(".loadingcover").show();
    var strParaJson = $.toJSON({
        qid: m_QrId,
    });
    manageObj.deletequickreply(strParaJson);
}
//变更快捷回复结果回调方法
function AsyncUpdateQuickReplyResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtTagName").val("");
        $("#selectQKey1").val("");
        $("#selectQKey2").val("");
        $("#txtTagRemark").val("");
        $('#selectGroup option:eq(0)').attr('selected', 'selected');

        ShowMsg("myPageAlert", "操作成功！");
        $("#divEditTag").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
        manageObj.getquicktreelist();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}
//删除快捷回复回调
function AsyncDeleteQuickReplyResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtTagName").val("");
        $("#selectQKey1").val("");
        $("#selectQKey2").val("");
        $("#txtTagRemark").val("");
        $('#selectGroup option:eq(0)').attr('selected', 'selected');

        $("#DeleteTagModal").modal("hide");
        $("#divEditTag").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏

        ShowMsg("myPageAlert", "操作成功！");

        manageObj.getquicktreelist();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}
//添加快捷回复回调
function AsyncAddQuickReplyResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtTagName").val("");
        $("#selectQKey1").val("");
        $("#selectQKey2").val("");
        $("#txtTagRemark").val("");
        $('#selectGroup option:eq(0)').attr('selected', 'selected');

        $("#DeleteTagModal").modal("hide");
        $("#divEditTag").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏

        ShowMsg("myPageAlert", "操作成功！");

        manageObj.getquicktreelist();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}

//查询
function ChangeReplyInput() {
    var objselectGroup = $("#selectGroup");
    objselectGroup.empty();//清空
    var replaycondition = $.trim($("#txttag").val());
    if (replaycondition.length > 0) {
        var strTreeContent = "";
        strTreeContent += "<div class=\"input-group\"><input id=\"txttag\"  name=\"txttag\" type=\"text\" value=\"" + replaycondition + "\" class=\"form-control input-txt-l\" placeholder=\"名称\\内容\"><span class=\"input-group-btn\"><button onclick=\"ChangeReplyInput()\" class=\"btn btn-default\" type=\"button\" style=\"border-radius:0\"><i class=\"glyphicon glyphicon-search\"></i></button></span><span class=\"input-group-btn\"><button class=\"btn btn-default\"  onclick=\"resetTag()\" type=\"button\"><i class=\"glyphicon glyphicon-trash\"></i></button></span></div>";

        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            var groupname = "";

            var reaplay = m_JsonResult.result.list[i];
            if (reaplay != null) {

                for (var k = 0; k < reaplay.quick.length; k++) {
                    var itemTagName = reaplay.quick[k].qname;
                    var itemcontent = reaplay.quick[k].qcontent;
                    var addnum = 0;

                    if (itemTagName.indexOf(replaycondition) != -1 || itemcontent.indexOf(replaycondition) != -1) {
                        addnum++;

                        if (groupname != reaplay.qgname) {
                            strTreeContent += "<ul class=\"yiji-m\">";
                            strTreeContent += "<li>";
                            var qgnamesub = reaplay.qgname.length > 7 ? reaplay.qgname.substring(0, 7) + ".." : reaplay.qgname;
                            strTreeContent += "<a href=\"#divEditDept\" class=\"inactive-m\" data-toggle=\"tab\" onclick=\"SetEditGroupBind('" + reaplay.qgid + "','" + reaplay.qgname + "');\">" + qgnamesub + "<span class=\"label label-info pull-right badge-color\"></span></a>";
                            strTreeContent += "<ul style=\"display: none;\">";
                            groupname = reaplay.qgname;
                        }

                        var tagItem = reaplay.quick[k];
                        var isChecked = "";
                        for (var j = 0; j < m_SelectQuick.length; j++) {
                            if (m_SelectQuick[j] == tagItem.qid) {
                                isChecked = "checked";
                                break;
                            }
                        }
                        strTreeContent += "<li><a href=\"#divEditPerson\" data-toggle=\"tab\" onclick=\"SetEditTagBind('" + tagItem.qid + "','" + tagItem.qgid + "','" + tagItem.qname + "','" + tagItem.qkey1 + "','" + tagItem.qkey2 + "','" + encodeURI(tagItem.qcontent) + "','" + reaplay.qgname + "')\">" + tagItem.qname + "<span class=\"pull-left\"><input " + isChecked + " qid='" + tagItem.qid + "' onclick=\"OneSelectQuick(event,'" + tagItem.qid + "'," + k + "," + i + ");\" id='OneSelectQuick" + k + "' class=\"BatchCheckbox BatchCheckbox" + i + "\" style=\"display:none\" type=\"checkbox\" style=\"  vertical-align: middle; margin:0;\"></span></a></li>";
                    }
                }
                strTreeContent += "</ul>";
                strTreeContent += "</li>";
                strTreeContent += "</ul>";
                objselectGroup.append("<option value='" + reaplay.qgid + "'>" + reaplay.qgname + "</option>");
            }

        }

    }
    else {
        strTreeContent = ResultShow(m_JsonResult);
    }
    $("#divTree").html(strTreeContent);
    //判断复选框是否显示
    if ($(".BtnBachDelShow").attr("style") == "width: 180px;display:none;") {
        $(".BatchCheckbox").show();
    }
    PageLoadTrees();
}
//导入excle
function ImportExcle() {
    manageObj.importexcle();
}
//导入快捷回复回调
function AsyncImportExcleResult(jsonResult) {
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "操作成功！");
        manageObj.getquicktreelist();
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}
function ShowTemplateImportExcle() {
    $("#showtemplateimportexcle").modal("show");
}
//模版快捷回复导入
function TemplateImportExcle() {
    manageObj.companytemplatequickexport();
}

function AsyncCompanyTemplateQuickExportResult(jsonResult) {
    if (jsonResult.success) {
        $("#showtemplateimportexcle").modal("hide");
        ShowMsg("myPageAlert", "操作成功！");
        manageObj.getquicktreelist();
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}

//清空按钮
function resetTag() {
    manageObj.getquicktreelist();
    $(".loading").show();
    $(".loadingcover").show();
}
$(window).resize(function () {
    PageLoadTrees();
});

function key1Change() {
    charout();
}

///----------------------分组向上向下移动-------------------start
//向上移动  m_JsonResult.result.list
function QuickGroupTop(index) {
    event.cancelBubble = true;//禁止触发父级事件
    $(".loadingcover").show();
    $(".loading").show();
    isSelectIndex = -1;
    //没提交过赋值
    if (!m_isGroupSubmit) {
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            m_JsonResult.result.list[i].sort = 10000 + Number(i) + 1;
        }
    }
    var top = m_JsonResult.result.list[index].sort;
    m_JsonResult.result.list[index].sort = m_JsonResult.result.list[index - 1].sort;
    m_JsonResult.result.list[index - 1].sort = top;

    var list = [];
    var jObject = {};
    if (m_isGroupSubmit) {
        jObject.id = m_JsonResult.result.list[index - 1].qgid;
        jObject.sort = m_JsonResult.result.list[index - 1].sort;
        list.push(jObject);
        jObject = {};
        jObject.id = m_JsonResult.result.list[index].qgid;
        jObject.sort = m_JsonResult.result.list[index].sort;
        list.push(jObject);
    } else {
        //没提交过
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            jObject = {};
            jObject.id = m_JsonResult.result.list[i].qgid;
            jObject.sort = m_JsonResult.result.list[i].sort;
            list.push(jObject);
        }
    }
    var json = $.toJSON({
        qglist: list
    });
    manageObj.changequickgroupposition(json);
}
function QuickGroupBottom(index) {
    event.cancelBubble = true;//禁止触发父级事件
    $(".loadingcover").show();
    $(".loading").show();
    isSelectIndex = -1;
    if (!m_isGroupSubmit) {
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            m_JsonResult.result.list[i].sort = 10000 + i + 1;
        }
    }
    var bottom = m_JsonResult.result.list[index].sort;
    m_JsonResult.result.list[index].sort = m_JsonResult.result.list[index + 1].sort;
    m_JsonResult.result.list[index + 1].sort = bottom;
    var list = [];
    var jObject = {};
    if (m_isGroupSubmit) {
        jObject.id = m_JsonResult.result.list[index].qgid;
        jObject.sort = m_JsonResult.result.list[index].sort;
        list.push(jObject);
        jObject = {};
        jObject.id = m_JsonResult.result.list[index + 1].qgid;
        jObject.sort = m_JsonResult.result.list[index + 1].sort;
        list.push(jObject);
    } else {
        //没提交过
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            jObject = {};
            jObject.id = m_JsonResult.result.list[i].qgid;
            jObject.sort = m_JsonResult.result.list[i].sort;
            list.push(jObject);
        }
    }
    var json = $.toJSON({
        qglist: list
    });
    manageObj.changequickgroupposition(json);
}

//组上下移动回调
function AsyncChangeQuickGroupPositionResult(jsonResult) {
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "操作成功！");
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
        $(".loadingcover").hide();
        $(".loading").hide();
    }
    manageObj.getquicktreelist();
}
///----------------------分组向上向下移动-------------------end

///---------------------分组内标签向上向下移动--------------start
//分组内标签向上移动单击
function QuickTop(GroupIndex, TagIndex) {
    //参数：GroupIndex是所选标签组的下标，TagIndex是所选标签的下标
    event.cancelBubble = true;//禁止触发父级事件
    $(".loadingcover").show();
    $(".loading").show();
    isSelectIndex = GroupIndex;
    var isQuick = true;//true提交过当前组，false没提交过
    for (var i = 0; i < m_JsonResult.result.list[GroupIndex].quick.length; i++) {
        if (m_JsonResult.result.list[GroupIndex].quick[i].sort == "" || m_JsonResult.result.list[GroupIndex].quick[i].sort == undefined || m_JsonResult.result.list[GroupIndex].quick[i].sort == null) {
            isQuick = false;
            break;
        }
    }
    if (!isQuick) {
        for (var i = 0; i < m_JsonResult.result.list[GroupIndex].quick.length; i++) {
            m_JsonResult.result.list[GroupIndex].quick[i].sort = 10000 + i + 1;
        }
    }
    var top = m_JsonResult.result.list[GroupIndex].quick[TagIndex].sort
    m_JsonResult.result.list[GroupIndex].quick[TagIndex].sort = m_JsonResult.result.list[GroupIndex].quick[TagIndex - 1].sort;
    m_JsonResult.result.list[GroupIndex].quick[TagIndex - 1].sort = top;

    var list = [];
    var jObject = {};
    if (!isQuick) {
        for (var j = 0; j < m_JsonResult.result.list[GroupIndex].quick.length; j++) {
            jObject = {};
            jObject.id = m_JsonResult.result.list[GroupIndex].quick[j].qid;
            jObject.sort = m_JsonResult.result.list[GroupIndex].quick[j].sort;
            list.push(jObject);
        }
    } else {
        jObject.id = m_JsonResult.result.list[GroupIndex].quick[TagIndex - 1].qid;
        jObject.sort = m_JsonResult.result.list[GroupIndex].quick[TagIndex - 1].sort;
        list.push(jObject);
        jObject = {};
        jObject.id = m_JsonResult.result.list[GroupIndex].quick[TagIndex].qid;
        jObject.sort = m_JsonResult.result.list[GroupIndex].quick[TagIndex].sort;
        list.push(jObject);

    }
    var json = $.toJSON({
        qlist: list
    });
    manageObj.changequickposition(json);
}

//分组内标签向下移动单击事件
function QuickBottom(GroupIndex, TagIndex) {
    //参数：GroupIndex是所选标签组的下标，TagIndex是所选标签的下标
    event.cancelBubble = true;//禁止触发父级事件
    $(".loadingcover").show();
    $(".loading").show();
    isSelectIndex = GroupIndex;
    var isQuick = true;//true提交过当前组，false没提交过
    for (var i = 0; i < m_JsonResult.result.list[GroupIndex].quick.length; i++) {
        if (m_JsonResult.result.list[GroupIndex].quick[i].sort == "" || m_JsonResult.result.list[GroupIndex].quick[i].sort == undefined || m_JsonResult.result.list[GroupIndex].quick[i].sort == null) {
            isQuick = false;
            break;
        }
    }
    if (!isQuick) {
        for (var i = 0; i < m_JsonResult.result.list[GroupIndex].quick.length; i++) {
            m_JsonResult.result.list[GroupIndex].quick[i].sort = 10000 + i + 1;
        }
    }
    var bottom = m_JsonResult.result.list[GroupIndex].quick[TagIndex].sort;
    m_JsonResult.result.list[GroupIndex].quick[TagIndex].sort = m_JsonResult.result.list[GroupIndex].quick[TagIndex + 1].sort;
    m_JsonResult.result.list[GroupIndex].quick[TagIndex + 1].sort = bottom;

    var list = [];
    var jObject = {};
    if (!isQuick) {
        for (var j = 0; j < m_JsonResult.result.list[GroupIndex].quick.length; j++) {
            jObject = {};
            jObject.id = m_JsonResult.result.list[GroupIndex].quick[j].qid;
            jObject.sort = m_JsonResult.result.list[GroupIndex].quick[j].sort;
            list.push(jObject);
        }
    } else {
        jObject.id = m_JsonResult.result.list[GroupIndex].quick[TagIndex + 1].qid;
        jObject.sort = m_JsonResult.result.list[GroupIndex].quick[TagIndex + 1].sort;
        list.push(jObject);
        jObject = {};
        jObject.id = m_JsonResult.result.list[GroupIndex].quick[TagIndex].qid;
        jObject.sort = m_JsonResult.result.list[GroupIndex].quick[TagIndex].sort;
        list.push(jObject);

    }
    var json = $.toJSON({
        qlist: list
    });
    manageObj.changequickposition(json);
}

//组内回复上下移动回调
function AsyncChangeQuickPositionResult(jsonResult) {
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "操作成功！");
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
        $(".loadingcover").hide();
        $(".loading").hide();
    }
    manageObj.getquicktreelist();
}
///---------------------分组内标签向上向下移动--------------end

//---------------------------------批量删除标签--------------------
var m_SelectQuick = [];//批量删除选中的快捷回复集合
//显示批量删除按钮 多选框
function BtnBachDelShow() {
    $(".BtnBachDelShow").attr("style", "width: 180px;display:none;");
    $("#BtnBachDel").show();
    $(".BatchCheckbox").show();
}

//取消按钮，隐藏批量删除按钮 多选框
function BtnBachDelHide() {
    m_SelectQuick = [];//清空缓存
    $(".BtnBachDelShow").attr("style", "width: 180px;display:block;");
    $("#BtnBachDel").hide();
    $(".BatchCheckbox").hide();
}

//组内全选全取消
function SelectAllQuick(event, index) {
    event.stopPropagation();
    //全选
    if ($("#SelectAllQuick" + index).is(":checked")) {
        if (m_SelectQuick.length > 500) {
            ShowError("myPageAlert", "一次最多选择500个");
            return;
        }
        var SelectAllQuick = $("#divTree").find(".BatchCheckbox" + index);
        SelectAllQuick.each(function (i) {
            var isSelect = true;
            for (var i = 0; i < m_SelectQuick.length; i++) {
                if (m_SelectQuick[i] == $(this).attr("qid")) {
                    isSelect = false;
                    break;
                }
            }
            if (isSelect) {
                m_SelectQuick.push($(this).attr("qid"));
            }
        });
        $(".BatchCheckbox" + index).prop("checked", true);
    }
        //全取消
    else {
        var SelectAllQuick = $("#divTree").find(".BatchCheckbox" + index);
        SelectAllQuick.each(function (i) {
            for (var i = 0; i < m_SelectQuick.length; i++) {
                if (m_SelectQuick[i] == $(this).attr("qid")) {
                    m_SelectQuick.splice(i, 1);
                    break;
                }
            }
        });
        $(".BatchCheckbox" + index).prop("checked", false);
    }
}

//选中取消单个快捷回复
function OneSelectQuick(event, qid, index, gindex) {
    event.stopPropagation();//阻止事件默认行为
    if ($("#OneSelectQuick" + index).is(":checked")) {
        //添加
        if (m_SelectQuick.length > 500) {
            ShowError("myPageAlert", "快捷回复批量删除最多为500个！");
            return;
        }
        m_SelectQuick.push(qid);
    } else {
        //删除
        for (var i = 0; i < m_SelectQuick.length; i++) {
            if (m_SelectQuick[i] == qid) {
                m_SelectQuick.splice(i, 1);
                break;
            }
        }
    }
}

//确认删除按钮
function SubmitDelete() {
    if (m_SelectQuick.length == 0) {
        ShowError("myPageAlert", "请选择需要删除的快捷回复");
        return;
    }
    if (m_SelectQuick.length > 500) {
        ShowError("myPageAlert", "快捷回复批量删除最多为500个！");
        return;
    }
    var strQids = "";
    for (var i = 0; i < m_SelectQuick.length; i++) {
        strQids += m_SelectQuick[i] + ",";
    }
    strQids = strQids.substr(0, strQids.length - 1);
    var json = $.toJSON({
        "qids": strQids
    });
    $(".loading").show();
    $(".loadingcover").show();
    manageObj.batchdeletequick(json);
}
//批量删除快捷回复回调
function AsyncBatchDeleteQuickResult(jsonResult) {
    if (jsonResult.success) {
        m_SelectQuick = [];
        ShowMsg("myPageAlert", "操作成功！");
        manageObj.getquicktreelist();
    } else {
        ShowError("myPageAlert", "操作失败!" + jsonResult.error);
    }
    $(".loading").hide();
    $(".loadingcover").hide();
}