//==================工资构成设置===============
//state:1 新增
//state:2 修改
function openGZGC() {
    try {
        var tbId = strTableP050802;
        var dlgId = strDivP050802;
        var $dlgId = $("#" + dlgId);

        //创建dialog
        creatDiv_ShowDlgHaveTbl(dlgId, "工资构成设置", true, false, tbId, 600);
        $("#" + dlgId + "_divform").append('<table id="' + tbId + '"></table></br>');

        var $tbId = $("#" + tbId);
        $("#" + dlgId + "_pop_box_top").html('<a style="cursor:pointer;text-decoration:none" title="添加" onclick="openSetGZGC();return false; ">添加</a>');
        var config = ParseData(window.external.DoOptions(tbId, "GetTableConfig", "{}"));

        config.colNames.push("操作");
        config.colModel.push({ "sortable": false, "edittype": "text", "editable": false, "index": "cz", "hidden": false, "width": 250, "editrules": { "required": false }, "name": "cz", "Formatter": {}, "align": "center" });

        createDjGrid(tbId, config.colNames, config.colModel, false);
		$("#" + tbId).setLabel("rn", "序号");
		
        bindHoverEvent(tbId);
        $tbId.setColProp("zt", { unformat: unformatState, formatter: formatState });
        $tbId.setColProp("lb", { unformat: unformatClass, formatter: formatClass });
        var s = ParseData(window.external.DoOptions(tbId, "GetList", "{}"));
        var mylist = [];
        if (s.ReturnCode == 0 && s.Records instanceof Array) {
            mylist = s.Records;
        }

        loadGridData(tbId, mylist);
        $("#" + tbId).setGridHeight(281);

        $(".layoutpanel").css("z-index", "50000009");
        resizeGridScroll(tbId);
        $tbId = $dlgId = config = mylist = s = null;
    } catch (e) { }
}

//为工资构成设置绑定事件
function bindHoverEvent(tbId) {
    $("#" + tbId).delegate("tr[id]:visible", "hover", function (e) {
        var $tbId = $("#" + tbId);
        if (e.type == "mouseenter") {
            var state = $tbId.getCell(this.id, "zt");
            if (state == "True") { var title = "停用"; } else { var title = "启用"; }
            var item = $tbId.getRowData(this.id);
            if (item.flag == "True") {
                var btn = '<a  style="color:red;cursor:pointer;text-decoration:none" onclick=" setRowOrder(' + this.id + ', \'up\');return false; ">上移</a>|<a  style="color:red;cursor:pointer;text-decoration:none" onclick=" setRowOrder(' + this.id + ', \'down\');return false; ">下移</a>';
            } else {
                var btn = '<a style="color:red;cursor:pointer;text-decoration:none" onclick="changeStateGZGC(' + this.id + ');return false; ">' + title + '</a>|<a style="color:red;cursor:pointer;text-decoration:none" onclick="openSetGZGC(' + this.id + ');return false; ">修改</a>|<a style="color:red;cursor:pointer;text-decoration:none" onclick="delGZGC(' + this.id + ');return false; ">删除</a>|<a  style="color:red;cursor:pointer;text-decoration:none" onclick=" setRowOrder(' + this.id + ', \'up\');return false; ">上移</a>|<a  style="color:red;cursor:pointer;text-decoration:none" onclick=" setRowOrder(' + this.id + ', \'down\');return false; ">下移</a>';
            }
            $tbId.setCell(this.id, "cz", btn);
            item = null;
        }
        if (e.type == "mouseleave") {
            $tbId.setCell(this.id, "cz", " ");
        }
        $tbId = null;
    });
}

function openSetGZGC(rowId) {
    try {
        creatDiv_ShowDialog("divAdd", "工资类别", false, true, strTableP050802, 400);
        var strhtml = '<ul><li>类别：<select id="type"  class="easyui-combobox"><option value="0">增加类</option><option value="1">减少类</option></select></li><li>名称：<input  id="typeName" type="text"  maxlength="50"  onkeypress="return checkValidate(event)" onpaste="return validatePaste()" /></li></ul>';
        $("#divAdd_divform").append(strhtml);

        $("#type", "#divAdd").combobox({ panelHeight: 42, editable: false, onSelect: function () { $("#divAdd").data("change", true); } });
        $("#typeName", "#divAdd").change(function () {
            $("#divAdd").data("change", true);
        });
        if (rowId) {                                       //修改
            $("#divAdd").data("state", 2).data("rowId", rowId);
            var item = $("#" + strTableP050802).getRowData(rowId);
            var ss = window.external.DoOptions(strTableP050802, "DoOthers", '{"flag":"1","lmguid":"' + item.lmguid + '"}');
            if (ss == "false") {
                $("#type", "#divAdd").combobox("disable");
            }
            $("#type", "#divAdd").combobox("setValue", item.lb);
            $("#typeName", "#divAdd").val(item.lm);
        } else {                                        //新增
            $("#divAdd").data("state", 1);
        }
        $("#divAdd").data("change", false);

        $(".layoutpanel").css("z-index", "50000009");
        setTimeout(function () { $("#typeName", "#divAdd").focus(); }, 100);
    } catch (e) { }
}

