var corpData;
var matSortData;
var lineData;
var dd = 0;

//删除按钮
function formatDeleteButton(value, rec) {

    return QLinkButtonHtml("删除", value);
}

$(function () {
    corpData = JSON.parse(document.getElementById("CorpList").value);
    matSortData = JSON.parse(document.getElementById("MatSortList").value);
    lineData = JSON.parse(document.getElementById("LineList").value);
    ReqPlaceData = JSON.parse(document.getElementById("RequestPlaceList").value);
    SendPlaceData = JSON.parse(document.getElementById("SendPlaceList").value);

    //设置tab
    $('#tab_set').tabs({
    });

    //设置仓库权限表
    $('#tblCorp').datagrid({
    });
    $('#tblCorp').datagrid("loadData", corpData);

    //设置零件类别权限表
    $('#tblLine').datagrid({

    });
    $('#tblLine').datagrid("loadData", lineData);

    //设置零件类别权限表
    $('#tblType').datagrid({

    });
    $('#tblType').datagrid("loadData", matSortData);

    //设置要货地权限表
    $('#tblReqPlace').datagrid({

    });
    $('#tblReqPlace').datagrid("loadData", ReqPlaceData);

    //设置发货地权限表
    $('#tblSendPlace').datagrid({

    });
    $('#tblSendPlace').datagrid("loadData", SendPlaceData);
});


//设置仓库主键
function setCorpID(data) {

    if (data != null) {

        if (data.id == document.getElementById("CorpID").value)
            return;
        document.getElementById("CorpID").value = data.id;

    }
    else {
        if (document.getElementById("CorpID").value == "")
            return;
        document.getElementById("CorpID").value = "";
    }
}

//设置零件类别主键
function setMatSortID(data) {
    if (data != null) {
        if (data.id == document.getElementById("MatSortID").value)
            return;
        document.getElementById("MatSortID").value = data.id;
    }
    else {
        if (document.getElementById("MatSortID").value == "")
            return;
        document.getElementById("MatSortID").value = "";
    }
}

//设置要货地主键
function setReqPlaceID(data) {
    if (data != null) {
        if (data.id == document.getElementById("PLACEDISID").value)
            return;
        document.getElementById("PLACEDISID").value = data.id;
    }
    else {
        if (document.getElementById("PLACEDISID").value == "")
            return;
        document.getElementById("PLACEDISID").value = "";
    }
}

//设置发货地主键
function setSendPlaceID(data) {
    if (data != null) {
        if (data.id == document.getElementById("SENDPLACEID").value)
            return;
        document.getElementById("SENDPLACEID").value = data.id;
    }
    else {
        if (document.getElementById("SENDPLACEID").value == "")
            return;
        document.getElementById("SENDPLACEID").value = "";
    }
}

//设置路线主键
function setLineID(data) {
    if (data != null) {
        if (data.id == document.getElementById("LineID").value)
            return;
        document.getElementById("LineID").value = data.id;
    }
    else {
        if (document.getElementById("LineID").value == "")
            return;
        document.getElementById("LineID").value = "";
    }
}

//添加仓库
function addCorp() {
    var data = $('#tblCorp').datagrid("getData");
    var addIndex = parseInt(data.rows.length) + 1;

    if (document.getElementById("CorpCode").value == "") {
        MSI("提示", "请选择仓库!");
        return;
    }

    if (existsJsonItem(data.rows, "CORPID", document.getElementById("CorpID").value) == true) {
        MSI("提示", "仓库已存在!");
        return;
    }

    //添加仓库
    var corp = JSON.parse("{}");
    corp["Seq"] = addIndex;
    corp["CORPID"] = document.getElementById("CorpID").value;
    corp["CORPCODE"] = document.getElementById("CorpCode").value;
    corp["CORPNAME"] = document.getElementById("CorpName").value;
    corp["DeleteAction"] = "deleteCorp(\'" + corp["CORPID"] + "\')";

    data.rows.push(corp);
    $('#tblCorp').datagrid("loadData", data);

    document.getElementById("CorpID").value = "";
    document.getElementById("CorpCode").value = "";
    document.getElementById("CorpName").value = "";

    //刷新路线
    GetAllLines();

    //刷新路线视图
    updateLineDiv();


}

//删除仓库
function deleteCorp(corpID) {
    var data = $('#tblCorp').datagrid("getData");

    data.rows = deleteJsonItem(data.rows, "CORPID", corpID);

    $('#tblCorp').datagrid("loadData", data);

    //刷新路线
    GetAllLines();

    //刷新路线视图
    updateLineDiv();
}

