var m_UserList = [];//客服缓存
var m_WeChatList = [];//微信号缓存
var m_SelectUsers = [];//已选人员缓存数据
var m_NowOperationUserId = "";//当前操作人员ID
var m_DisKey = "dis";//分配规则Key
$(function () {
    PageLoadTrees();
    $("#myPageAlert").hide();
    $("#divUserInfo").hide();
    $("#BtnSubmit").hide();
    BindData();
   

});
//分配规则获取
function BindData() {
    $(".loading").show();
    $(".loadingcover").show();
    manageObj.getwechatlist();//获取公众帐号列表
    manageObj.getuserslist();//获取部门树形列表
    var strParaJson = $.toJSON({
        key: m_DisKey
    });
    manageObj.getpara(strParaJson);
    $("#divUserInfo").hide();
    $("#BtnSubmit").hide();
}
//获取分配规则回调
function AsyncGetParaResult(jsonResult) {
    if (jsonResult.success) {
        //var dad = $.parseJSON(jsonResult);
        var jsonval = $.parseJSON(jsonResult.result.info.val)
        if (jsonResult.result.info.key == m_DisKey) {
            for (var i = 0; i < jsonval.length; i++) {
                m_SelectUsers = jsonval;
            }
        }
    }
    else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
    $(".loadingcover").hide();
    $(".loading").hide();
}