//删除工资构成中的列
function delGZGC(rowId) {
    try {
        var tbId = strTableP050802;
        var $tbId = $("#" + strTableP050802);
        var delItem = $tbId.getRowData(rowId);
        if (delItem.lmguid) {//如果有mxguid
            var ss = window.external.DoOptions(tbId, "DoOthers", '{"flag":"1","lmguid":"' + delItem.lmguid + '"}');
            if (ss == "true") {
                window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo,
                     function (strRet) {
                         if (msgBox.dlgRst.Yes == strRet) {                             //发送到后台删除数据
                             var s = ParseData(window.external.DoOptions(tbId, "Delete", '{"lmguid":"' + delItem.lmguid + '"}'));      // 向后台请求配置信息
                             if (s.ReturnCode == 0) {
                                 $tbId.delRowData(rowId);
                                 resizeGridScroll(tbId);
                             } else {
                                 window.parent.cihoAlert(record.MSG, msgBox.dlgBtn.Ok);
                             }
                             $tbId = delItem = null;
                         }
                     }
                );
            } else {
                window.parent.cihoAlert("名称已被引用，不允许删除！", msgBox.dlgBtn.Ok);
            }
        }
        else {
            $tbId.delRowData(rowId);
            resizeGridScroll(tbId);
        }
    } catch (e) { }
}

//改变工资
function changeStateGZGC(rowId) {
    try {
        var tbId = strTableP050802;
        var $tbId = $("#" + tbId);
        var item = $tbId.getRowData(rowId);

        if (item.zt == "True") {
            var s = ParseData(window.external.DoOptions(tbId, "ChangeStat", '{"lmguid":"' + item.lmguid + '","zt":"false"}'));
            if (s.ReturnCode == 0) {
                $tbId.setCell(rowId, "zt", "False");
                //var btn = '<a style="color:red;cursor:pointer;text-decoration:none" onclick="changeStateGZGC(' + rowId + ');return false; ">停用</a>|<a style="color:red;cursor:pointer;text-decoration:none" onclick="openSetGZGC(' + rowId + ');return false; ">修改</a>|<a style="color:red;cursor:pointer;text-decoration:none" onclick="delGZGC(' + rowId + ');return false; ">删除</a>|<a  style="color:red;cursor:pointer;text-decoration:none" onclick=" setRowOrder(' + rowId + ', \'up\');return false; ">上移</a>|<a  style="color:red;cursor:pointer;text-decoration:none" onclick=" setRowOrder(' + rowId + ', \'down\');return false; ">下移</a>';
                $tbId.setCell(rowId, "cz", null);
            }
        } else {
            var s = ParseData(window.external.DoOptions(tbId, "ChangeStat", '{"lmguid":"' + item.lmguid + '","zt":"true"}'));
            if (s.ReturnCode == 0) {
                $tbId.setCell(rowId, "zt", "True");
                //var btn = '<a style="color:red;cursor:pointer;text-decoration:none" onclick="changeStateGZGC(' + rowId + ');return false; ">启用</a>|<a style="color:red;cursor:pointer;text-decoration:none" onclick="openSetGZGC(' + rowId + ');return false; ">修改</a>|<a style="color:red;cursor:pointer;text-decoration:none" onclick="delGZGC(' + rowId + ');return false; ">删除</a>|<a  style="color:red;cursor:pointer;text-decoration:none" onclick=" setRowOrder(' + rowId + ', \'up\');return false; ">上移</a>|<a  style="color:red;cursor:pointer;text-decoration:none" onclick=" setRowOrder(' + rowId + ', \'down\');return false; ">下移</a>';
                $tbId.setCell(rowId, "cz", null);
            }
        }
        $tbId = item = null;
    } catch (e) { }
}

function setRowOrder(rowId, dir) {
    try {
        var tbId = strTableP050802;
        var $tbId = $("#" + tbId);
        var ids = $tbId.getDataIDs();
        var idx = -1;

        for (var i in ids) {
            if (ids[i] == rowId) { idx = i * 1; break; }
        }

        var item = $tbId.getRowData(rowId);
        item.cz = "";

        if (dir == "down") {
            if (ids[idx + 1]) {                         //如果存在此id
                if ($tbId.delRowData(rowId)) {
                    $tbId.addRowData(rowId, item, "after", ids[idx + 1]);
                }
            }
        } else if (dir == "up") {
            if (ids[idx - 1]) {
                if ($tbId.delRowData(rowId)) {
                    $tbId.addRowData(rowId, item, "before", ids[idx - 1]);
                }
            }
        }

    } catch (e) { }
}