//添加零件类别
function addMatSort() {
    var data = $('#tblType').datagrid("getData");
    var addIndex = parseInt(data.rows.length) + 1;

    if (document.getElementById("MatSortID").value == "") {
        MSI("提示", "请选择零件类别!");
        return;
    }

    if (existsJsonItem(data.rows, "MATSORTID", document.getElementById("MatSortID").value) == true) {
        MSI("提示", "零件类别已存在!");
        return;
    }

    //添加零件类别
    var matSort = JSON.parse("{}");
    matSort["Seq"] = addIndex;
    matSort["MATSORTID"] = document.getElementById("MatSortID").value;
    matSort["MATSORTNO"] = document.getElementById("MatSortNO").value;
    matSort["MATSORTNAME"] = document.getElementById("MatSortName").value;
    matSort["DeleteAction"] = "deleteMatSort(\'" + matSort["MATSORTID"] + "\')";

    data.rows.push(matSort);
    $('#tblType').datagrid("loadData", data);

    document.getElementById("MatSortID").value = "";
    document.getElementById("MatSortNO").value = "";
    document.getElementById("MatSortName").value = "";
}

//删除零件类别
function deleteMatSort(id) {
    var data = $('#tblType').datagrid("getData");

    data.rows = deleteJsonItem(data.rows, "MATSORTID", id);

    $('#tblType').datagrid("loadData", data);
}

//全部路线标识
function allLinesStatusChange() {
    if (document.getElementById("AllLines").checked == false) {
        var data = $('#tblLine').datagrid("getData");

        data.rows.splice(0, data.rows.length)

        $('#tblLine').datagrid("loadData", data);
    }
    else {
        //刷新路线
        GetAllLines();
    }
}

//根据仓库获取全部路线
function GetAllLines() {
    if (document.getElementById("AllLines").checked == false) {
        var corpData = $('#tblCorp').datagrid("getData");
        var lineData = $('#tblLine').datagrid("getData");

        //去除不存在仓库的路线
        for (var i = lineData.rows.length - 1; i >= 0; i--) {
            if (!existsJsonItem(corpData.rows, "CORPID", lineData.rows[i]["CORPID"])) {
                lineData.rows = deleteJsonItem(lineData.rows, "LINEID", lineData.rows[i]["LINEID"]);
            }
        }
        $('#tblLine').datagrid("loadData", lineData);
        return;
    }

    var data = $('#tblCorp').datagrid("getData");

    var corpID = "";
    for (var i = 0; i < data.rows.length; i++) {
        corpID += "," + data.rows[i]["CORPID"];
    }

    if (corpID != "") {
        corpID = corpID.substr(1, corpID.length - 1);
    }
    else {
        corpID = "none";
    }
    
    var response = $.ajax({ url: "/User/GetLines?corpID=" + corpID, async: false });
    var result = response.responseText;
    lineData = JSON.parse(result);

    $('#tblLine').datagrid("loadData", lineData);
}

//添加路线
function addLine() {
    if (document.getElementById("AllLines").checked == true) {
        MSI("提示", "全部授权时不能进行编辑!");
        return;
    }

    var data = $('#tblLine').datagrid("getData");
    var addIndex = parseInt(data.rows.length) + 1;

    if (document.getElementById("LineNO").value == "") {
        MSI("提示", "请选择路线!");
        return;
    }

    var values = document.getElementById("LineID").value.split(",");


    if (existsJsonItem(data.rows, "LINEID", values[0]) == true) {
        MSI("提示", "路线已存在!");
        return;
    }

    var corpData = $('#tblCorp').datagrid("getData").rows;

    var corp = findJsonItem(corpData, "CORPID", values[1]);

    if (corp == null) {
        MSI("提示", "请选择有效仓库下的路线!");
        return;
    }

    var line = JSON.parse("{}");
    line["Seq"] = addIndex;
    line["LINEID"] = values[0];
    line["LINENO"] = document.getElementById("LineNO").value;
    line["LINENAME"] = document.getElementById("LineName").value;
    line["CORPID"] = corp.CORPID;
    line["CORPNAME"] = corp.CORPNAME;
    line["DeleteAction"] = "deleteLine(\'" + line["LINEID"] + "\')";

    data.rows.push(line);
    $('#tblLine').datagrid("loadData", data);

    document.getElementById("LineID").value = "";
    document.getElementById("LineNO").value = "";
    document.getElementById("LineName").value = "";
}

//删除路线
function deleteLine(lineID) {
    if (document.getElementById("AllLines").checked == true) {
        MSI("提示", "全部授权时不能进行编辑!");
        return;
    }

    var data = $('#tblLine').datagrid("getData");

    data.rows = deleteJsonItem(data.rows, "LINEID", lineID);

    $('#tblLine').datagrid("loadData", data);
}

