var m_SERVICE_STATE = "friendgroup"; //好友分组key
var m_Color_STATE = "friendgroupcolor";
var m_JSON_SERVICE_GROUP = []; //好友分组缓存
var m_JSON_SERVICE_GROUPColor = [];//好友分组颜色缓存
var m_num = "";
$(function () {
    $("#myPageAlert").hide();
    PageLoadTrees();
    BindData();
});
//绑定好友分组
function BindData() {
    //var strParaJson = $.toJSON({
    //    key: m_SERVICE_STATE
    //});
    $(".loading").show();
    $(".loadingcover").show();
    manageObj.getgroupval();
}
//好友分组回调
function AsyncGetGroupValResult(jsonResult) {
    var ResultServiceLt = "";
    if (jsonResult.success) {
        if (jsonResult.wechats.length > 0) {
            var itemvalues = "";
            var itemcolors = "";
            //将json. jsonResult.wechats.m_SERVICE_STATE放到好友分组缓存中
            for (var i = 0; i < jsonResult.wechats.length; i++) {
                if (jsonResult.wechats[i].key == m_SERVICE_STATE) {
                    itemvalues = jsonResult.wechats[i];
                    //长度不为0  放入缓存
                    if (itemvalues.valus.length > 0) {
                        m_JSON_SERVICE_GROUP = $.parseJSON(itemvalues.valus);
                    }
                }
            }
            if (jsonResult.wechats.length < 2 && m_JSON_SERVICE_GROUP.length > 0) {
                for (var k = 0; k < m_JSON_SERVICE_GROUP.length; k++) {
                    m_JSON_SERVICE_GROUPColor.push("#09bb07");
                }
            } else {
                //将json. jsonResult.wechats[j].color放到颜色缓存中
                for (var j = 0; j < jsonResult.wechats.length; j++) {
                    if (jsonResult.wechats[j].key == m_Color_STATE) {
                        itemcolors = jsonResult.wechats[j];
                        //长度不为0  放入缓存
                        if (itemcolors.color.length > 0) {
                            m_JSON_SERVICE_GROUPColor = $.parseJSON(itemcolors.color);
                        } else {
                            //长度为0，给初始值，使colors的长度和m_SERVICE_STATE相等
                            for (var k = 0; k < m_JSON_SERVICE_GROUP.length; k++) {
                                m_JSON_SERVICE_GROUPColor.push("#09bb07");
                            }
                        }
                    }
                }
            }
          
            //}
            $("#ServiceState").empty();
            $("#ServiceState").html(SetServiceHTML());
            PageLoadTrees();
        }
        $(".loadingcover").hide();
        $(".loading").hide();
    }
}
//好友分组遍历
function SetServiceHTML() {
    //var max = m_JSON_SERVICE_GROUP.length;
    //var color = m_JSON_SERVICE_GROUPColor;
    var strContent = "";
    if (m_JSON_SERVICE_GROUP.length > 0) {
        for (var i = 0; i < m_JSON_SERVICE_GROUP.length; i++) {
            if (m_JSON_SERVICE_GROUPColor.length > 0) {
                strContent += "<li class=\"li-state-box li-state-box2  li-state-box3\" style='overflow:hidden' onclick=\"UpdateService(" + i + ");\"> <span style=\"background:" + m_JSON_SERVICE_GROUPColor[i] + ";\"></span>";
            } else {
                strContent += "<li class=\"li-state-box li-state-box2  li-state-box3\" style='overflow:hidden' onclick=\"UpdateService(" + i + ");\"> <span style=\"background:#09bb07;\"></span>";
            }
            //strContent += "<li class=\"li-state-box li-state-box2  li-state-box3\" onclick=\"UpdateService(" + i + ");\"> <span style=\"background:#C6ADFC;\"></span>";
            strContent += "<div class=\"state-label-div2\">" + $.trim(m_JSON_SERVICE_GROUP[i]) + " </div> ";
            strContent += "<div class=\"i-remove-delete2 i-remove-delete\" onclick=\"RemoveService(" + i + ");\"><i class=\"glyphicon glyphicon-remove\" ></i></div></li>";
        }
    }
    strContent += "<li class=\"li-state-box li-state-box2  li-state-box3\" onclick=\"startAdd();\"> ";
    strContent += "<div class=\"state-label-div2\"><i class='icon iconfont icon-add' style='font-size:14px;'></i></div> ";
    return strContent;
}

