
var m_QrGroupId = "";//快捷回复组ID
var m_QrId = "";//快捷回复ID
var m_JsonResult = [];//快捷回复缓存
var m_qgname = "";//储存当前选中快捷回复组的名称

function ShowMyQuickModel() {
    BindMyQuickList();
    charout();
    $("#MyQuickModel").modal("show");
    BtnBachDelHide();
  
}

function BindMyQuickList() {
    var qList = $.parseJSON(talkObj.getmyquicklist());
    $("#divMyQuickTree").html(ResultShow(qList));
    PageLoadTrees();
}

function ResultShow(jsonTree) {
    var objselectMyQuickGroup = $("#selectMyQuickGroup");
    objselectMyQuickGroup.empty();//清空
    m_JsonResult = jsonTree;
    var strTreeContent = "";//树型菜单内容
    for (var i = 0; i < jsonTree.length; i++) {
        var jsonItem = jsonTree[i];
        if (jsonItem.qgname == undefined) {
            continue;
        }
        strTreeContent += "<ul class=\"yiji-m\">";
        strTreeContent += "<li>";
        var qgname = jsonItem.qgname.length > 5 ? jsonItem.qgname.substring(0, 5) + ".." : jsonItem.qgname;
        var strInput = jsonItem.quicks.length > 0 ? "<input id=\"SelectAllQuick" + i + "\" onclick=\"SelectAllQuick(event," + i + ");\" class=\"BatchCheckbox\" style=\"display:none\" type=\"checkbox\" style=\"  vertical-align: middle; margin:0;\">" : "";
        if (i == 0) {
            strTreeContent += "<a href=\"#divEditDept\" class=\"inactive-m inactives-m\" data-toggle=\"tab\" onclick=\"SetEditQuickGroupBind('" + jsonItem.qgid + "','" + jsonItem.qgname + "');\">" + strInput + qgname + "<span class=\"label pull-right badge-color\" style=\"margin-right: 10px\">" + jsonItem.quicks.length + "</span></a>";
            strTreeContent += "<ul style=\"display: block;\">";
        } else {
            strTreeContent += "<a href=\"#divEditDept\" class=\"inactive-m\" data-toggle=\"tab\" onclick=\"SetEditQuickGroupBind('" + jsonItem.qgid + "','" + jsonItem.qgname + "');\">" + strInput + qgname + "<span class=\"label pull-right badge-color\" style=\"margin-right: 10px\">" + jsonItem.quicks.length + "</span></a>";
            strTreeContent += "<ul style=\"display: none;\">";
        }
        for (var j = 0; j < jsonItem.quicks.length; j++) {
            var tagItem = jsonItem.quicks[j];
            var checked = "";
            //for (var k = 0; k < m_SelectQuick.length; k++) {
            //    if (m_SelectQuick[k] == tagItem.qid) {
            //        checked = "checked";
            //        break;
            //    }
            //}
            strTreeContent += "<li><a href=\"#divEditPerson\" data-toggle=\"tab\" onclick=\"SetEditQuickBind('" + tagItem.qid + "','" + tagItem.qgid + "','" + tagItem.qname + "','" + tagItem.qkey1 + "','" + tagItem.qkey2 + "','" + escape(tagItem.qcontent) + "','" + jsonItem.qgid + "')\"><span class=\"pull-left\"><input " + checked + " id='" + tagItem.qid + "' onclick=\"OneSelectQuick(event,'" + tagItem.qid + "'," + j + ");\" id='OneSelectQuick" + j + "' class=\"BatchCheckbox BatchCheckbox" + i + "\" name=\"zquick\" style=\"display:none\"  type=\"checkbox\" style=\"  vertical-align: middle; margin:0;\"></span>" + tagItem.qname + "</a></li>";
        }

        strTreeContent += "</ul>";
        strTreeContent += "</li>";
        strTreeContent += "</ul>";

        //DDL标签组
        objselectMyQuickGroup.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
    });
    initTree();
}