//保存业务权限
function Save() {
    var corpData = $('#tblCorp').datagrid("getData");
    document.getElementById("CorpList").value = JSON.stringify(corpData.rows);

    var lineData = $('#tblLine').datagrid("getData");
    document.getElementById("LineList").value = JSON.stringify(lineData.rows);

    var matSortData = $('#tblType').datagrid("getData");
    document.getElementById("MatSortList").value = JSON.stringify(matSortData.rows);

    var reqPlaceData = $('#tblReqPlace').datagrid("getData");
    document.getElementById("RequestPlaceList").value = JSON.stringify(reqPlaceData.rows);

    var sendPlaceData = $('#tblSendPlace').datagrid("getData");
    document.getElementById("SendPlaceList").value = JSON.stringify(sendPlaceData.rows);

    //提交保存
    MSQ("提示", "确定要保存吗?", function () {
        submitByButton("UserBusiPowerSave");
    })
}

///刷新路线视图
function updateLineDiv() {
    var data = $('#tblCorp').datagrid("getData");
    var corpID = "";
    for (var i = 0; i < data.rows.length; i++) {
        corpID += "," + data.rows[i]["CORPID"];
    }

    if (corpID != "") {
        corpID = corpID.substr(1, corpID.length - 1);
    }

    $("#divLine").load("/User/UpdateDivLine",
                        { corpID: corpID });
}

//添加要货地
function addReqPlace() {
    var data = $('#tblReqPlace').datagrid("getData");
    var addIndex = parseInt(data.rows.length) + 1;

    if (document.getElementById("PLACEDISNO").value == "") {
        MSI("提示", "请选择要货地!");
        return;
    }

    if (existsJsonItem(data.rows, "PLACEDISID", document.getElementById("PLACEDISID").value) == true) {
        MSI("提示", "要货地已存在!");
        return;
    }

    //添加要货地
    var reqPlace = JSON.parse("{}");
    reqPlace["Seq"] = addIndex;
    reqPlace["PLACEDISID"] = document.getElementById("PLACEDISID").value;
    reqPlace["PLACEDISNO"] = document.getElementById("PLACEDISNO").value;
    reqPlace["DESCRIBE_SITE"] = document.getElementById("DESCRIBE_SITE").value;
    reqPlace["DeleteAction"] = "deleteReqPlace(\'" + reqPlace["PLACEDISID"] + "\')";

    data.rows.push(reqPlace);
    $('#tblReqPlace').datagrid("loadData", data);

    document.getElementById("PLACEDISID").value = "";
    document.getElementById("PLACEDISNO").value = "";
    document.getElementById("DESCRIBE_SITE").value = "";
}

//删除要货地
function deleteReqPlace(id) {
    var data = $('#tblReqPlace').datagrid("getData");

    data.rows = deleteJsonItem(data.rows, "PLACEDISID", id);

    $('#tblReqPlace').datagrid("loadData", data);
}

//添加发货地
function addSendPlace() {
    var data = $('#tblSendPlace').datagrid("getData");
    var addIndex = parseInt(data.rows.length) + 1;

    if (document.getElementById("SENDPLACEID").value == "") {
        MSI("提示", "请选择发货地!");
        return;
    }

    if (existsJsonItem(data.rows, "SENDPLACEID", document.getElementById("SENDPLACEID").value) == true) {
        MSI("提示", "发货地已存在!");
        return;
    }

    //添加发货地
    var sendPlace = JSON.parse("{}");
    sendPlace["Seq"] = addIndex;
    sendPlace["SENDPLACEID"] = document.getElementById("SENDPLACEID").value;
    sendPlace["SENDPLACENO"] = document.getElementById("SENDPLACENO").value;
    sendPlace["SENDPLACENAME"] = document.getElementById("SENDPLACENAME").value;
    sendPlace["DeleteAction"] = "deleteSendPlace(\'" + sendPlace["SENDPLACEID"] + "\')";

    data.rows.push(sendPlace);
    $('#tblSendPlace').datagrid("loadData", data);

    document.getElementById("SENDPLACEID").value = "";
    document.getElementById("SENDPLACENO").value = "";
    document.getElementById("SENDPLACENAME").value = "";
}

//删除发货地
function deleteSendPlace(id) {
    var data = $('#tblSendPlace').datagrid("getData");

    data.rows = deleteJsonItem(data.rows, "SENDPLACEID", id);

    $('#tblSendPlace').datagrid("loadData", data);
}