var m_SERVICE_STATE = "g_business_state"; //业务状态
var m_JSON_SERVICE_STATE = []; //业务状态缓存
var m_num = -1;
$(function () {
    $("#myPageAlert").hide();
    PageLoadTrees();
    BindData();
});
//绑定业务状态
function BindData()
{
    //var strParaJson = $.toJSON({
    //    key: m_SERVICE_STATE
    //});
    manageObj.getbisval();
}
//业务状态回调
function AsyncGetBisValResult(jsonResult) {
    var ResultServiceLt = "";
    if (jsonResult.success) {
    	if (jsonResult.wechats.length > 0) {
    		for (var i = 0; i < jsonResult.wechats.length; i++) {
    			if (jsonResult.wechats[i].key == m_SERVICE_STATE) {
    				if (jsonResult.wechats[i].valus.length > 2) {
    					var item = jsonResult.wechats[i];
    						m_JSON_SERVICE_STATE = $.parseJSON(item.valus);
    				}
    			}
    		}
        	
        }

        $("#ServiceState").empty();
        $("#ServiceState").html(SetServiceHTML());
        PageLoadTrees();
    }
    $(".loadingcover").hide();
    $(".loading").hide();
}
//业务状态遍历
function SetServiceHTML() {
    var max = m_JSON_SERVICE_STATE.length
    var strContent = "";
    if (m_JSON_SERVICE_STATE.length > 0) {
        for (var i = 0; i < m_JSON_SERVICE_STATE.length; i++) {
            strContent += "<li class=\"li-state-box\" onclick=\"UpdateService(" + i + ");\" style='margin-left:5px;'> ";
            strContent += "<div class=\"state-label-div\">" + $.trim(m_JSON_SERVICE_STATE[i]) + " </div> ";
            if (Number(i) == 0) {
            }
            else {
                strContent += "<div class=\" i-remove-left\" onclick=\"SetLeftServiceState(" + i + ");\"><i class=\"fa fa-chevron-left\" ></i></div>";
            }
            if (Number(i + 1) == Number(max)) {
            }
            else {
                strContent += "<div class=\"i-remove-right\" onclick=\"SetRightServiceState(" + i + ");\"><i class=\"fa fa-chevron-right\" ></i></div>";
            }
            strContent += "<div class=\"i-remove-delete\" onclick=\"RemoveService(" + i + ");\"><i class=\"fa fa-times\" ></i></div></li>";
            if (Number(i + 1) == Number(max)) {
            }
            else {
                strContent += "<li class=\"li-state-img\" style='margin-left:5px;'><i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"></i></li>";
            }
        }
    }

    strContent += "<li class=\"li-state-box li-state-box2\" onclick=\"startAdd();\" style='margin-left:5px;'> ";
    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_STATE[m_num]);
    $("#BtnSubmit").html("修改");
}
//删除业务状态
function RemoveService(i) {
    event.cancelBubble = true;//禁止出发父级div事件
    m_JSON_SERVICE_STATE.splice(i, 1);
    ShowMsg("myPageAlert", "操作成功");
    $("#ServiceState").html(SetServiceHTML());
    //清空状态修改下标
    m_num = -1;
    $("#txtServiceState").val("");
    $("#BtnSubmit").html("添加");
}
//状态修改确认
function BtnServiceState() {
    var strService = $("#txtServiceState");
    if ($.trim(strService.val().replace(/["]/g, '').replace(/[']/g, '')) == "") {
        ShowError("myPageAlert", "状态不能为空！");
        $("#txtServiceState").val("");
        strService.focus();
        return;
    }
    //添加时
    if ($("#BtnSubmit").text() == "添加") {
        for (var i = 0; i < m_JSON_SERVICE_STATE.length; i++) {
            if ($.trim(m_JSON_SERVICE_STATE[i]) == $.trim(Canceller(strService.val()))) {
                ShowError("myPageAlert", "该状态已存在！");
                return;
            }
        }
    } else {
        //修改时
        for (var i = 0; i < m_JSON_SERVICE_STATE.length; i++) {
            if ($.trim(m_JSON_SERVICE_STATE[i]) == $.trim(Canceller(strService.val())) && i != m_num) {
                ShowError("myPageAlert", "该状态已存在！");
                return;
            }
        }
    }


    if (m_num != -1) {
        m_JSON_SERVICE_STATE[m_num] = $.trim(Canceller(strService.val()));
    }
    else {
        if (Number(m_JSON_SERVICE_STATE.length) == 20) {
            ShowError("myPageAlert", "业务状态个数不能超过20个，请删除后再进行添加!");
            return;
        }
        else {
            m_JSON_SERVICE_STATE.push($.trim(Canceller(strService.val())));
        }
    }
    SetServiceHTML(m_JSON_SERVICE_STATE);
    $("#ServiceState").html(SetServiceHTML(m_JSON_SERVICE_STATE));
    //清空状态修改下标
    m_num = -1;
    $("#txtServiceState").val("");
    $("#BtnSubmit").html("添加");

    $("#divEdit").hide();
}
//右移状态
function SetRightServiceState(index) {
    if (m_num != -1) {
        if (m_num - index == 1) {
            m_num--;
        }else if(m_num == index){
            m_num++;
        }
    }
    event.cancelBubble = true;//禁止出发父级div事件
    var ItemServiceState = m_JSON_SERVICE_STATE[index];
    m_JSON_SERVICE_STATE[index] = m_JSON_SERVICE_STATE[index + 1];
    m_JSON_SERVICE_STATE[index + 1] = ItemServiceState;
    $("#ServiceState").html(SetServiceHTML());
}
//左移状态
function SetLeftServiceState(index) {
    if(m_num != -1){
        if (index - m_num == 1) {
            m_num++;
        }else if(m_num == index){
            m_num--;
        }
    }
    event.cancelBubble = true;//禁止出发父级div事件
    var ItemServiceState = m_JSON_SERVICE_STATE[index];
    m_JSON_SERVICE_STATE[index] = m_JSON_SERVICE_STATE[index - 1];
    m_JSON_SERVICE_STATE[index - 1] = ItemServiceState;
    $("#ServiceState").html(SetServiceHTML());
}

//提交数据
function SetSubmit() {

    if (m_JSON_SERVICE_STATE.length < 1)
    {
        ShowError("myPageAlert", "请至少添加一个业务状态！");
        return;
    }
    for (var i = 0; i < m_JSON_SERVICE_STATE.length; i++) {
        m_JSON_SERVICE_STATE[i] = $.trim(m_JSON_SERVICE_STATE[i]);
    }
    var list = [];
    var item = {};
    item.key = m_SERVICE_STATE;
    item.value = $.trim(m_JSON_SERVICE_STATE);
    var strParaJson = $.toJSON({
        key: m_SERVICE_STATE,
        param: m_JSON_SERVICE_STATE
    });
    $(".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() {
    if (Number(m_JSON_SERVICE_STATE.length) == 20) {
        ShowError("myPageAlert", "业务状态个数不能超过20个，请删除后再进行添加!");
        return;
    }
    $("#divEdit").show();
    //清空状态修改下标
    m_num = -1;
    $("#txtServiceState").val("");
    $("#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
    });
    $(".friends-table").css({
        height: $(window).height() - 180
    });
    initTree();
}
$(window).resize(function () {
    PageLoadTrees();
});