//获取人员信息回调
function AsyncGetUsersListResult(jsonResult) {
    m_UserList = jsonResult.result.list;
    if (jsonResult.success) {
        var strContent = "<table class=\"table\">";
        for (var i = 0; i < jsonResult.result.list.length; i++) {
            var item = jsonResult.result.list[i];
            strContent += "<tr>";
            strContent += "<td align=\"left\"><span style=\"cursor:pointer;\" onclick=\"ShowUserInfo('" + item.uid + "');\">";
            strContent += item.name;
            strContent += "</span></td>";
            strContent += "<td align=\"right\" width=\"14\">";
            strContent += "</td>";
        }
        strContent += "</table>";

        $("#divUserList").html(strContent);


    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}
//获取微信号列表回调
function AsyncGetWechatListResult(jsonResult) {
    if (jsonResult.success) {
        m_WeChatList = jsonResult;
    }
}
function PageLoadTrees() {
    $(".left-list-l").css({
        height: $(window).height() - 40
    });

    $(".talklist-m").css({
        height: $(window).height() - 130
    });
    initTree();
}

$(document).ready(function () {
    PageLoadTrees();
    $(".distribution-m span").click(function () {
        $(this).toggleClass("active");
    });
    $(".distribution1-m span").click(function () {
        $(this).toggleClass("active");
    });
    $(".department2-m span").click(function () {
        $(this).toggleClass("active");
    });
    $(".distributionlist-m li").click(function () {
        $(this).toggleClass("active");
    });
    $(".distributionbtn-m").click(function () {
        $(".distributiontip-m").removeClass("hidden");
        $(".distribution-m").addClass("hidden");
    });
    $(".distributionbtn1-m").click(function () {
        $(".distributiontip1-m").removeClass("hidden");
        $(".distribution1-m").addClass("hidden");
    });
});

$(window).resize(function () {
    PageLoadTrees();
});

function ShowUserInfo(code) {

    var item = GetUser(code);
    m_NowOperationUserId = code;
    $("#spSelectUserName").html(item.name);
    BindPublicList();//刷新公众帐号绑定情况
    $("#divUserInfo").show();
    $("#BtnSubmit").show();
}
//刷新绑定微信号列表
function BindPublicList() {
    var strContent = "";
    var selectUserItem = GetUser(m_NowOperationUserId);
    if (selectUserItem != null) {
        if (m_WeChatList.wechats.length == 0) {
            //全部分配
            strContent += "<p class=\" distributiontip-m\"><span class=\"icon-edit\"></span>全部分配</p>";
        }
    }
    var itemBindWechat = GetBingUserWechat(m_NowOperationUserId);
    strContent += "<ul class=\"distributionlist-m\">";
    for (var i = 0; i < m_WeChatList.wechats.length; i++) {
        var item = m_WeChatList.wechats[i];
        if (itemBindWechat != null && itemBindWechat.wechats.length > 0) {
            var bIsSelect = false;
            for (var j = 0; j < itemBindWechat.wechats.length; j++) {

                if (itemBindWechat != null) {
                    if (item.uin == itemBindWechat.wechats[j]) {
                        bIsSelect = true;
                    }
                }
                var strTypeMark = "";
            }
            if (bIsSelect) {
                //已选
                strContent += "<li class=\"active\" id=\"li" + item.uin + "\" onclick=\"CancelSelectPublic('" + item.uin + "');\"><img src=\"" + item.headimgurl + "\" width=\"50\" height=\"50\"><div><h2>" + item.nickname + "&nbsp;&nbsp;<i class=\"icon-ok-sign\"></i></h2><p>" + strTypeMark + "</p>已选</div></li>";
            } else {
                //取消选中
                strContent += "<li id=\"li" + item.uin + "\" onclick=\"SelectPublic('" + item.uin + "');\"><img src=\"" + item.headimgurl + "\" width=\"50\" height=\"50\"><div><h2>" + item.nickname + "</h2><p>" + strTypeMark + "</p></div></li>";
            }
        } else {

            var bIsSelect = false;
            var strTypeMark = "";

            if (bIsSelect) {
                //已选
                strContent += "<li class=\"active\" id=\"li" + item.uin + "\" onclick=\"CancelSelectPublic('" + item.uin + "');\"><img src=\"" + item.headimgurl + "\" width=\"50\" height=\"50\"><div><h2>" + item.nickname + "&nbsp;&nbsp;<i class=\"icon-ok-sign\"></i></h2><p>" + strTypeMark + "</p>已选</div></li>";
            } else {
                //取消选中
                strContent += "<li id=\"li" + item.uin + "\" onclick=\"SelectPublic('" + item.uin + "');\"><img src=\"" + item.headimgurl + "\" width=\"50\" height=\"50\"><div><h2>" + item.nickname + "</h2><p>" + strTypeMark + "</p></div></li>";
            }
        }
    }
    strContent += "</ul>";
    $("#divPublics").html(strContent);
    PageLoadTrees();
}

//根据ID获取是否绑定微信号
function GetBingUserWechat(userid) {
    for (var i = 0; i < m_SelectUsers.length; i++) {
        if (m_SelectUsers[i].user == userid) {
            return m_SelectUsers[i];
        }
    }
    return null;
}
//根据ID取得一个人员信息
function GetUser(userid) {
    for (var i = 0; i < m_UserList.length; i++) {
        if (m_UserList[i].uid == userid) {
            return m_UserList[i];
        }
    }
    return null;
}

//选择人员的微信号
function SelectPublic(wechat) {
    var selectUserItem = GetUser(m_NowOperationUserId);
    if (selectUserItem != null) {
        if (m_SelectUsers.length < 1) {
            var item = {};
            item.user = selectUserItem.uid;
            var chartslist = []
            chartslist.push(wechat);
            item.wechats = chartslist;
            m_SelectUsers.push(item);
        } else {
            var num = 0;
            for (var i = 0; i < m_SelectUsers.length; i++) {
                if (m_SelectUsers[i].user == selectUserItem.uid) {

                    m_SelectUsers[i].wechats.push(wechat);
                    num++;
                }
            }
            if (num == 0) {
                var item = {};
                item.user = selectUserItem.uid;
                var chartslist = []
                chartslist.push(wechat);
                item.wechats = chartslist;
                m_SelectUsers.push(item);
            }
        }
        BindPublicList();
    }
}

//取消选择人员公众帐号
function CancelSelectPublic(appid) {
    var selectUserItem = GetUser(m_NowOperationUserId);
    for (var i = 0; i < m_SelectUsers.length; i++) {
        if (selectUserItem.uid == m_SelectUsers[i].user) {
            for (var j = 0; j < m_SelectUsers[i].wechats.length; j++) {
                if (m_SelectUsers[i].wechats[j] == appid) {
                    m_SelectUsers[i].wechats.splice(j, 1);

                }
            }
        }
    }
    BindPublicList();
}

//提交数据
function SetSubmit() {
    var strParaJson = $.toJSON({
        key: m_DisKey,
        param: m_SelectUsers
    });
    manageObj.parareset(strParaJson);
}

//提交数据回调方法
function AsyncParaResetResult(jsonResult) {
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "操作成功！");
        BindData();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}

//显示删除分配规则模态对话框
function ShowDeleteModel() {
    $("#DeleteGroupModal").modal("show");
}
//提交删除组
function CommitGroupDelete() {
    for (var i = 0; i < m_SelectUsers.length; i++) {
        if (m_SelectUsers[i].user == m_NowOperationUserId) {
            m_SelectUsers.splice(i, 1);
        }
    }
    var strParaJson = $.toJSON({
        key: m_DisKey,
        param: m_SelectUsers
    });
    manageObj.parareset(strParaJson);
}
//删除分配规则回调
function AsyncParaResetResult(jsonResult)
{
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "操作成功！");
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
    $("#DeleteGroupModal").modal("hide");
}