//选中组时
function SetEditQuickGroupBind(value, name, type) {
    m_QrGroupId = value;//标签组ID

    if (m_QrGroupId.length > 0) {
        $("#BtnDleteQuickGroup").show();
    } else {
        $("#BtnDleteQuickGroup").hide();
    }

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

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

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

//添加、修改组
function EditMyQuickGroup() {
    var objtxtMyQuickGroupName = $("#txtMyQuickGroupName");

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

    if (objtxtMyQuickGroupName.val().length == 0) {
        ShowError("myPageAlert", "快捷回复组名称不能为空！");
        objtxtMyQuickGroupName.focus();
        return;
    }

    var jsonResult;
    if (m_QrGroupId.length == 0) {
        //添加
        if (m_JsonResult.length >= 10) {
            ShowError("myPageAlert", "您最多只能添加10个自定义快捷回复组！");
            return;
        }
        talkObj.addmyquickgroup(objtxtMyQuickGroupName.val());
    }
    else {
        //修改
        talkObj.updatemyquickgroup(m_QrGroupId, objtxtMyQuickGroupName.val());
    }

    BindMyQuickList();

    $("#txtMyQuickGroupName").val("");
    $("#DeleteGroupModal").modal("hide");
    $("#divEditQuickGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
    ShowMsg("myPageAlert", "操作成功！");
}
//显示删除标签组模态框
function ShowDeleteGroup() {
    if (m_QrGroupId.length == 0) {
        return;
    }
    for (var i = 0; i < m_JsonResult.length; i++) {
        if (m_QrGroupId == m_JsonResult[i].qgid) {
            if (m_JsonResult[i].quicks.length > 0) {
                ShowError("myPageAlert", "此快捷回复组下面存在快捷回复，请先删除快捷回复！");
                return;
            } 
        }
    }
    $("#DeleteGroupModal").modal("show");
}
 
//删除组
function DeleteMyQuickGroup() {
    if (m_QrGroupId.length == 0) {
        return;
    }
    talkObj.deletemyquickgroup(m_QrGroupId);
    $("#DeleteGroupModal").modal("hide");

    $("#divEditQuickGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏

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

//选中快捷回复时
function SetEditQuickBind(qrid, parentid, name, qkey1, qkey2, qrcontent,qgid) {
    m_QrId = qrid;//ID
    m_QrGroupId = qgid;//组id
    m_qgname = name;
    var content = unescape(qrcontent);
    if (m_QrId.length > 0) {
        $("#DeleteMyQuick").show();
        $("#spDeleteTagName").html(name);
        $("#txtMyQuickName").val(name);
        $("#selectMyQuickGroup").val(parentid);
        $("#txtMyQuickRemark").val(content);
        $("#selectMyQuickKey1").val(qkey1);
        $("#selectMyQuickKey2").val(qkey2);
    } else {
        $("#DeleteMyQuick").hide();
        $("#txtMyQuickName").val("");
        $('#selectMyQuickGroup option:eq(0)').attr('selected', 'selected');
        $("#txtMyQuickRemark").val("");
        $("#selectMyQuickKey1 option:eq(0)").attr('selected', 'selected');
        $("#selectMyQuickKey2 option:eq(0)").attr('selected', 'selected');
    }

    $("#txtMyQuickName").focus();
    $("#divEditQuick").removeClass("tab-pane fade").addClass("tab-pane fade active in");//显示
    $("#divEditQuickGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
}

//添加快捷回复
function EditMyQuick() {
    var objtxtMyQuickName = $("#txtMyQuickName");
    var objselectMyQuickGroup = $("#selectMyQuickGroup");
    var objtxtMyQuickRemark = $("#txtMyQuickRemark");
    var objselectQKey1 = $("#selectMyQuickKey1");
    var objselectQKey2 = $("#selectMyQuickKey2");

    objtxtMyQuickName.val($.trim(objtxtMyQuickName.val().replace(/["]/g, '').replace(/[']/g, '')));
    objselectMyQuickGroup.val($.trim(objselectMyQuickGroup.val()));
    objtxtMyQuickRemark.val($.trim(objtxtMyQuickRemark.val().replace(/["]/g, '').replace(/[']/g, '')));
    objselectQKey1.val($.trim(objselectQKey1.val()));
    objselectQKey2.val($.trim(objselectQKey2.val()));

    if (objtxtMyQuickName.val().length == 0) {
        ShowError("myPageAlert", "名称不能为空！");
        objtxtMyQuickName.focus();
        return;
    }
    if (objselectMyQuickGroup.val() == null) {
        ShowError("myPageAlert", "未选择所属分组,若没有请先创建组！");
        objselectMyQuickGroup.focus();
        return;
    }
    if (objtxtMyQuickRemark.val().length == 0) {
        ShowError("myPageAlert", "回复内容不能为空！");
        objtxtMyQuickRemark.focus();
        return;
    }

    var jsonResult;
    if (m_QrId.length == 0) {
        //添加
        for (var i = 0; i < m_JsonResult.length; i++) {
            if (objselectMyQuickGroup.val() == m_JsonResult[i].qgid) {
                for (var j = 0; j < m_JsonResult[i].quicks.length; j++) {
                    if (objtxtMyQuickName.val() == m_JsonResult[i].quicks[j].qname) {
                        ShowError("myPageAlert", "快捷回复名称不可以重复！");
                        return;
                    }
                }
            }
        }
        talkObj.addmyquick(objselectMyQuickGroup.val(), objtxtMyQuickName.val(),
            objselectQKey1.val(), objselectQKey2.val(), objtxtMyQuickRemark.val().replace(/\n/g, '\r\n'));
    }
    else {
        //修改
        if (objselectMyQuickGroup.val() == m_QrGroupId) {
            //修改在同一组时
            for (var i = 0; i < m_JsonResult.length; i++) {
                if (objselectMyQuickGroup.val() == m_JsonResult[i].qgid) {
                    for (var j = 0; j < m_JsonResult[i].quicks.length; j++) {
                        if (objtxtMyQuickName.val() == m_JsonResult[i].quicks[j].qname && m_JsonResult[i].quicks[j].qname != m_qgname) {
                            ShowError("myPageAlert", "快捷回复名称不可以重复！");
                            return;
                        }
                    }
                }
            }
        } else {
            //修改在不同组时
                for (var i = 0; i < m_JsonResult.length; i++) {
                if (objselectMyQuickGroup.val() == m_JsonResult[i].qgid) {
                    for (var j = 0; j < m_JsonResult[i].quicks.length; j++) {
                        if (objtxtMyQuickName.val() == m_JsonResult[i].quicks[j].qname) {
                            ShowError("myPageAlert", "快捷回复名称不可以重复！");
                            return;
                        }
                    }
                }
            }
        }
        talkObj.updatequick(m_QrId, objselectMyQuickGroup.val(), objtxtMyQuickName.val(),
            objselectQKey1.val(), objselectQKey2.val(), objtxtMyQuickRemark.val().replace(/\n/g, '\r\n'));
    }
    //$("#txtMyQuickName").val("");
    //$('#selectMyQuickGroup option:eq(0)').attr('selected', 'selected');
    //$("#txtMyQuickRemark").val("");
    //$("#selectMyQuickKey1 option:eq(0)").attr('selected', 'selected');
    //$("#selectMyQuickKey2 option:eq(0)").attr('selected', 'selected');

    ShowMsg("myPageAlert", "操作成功！");
    $("#divEditQuick").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏

    BindMyQuickList();
}
//输出快捷键2
function charout() {
    $("#selectMyQuickKey2").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", "X", "Y", "Z"];
    for (var i = 0; i < arr.length; i++) {
        vhtml += "<option ";
        vhtml += " value=\"";
        vhtml += arr[i];
        vhtml += "\"";
        vhtml += ">";
        vhtml += arr[i];
        vhtml += "</option>";
    }
    $("#selectMyQuickKey2").html(vhtml);
}

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

//提交删除自定义快捷回复
function SetDeleteMyQuick() {
    if (m_QrId.length == 0)
        return;
    talkObj.deletequick(m_QrId);
    $("#DeleteTagModal").modal("hide");//隐藏删除标签模态框
    //$("#txtMyQuickName").val("");
    //$('#selectMyQuickGroup option:eq(0)').attr('selected', 'selected');
    //$("#txtMyQuickRemark").val("");
    //$("#selectMyQuickKey1 option:eq(0)").attr('selected', 'selected');
    //$("#selectMyQuickKey2 option:eq(0)").attr('selected', 'selected');
    $("#divEditQuick").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
    ShowMsg("myPageAlert", "操作成功！");
    BindMyQuickList();
}

//完成自定义，重新绑定
function SetMyQuickOK() {
    $("#MyQuickModel").modal("hide");
    talkObj.savemyquick();
    //绑定快捷回复缓存s
    homeObj.getquicktree();
    ShowMsg("myPageAlert", "请求成功！");
}

//模板导入
function ImportTemplate() {
    talkObj.importtemplate();
}
//模板导入回调
function AsyncImportTemplateResult(jsonResult) {
    if (jsonResult.success) {
        homeObj.getquicktree();
        BindMyQuickList();
        ShowMsg("myPageAlert", "操作成功！");
        //绑定快捷回复缓存   homeObj.getquicktree();
    }
    else {
        ShowError("myPageAlert", "操作失败: " + jsonResult.error);
    }
}

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

//取消按钮，隐藏批量删除按钮 多选框
function BtnBachDelHide() {
    $(".BtnBachDelShow").attr("style", "width: 100%;display:block; margin-top:4px;");
    $("#BtnBachDel").hide();
    $(".BatchCheckbox").hide();
    $("#divMyQuickTree").find(".BatchCheckbox").prop("checked", false);
}

//全选快捷回复或全不选
function SelectAllQuick(event, index) {
    event.stopPropagation();//阻止冒泡事件
    if ($("#SelectAllQuick" + index).is(":checked")) {
        $(".BatchCheckbox" + index).prop("checked", true);
    } else {
    
        $(".BatchCheckbox" + index).prop("checked", false);
    }
}

//单选
function OneSelectQuick(event, qid, index) {
    event.stopPropagation();//阻止冒泡事件
}

//确认删除按钮
function SubmitDelete() {
    var inputs = $("input[name='zquick']")
    var Qids = [];
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].checked) {
            Qids.push(inputs[i].id);
        }
    }
    if (Qids.length == 0) {
        ShowError("myPageAlert", "  请选择要删除的快捷回复！");
        return;
    }
    var strJson = $.toJSON({
        "qids": Qids
    });
    talkObj.deleteshortcutreply(strJson);
}
//确认删除按钮回调
function AsyncDeleteShortcutReplyResult(jsonResult) {
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "删除成功！");
        BindMyQuickList();
        BtnBachDelHide();
    } else {
        ShowError("myPageAlert", "删除失败！");
    }
}

//商务通快捷回复批量导入
function QuickReplyImport() {
    talkObj.quickreplyimport();
}

//商务通快捷回复批量导入回调
function AsyncQuickReplyImportResult(jsonResult) {
    if (jsonResult.success) {
        homeObj.getquicktree();
        ShowMsg("myPageAlert", "导入成功！");
        BindMyQuickList();
        //绑定快捷回复缓存
    } else {
        ShowError("myPageAlert", "导入失败:" + jsonResult.error);
    }
}