function updateOrder() {
    try {
        var tbId = strTableP050802;
        var ids = $("#" + tbId).getDataIDs();
        var order = [];
        for (var i = 0; i < ids.length; i++) {
            var guid = $("#" + tbId).getCell(ids[i], "lmguid");
            if (guid) {
                order.push('{"lmguid":"' + guid + '","order":"' + i + '"}');
            }
        }
        var records = '{"flag":"0","Records":[' + order + ']}';

        //发送排序到后台
        var s = ParseData(window.external.DoOptions(tbId, "DoOthers", records));

        if (s.ReturnCode == 0) {
            return true;
        } else {
            window.parent.cihoAlert(s.MSG, msgBox.dlgBtn.Ok);
            return false;
        }
    } catch (e) { }
}

function formatState(cellvalue, options, row) {
    switch (cellvalue) {
        case "True":
            return "已启用";
        case "False":
            return "已停用";
        default:
            return "&nbsp;";
    }
}

function unformatState(cellvalue, options) {
    switch (cellvalue) {
        case "已启用":
            return "True";
        case "已停用":
            return "False";
        default:
            return "";
    }
}

function formatClass(cellvalue, options, row) {
    switch (cellvalue) {
        case "0":
            return "增加类";
        case "1":
            return "减少类";
        default:
            return "&nbsp;";
    }
}

function unformatClass(cellvalue, options) {
    switch (cellvalue) {
        case "增加类":
            return "0";
        case "减少类":
            return "1";
        default:
            return "";
    }
}

function saveGZGC(tbId, afterSave, dlgId) {
    try {
        var $tbId = $("#" + tbId);
        var state = $("#divAdd").data("state");
        var typebh = $("#type", "#divAdd").combobox("getValue");
        var typeName = $("#typeName", "#divAdd").val();
        var rowId = $("#divAdd").data("rowId");

        if (!$.trim(typeName)) {                                                                             //请输入类名
            window.parent.cihoAlert("类名不能为空！", msgBox.dlgBtn.Ok, function () {
                $("#typeName", "#divAdd").focus();
            });
            return false;
        }

        var ids = $tbId.getDataIDs();
        for (var i = 0; i < ids.length; i++) {
            var k = $tbId.getCell(ids[i], "lm");
            if (k == typeName) {
                if (rowId == ids[i]) {
                    continue;
                } else {
                    window.parent.cihoAlert("已存在此名称！", msgBox.dlgBtn.Ok, function () {
                        $("#typeName", "#divAdd").select();
                    });
                    return false;
                }
            }
        }

        if (state == 1) {
            var s = ParseData(window.external.DoOptions(tbId, "Add", '{"lm":"' + typeName + '","lb":"' + typebh + '"}'));
        } else {
            var rowId = $("#divAdd").data("rowId");
            var lmguid = $("#" + tbId).getCell(rowId, "lmguid");
            var s = ParseData(window.external.DoOptions(tbId, "Edit", '{"lmguid":"' + lmguid + '","lm":"' + typeName + '","lb":"' + typebh + '"}'));
        }

        if (s.ReturnCode == 0) {                    //将数据添加到上一个对话框
            if (state == 1) {
                var item = {};
                item.zt = "True";
                item.lm = typeName;
                item.lb = typebh;
                item.lmguid = s.lmguid;
                var rowId = $("#" + tbId).data("nextNum");
                $("#" + tbId).addRowData(rowId, item);
                $("#" + tbId).data("nextNum", rowId + 1);
                resizeGridScroll(tbId);
            } else if (state == 2) {
                var rowId = $("#divAdd").data("rowId");
                var item = $("#" + tbId).getRowData(rowId);
                item.lm = typeName;
                item.lb = typebh;
                $("#" + tbId).setCell(rowId, "lm", typeName);
                $("#" + tbId).setCell(rowId, "lb", typebh);
            }

            if (afterSave == 1) {
                cihoDlgClose(dlgId);
            } else {
                $("#typeName", "#divAdd").val("");
                $("#" + dlgId).data("change", false);
                $("#typeName", "#divAdd").focus();
                $("#type", "#divAdd").combobox("enable");
            }
        } else {
            window.parent.cihoAlert(s.MSG, msgBox.dlgBtn.Ok);
            return false;
        }
    } catch (e) { }
}

// =================jqgrid非重复值判断==============
//添加时判断jqgrid某列中是否存在值
//colName---判断的列名
//val----------要比较的值
//bSel--------是否比较选中行的值,新增时为false
//tdId---------grid 的id

function isKeyExist(tbId, colName, val) {
    var $tbId = $("#" + tbId);
    var ids = $tbId.getDataIDs();
    for (var i in ids) {
        var k = $tbId.getCell(ids[i], colName);
        if (k == val) { return true; }
    }
}