//好友分组修改点击
function UpdateService(num) {
    $("#divEdit").show();
    m_num = num;//下标赋值（修改所用）
    $("#txtServiceState").val(m_JSON_SERVICE_GROUP[m_num]);
    $("#exampleInputAmount").css("color", m_JSON_SERVICE_GROUPColor[m_num]);
    $("#exampleInputAmount").val(m_JSON_SERVICE_GROUPColor[m_num]);
    $("#BtnSubmit").html("修改分组");
}
//删除业务好友分组
function RemoveService(i) {
    event.cancelBubble = true;//禁止出发父级div事件
    m_JSON_SERVICE_GROUP.splice(i, 1);
    m_JSON_SERVICE_GROUPColor.splice(i, 1);
    ShowMsg("myPageAlert", "操作成功");
    $("#ServiceState").html(SetServiceHTML());
    //清空状态修改下标
    m_num = "";
    $("#txtServiceState").val("");
    $("#exampleInputAmount").val("");
    $("#BtnSubmit").html("添加分组");
}
//好友分组修改确认
function BtnServiceState() {
    var strService = $("#txtServiceState");
    var strColor = $("#exampleInputAmount");
    if ($.trim(Canceller(strService.val())) == "") {
        ShowError("myPageAlert", "好友分组名称不能为空！");
        $("#txtServiceState").val("");
        strService.focus();
        return;
    }
    if ($("#BtnSubmit").html() == "添加分组") {
        for (var i = 0; i < m_JSON_SERVICE_GROUP.length; i++) {
            if ($.trim(m_JSON_SERVICE_GROUP[i]) == $.trim(Canceller(strService.val()))) {
                ShowError("myPageAlert", "该状态已存在！");
                return;
            }
        }
    }
    if ($("#BtnSubmit").html() == "修改分组") {
        for (var i = 0; i < m_JSON_SERVICE_GROUP.length; i++) {
            if ($.trim(m_JSON_SERVICE_GROUP[i]) == $.trim(Canceller(strService.val())) && i != m_num) {
                ShowError("myPageAlert", "该状态已存在!");
                return;
            }
        }
    }
    if (m_num.toString() != "") {
        m_JSON_SERVICE_GROUP[m_num] = $.trim(Canceller(strService.val()));
        m_JSON_SERVICE_GROUPColor[m_num] = $.trim(strColor.val()) == "" ? "#000000" : $.trim(strColor.val());
    }
    else {
        if (Number(m_JSON_SERVICE_GROUP.length) == 20) {
            ShowError("myPageAlert", "好友分组个数不能超过20个，请删除后再进行添加!");
            return;
        }
        else {
            m_JSON_SERVICE_GROUP.push($.trim(Canceller(strService.val())));
            m_JSON_SERVICE_GROUPColor.push($.trim(strColor.val()) == "" ? "#000000" : $.trim(strColor.val()));
        }
    }
    SetServiceHTML(m_JSON_SERVICE_GROUP, m_JSON_SERVICE_GROUPColor);
    $("#ServiceState").html(SetServiceHTML(m_JSON_SERVICE_GROUP));
    //清空状态修改下标
    m_num = "";
    $("#txtServiceState").val("");
    $("#BtnSubmit").html("添加分组");

    $("#divEdit").hide();
    //关闭颜色框
    closecolorpanel();
}
//提交数据
function SetSubmit() {

    for (var i = 0; i < m_JSON_SERVICE_GROUP.length; i++) {
        m_JSON_SERVICE_GROUP[i] = $.trim(m_JSON_SERVICE_GROUP[i]);
    }
    var list = [];
    var item = {};
    item.key = m_SERVICE_STATE;
    item.value = $.trim(m_JSON_SERVICE_GROUP);
    var strParaJson = $.toJSON({
        key: m_SERVICE_STATE,
        param: m_JSON_SERVICE_GROUP,
        colors: m_JSON_SERVICE_GROUPColor
    });
    $(".loading").show();
    $(".loadingcover").show();
    manageObj.parareset(strParaJson);
}
//提交数据回调
function AsyncParaResetResult(jsonResult) {
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "操作成功！");
    }
    else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
    $(".loadingcover").hide();
    $(".loading").hide();
}

//开始添加分组
function startAdd() {
    //点击添加，清空颜色文本框
    $("#exampleInputAmount").val("");
    //点击添加，清空输入文本框
    $("#txtServiceState").val("");
    if (Number(m_JSON_SERVICE_GROUP.length) == 20) {
        ShowError("myPageAlert", "好友分组个数不能超过20个，请删除后再进行添加!");
        return;
    }
    $("#divEdit").show();
    //清空状态修改下标
    m_num = "";
    $("#BtnSubmit").html("添加分组");
}


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

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

    $("#" + objId).colorpicker({
        fillcolor: true
    });
}
$(window).resize(function () {
    PageLoadTrees();
});

//清空按钮
function resetTag() {
    BindData();
    $(".loading").show();
    $(".historyzhezhao").show();
}
//关闭颜色框
function closecolorpanel() {
    $("#colorpanel").hide();
}
$(window).resize(function () {
    PageLoadTrees();
});