/*************************
*功能：会计科目
*创建人：张振华
*时间：2010-08-16
*备注：
*************************/

var strTableP0606 = "TKjkm";
var strDivP0606 = "divP0606";
var strOpratP0606 = "1";  //操作类型 0是新增;1是修改;2是新增下一级;3是从其他页修改
var strSelRowIdP0606;   //选中行ID
var objSelRowP0606; //选中行对象

//弹出对话框
function showDialogP060601(strType) {

    //保存类型
    strOpratP0606 = strType;

    if (strType == "1") {
        creatDiv_ShowDialog(strDivP0606, "添加科目", true, true, strTableP0606);
    }
    else {
        creatDiv_ShowDialog(strDivP0606, "修改科目", false, true, strTableP0606);
    }

    //添加弹出层内容
    addPopContent();
}

//刷新会计科目
function refreshInfos(obj) {
    if (obj && obj.page && obj.flag) {
        if (obj.page == "ciho" && obj.flag == "1") {
            $("#" + strTableP0606).setGridParam({ treeANode: -1 });
            loadTreeData(strTableP0606, ParseData(window.external.DoOptions(strTableP0606, 'GetList', '{}')).Records);
        }
    }
}

//添加弹出层内容
function addPopContent() {

    //定义变量
    var i = 0;
    var strForm;
    var item;

    //组动态html
    strForm = "<ul>";
    strForm += "<li><font color='red'>*</font>科目代码：<input type='text'  value=''  name='bh' id='bh' /></li>";
    strForm += "<li><font color='red'>*</font>科目名称：<input type='text'  value=''  name='kmmc' id='kmmc' maxlength='50' onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/></li>";
    strForm += "</ul>";

    //往层里添加内容
    $("#" + strDivP0606 + "_divform").append(strForm);

    //弹出对话框
    if (strOpratP0606 == "1") {
        $("#" + strDivP0606 + "_pop_box_top").append("<h3 id='idH3'>所属：" + trim(objSelRowP0606.a3) + "(" + trim(objSelRowP0606.bh) + ")</h3>");
        var strBh = ParseData(window.external.DoOptions(strTableP0606, 'MakeBH', '{"ParentBH":\"' + objSelRowP0606.bh + '\"}'));
        $("#bh", "#" + strDivP0606).val(strBh.BH);
    }
    else {
        $("#bh", "#" + strDivP0606).val(objSelRowP0606.bh);
        $("#kmmc", "#" + strDivP0606).val(objSelRowP0606.a3);
    }

    $("#bh", "#" + strDivP0606).attr("disabled", true);

    //设定状态
    $(":input", $("#" + strDivP0606 + "_divform")).change(function () {
        $("#" + strDivP0606 + "_divform").data("change", true);
    });
    $("#" + strDivP0606 + "_divform").data("change", false);

    setTimeout(function () { $("#kmmc", $("#" + strDivP0606 + "_divform")).focus(); }, 100);
}

//判断该节点是不是预设的特殊节点
function checkIsSpeclNode(strBh) {

    //不能修改
    if (objSelRowP0606.a6 == "False") {
        //不能修改但能新增下一级
        if (
                strBh == "1002" || strBh == "1005" || strBh == "3002" ||
                strBh == "3003" || strBh == "4003" ||
                strBh == "1" || strBh == "2" || strBh == "3" ||
                strBh == "4") {
            return { addNext: true, update: false, setZt: false, delet: false };
        }
        else if (strBh == "5") {
            return { addNext: false, update: false, setZt: false, delet: false };
        }
        return { addNext: false, update: false, setZt: false, delet: false };
    }

    //只能新增二级科目
    if (strBh.toString().substr(0, 1) == "1" || strBh.toString().substr(0, 1) == "3" || strBh.toString().substr(0, 1) == "4") {
        var objTable = $("#" + strTableP0606);
        var objCur = objTable.getRowData(strBh);
        if (objTable.getNodeParent(objCur).bh == "1002" || objTable.getNodeParent(objCur).bh == "3002" || objTable.getNodeParent(objCur).bh == "3003" || objTable.getNodeParent(objCur).bh == "4002") {
            return { addNext: false, update: true, setZt: true, delet: true };
        }
        objTable = null;
    }
    return false;

}

//格式化
function FormatterZtP0606(cellValue) {
    if (cellValue == "False") {
        return "已停用"
    }
    else {
        return "已启用"
    }
}

//点击弹出浮动层
function onSelectP0606(rowId) {

    //选择行
    strSelRowIdP0606 = rowId;
    objSelRowP0606 = $(this).getRowData(rowId);

    //定义变量
    var strButtonHtml = "";
    var i = 0;
    var objTable = $("#" + strTableP0606);
    var objResult;
    var objParentDate = objTable.getNodeParent(objSelRowP0606);
    var objSonNodes = objTable.getNodeChildren(objSelRowP0606);
    objResult = checkIsSpeclNode(objSelRowP0606.bh);

    //判断该节点是否是特殊值
    if (objResult == false) {
        objResult = { addNext: true, update: true, setZt: true, delet: true };
        if (objSelRowP0606.isLeaf == "false") {

            //当前节点是父节点
            var bIsAllStop = true;

            //判断停用启用
            for (i = 0; i < objSonNodes.length; i++) {
                if (objSonNodes[i].zt == "True" || objSonNodes[i].zt == "已启用") {
                    bIsAllStop = false;
                }
            }
            if (bIsAllStop == false) {
                objResult.setZt = false;
            }
            objResult.delet = false;

        }
        else {
            //当前节点子节点
            if (objSelRowP0606.a16 != "0") {

                //有期初值不能新增下一级
                objResult.addNext = false;
                objResult.delet = false;
            }
            if (objParentDate.zt == "True" || objParentDate.zt == "已启用") {
                objResult.setZt = true;
            }
            else {
                objResult.setZt = false;
            }
        }
    }

    //组浮动按钮

    //新增下一级
    if (objResult.addNext == true) {
        if (objSelRowP0606.zt == "已启用" || objSelRowP0606.zt == "True") {
            strButtonHtml = "<a href=\"#\" onclick=\"showDialogP060601('1');return false;\">新增下一级</a>";
        }
        else {
            objResult.addNext = false;
        }
    }

    //修改
    if (objResult.update == true) {
        if (strButtonHtml == "") {
            strButtonHtml = "<a href=\"#\" onclick=\"showDialogP060601('2');return false;\">修改</a>";
        }
        else {
            strButtonHtml += "-<a href=\"#\" onclick=\"showDialogP060601('2');return false;\">修改</a>";
        }
    }

    //设置状态
    if (objResult.setZt == true) {
        if (strButtonHtml == "") {
            if (objSelRowP0606.zt == "已启用" || objSelRowP0606.zt == "True") {
                strButtonHtml = "<a href=\"#\" onclick=\"setZtP0606();return false;\">停用</a>";
            }
            else {
                strButtonHtml = "<a href=\"#\" onclick=\"setZtP0606();return false;\">启用</a>";
            }

        }
        else {
            if (objSelRowP0606.zt == "已启用" || objSelRowP0606.zt == "True") {
                strButtonHtml += "-<a href=\"#\" onclick=\"setZtP0606();return false;\">停用</a>";
            }
            else {
                strButtonHtml += "-<a href=\"#\" onclick=\"setZtP0606();return false;\">启用</a>";
            }

        }

    }

    //删除
    if (objResult.delet == true) {
        if (strButtonHtml == "") {
            strButtonHtml = "<a href=\"#\" onclick=\"deleteDataP0606();return false;\">删除</a>";
        }
        else {
            strButtonHtml += "-<a href=\"#\" onclick=\"deleteDataP0606();return false;\">删除</a>";
        }
    }

    //释放内存
    objTable = null;

    //回调
    if (strButtonHtml != "") {
        createFloatButton.call(this, rowId, strButtonHtml);
    }

}

//双击行
function dbClickP0606(strTemp, rowId) {

    //行ID,行数据
    strSelRowIdP0606 = rowId;
    objSelRowP0606 = $(this).getRowData(rowId);

    if (objSelRowP0606.a6 != "False") {
        if (objSelRowP0606.isLeaf == "false") {
            //父节点的话
            // showDialogP060601("1");
        }
        else {
            //子节点
            showDialogP060601("2");
        }
    }
    else {
        if (
            objSelRowP0606.bh == "1002" || objSelRowP0606.bh == "1005" || objSelRowP0606.bh == "3002" ||
            objSelRowP0606.bh == "3003" || objSelRowP0606.bh == "4002" || objSelRowP0606.bh == "4003"
          ) {
            //showDialogP060601("1");
        }
    }
}

//验证数据
function checkFormP0606(strDialog, strType) {

    //编号，科目名称
    var strBh = trim($("#bh", "#" + strDivP0606).val());
    var strKmmc = trim($("#kmmc", "#" + strDivP0606).val());

    if (!strKmmc) {
        window.parent.cihoAlert(msg.sz.kjkm.NoEmp, null, function () {
            $("#kmmc", "#" + strDivP0606).focus();
        });
        return false;
    }

    //同级不允许重复
    var objResult = ParseData(window.external.DoOptions(strTableP0606, 'DoOthers', "{'bh':'" + strBh + "','kmmc':'" + strKmmc + "'}"));
    if (objResult == true) {
        window.parent.cihoAlert(msg.sz.kjkm.nameQqual, null, function () {
            $("#kmmc", "#" + strDivP0606).focus();
        });
        return false;
    }

}

//保存到数据库
function saveToDbP0606(strDialog, strType) {

    //数据库返回结果
    var objResult;

    //组字符串
    var strData = "{";
    strData += "\"a3\":\"" + trim($("#kmmc", "#" + strDivP0606).val()) + "\"";

    if (strOpratP0606 == "2") {
        strData += ",\"kmdm\":\"" + objSelRowP0606.kmdm + "\"";
    }
    if (strOpratP0606 == "1") {
        strData += ",\"bh\":\"" + trim($("#bh", "#" + strDivP0606).val()) + "\"";
        strData += ",\"parent\":\"" + objSelRowP0606.bh + "\"";
        strData += ",\"level\":\"" + (parseInt(objSelRowP0606.level) + 1).toString() + "\"";
    }
    strData += "}";

    //操作数据库
    if (strOpratP0606 == "1") {
        //新增
        objResult = ParseData(window.external.DoOptions(strTableP0606, 'Add', strData));
    }
    else {
        //修改
        objResult = ParseData(window.external.DoOptions(strTableP0606, 'Edit', strData));
    }

    //数据库操作失败
    if (objResult.ReturnCode != 0) {
        window.parent.cihoAlert(objResult.MSG);
        return false;
    }
    //添加到表格中
    if (strOpratP0606 == "1") {
        $("#" + strTableP0606).addChildNode(objResult.bh, objSelRowP0606.bh, objResult);

        //修改父节点的isLeaf属性
        var objParent = $("#" + strTableP0606).getRowData(objSelRowP0606.bh);
        objParent.isLeaf = "false";
        $("#" + strTableP0606).setRowData(objSelRowP0606.bh, objParent);
    }
    else if (strOpratP0606 == "2") {
        $("#" + strTableP0606).setCell(objSelRowP0606.bh, "a3", objResult.a3);
    }

    //调整滚动条
    resizeGridScroll(strTableP0606);

    if (strType == "2") {
        //新增
        $(":input", "#" + strDivP0606).val("");
        var strBh;
        if (strOpratP0606 == "2") {
            //修改的时候保存新增变为增加同级
            objSelRowP0606 = $("#" + strTableP0606).getNodeParent(objSelRowP0606);
            strBh = ParseData(window.external.DoOptions(strTableP0606, 'MakeBH', '{"ParentBH":\"' + objSelRowP0606.bh + '\"}')).BH;
        }
        else {
            //新增的时候保存新增一直是增加同级
            strBh = ParseData(window.external.DoOptions(strTableP0606, 'MakeBH', '{"ParentBH":\"' + objSelRowP0606.bh + '\"}')).BH;
        }

        $("#bh", "#" + strDivP0606).val(strBh);
        $("#" + strDialog + "_divform").data("change", false);
        $("#kmmc", $("#" + strDialog + "_divform")).focus();
    }
    else {
        //退出
        cihoDlgClose(strDialog);
    }

    //保存之后变为新增
    strOpratP0606 = "1";

    //修改全局设置
    changeComboZt("TKjkm");
    changePageZt("p0606","1");
}

//保存数据
function saveMyData(strDialog, strType, strTable) {

    //验证
    if (checkFormP0606(strDialog, strType) == false) {
        return;
    }

    //保存
    saveToDbP0606(strDialog, strType);
}

//关闭对话框
function closeMyDlg(strDialog, strType) {

    var isTextChange = $("#" + strDialog + "_divform").data("change");

    //当页面信息已修改时，弹出提示保存
    if (isTextChange == true) {
        window.parent.cihoAlert(
            msg.other.haveChange
            , msgBox.dlgBtn.YesNoCancel
            , function (strRet) {
                if (msgBox.dlgRst.Yes == strRet) {
                    saveMyData(strDialog, 0, strTableP0606);
                }
                else if (msgBox.dlgRst.No == strRet) {
                    cihoDlgClose(strDialog);
                }
            }
        );
    }
    else {
        cihoDlgClose(strDialog);
    }
}

//删除
function deleteDataP0606() {
    try {
        //表格对象
        var objTable = $("#" + strTableP0606);

        //取得要删除数据的名称
        var objCurrent = objTable.getRowData(objSelRowP0606.bh);

        if (objTable.getNodeChildren(objCurrent).length != 0) {
            window.parent.cihoAlert(msg.del.haveChild);
            return;
        }
        window.parent.cihoAlert(msg.del.delDatafirst + objSelRowP0606.a3 + msg.del.delDataLast, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                var objParent = $("#" + strTableP0606).getNodeParent(objCurrent);

                var strJsonData = "{\"kmdm\":\"" + objSelRowP0606.kmdm + "\",\"parent\":\"" + objParent.bh + "\"}";
                var objResult = ParseData(window.external.DoOptions(strTableP0606, 'Delete', strJsonData));

                //保存失败时弹出提示信息
                if (objResult.ReturnCode != 0) {
                    window.parent.cihoAlert(objResult.MSG);
                    return;
                }

                $("#" + strTableP0606).jqGrid("delRowData", objSelRowP0606.bh);
                if (objParent && $("#" + strTableP0606).getNodeChildren(objParent).length == 0) {
                    loadTreeData(strTableP0606, ParseData(window.external.DoOptions(strTableP0606, 'GetList', '{}')).Records);
                }
                resizeGridScroll(strTableP0606);

                //修改全局的状态
                changeComboZt("TKjkm");
                changePageZt("p0606","1");
            }
        });

        //释放内存
        objTable = null;
    }
    catch (e) {
        proceError("P0606", e.ToString());
    }
}

//修改状态
function setZtP0606() {

    //数据库操作结果
    var objResult;

    if (objSelRowP0606.zt == "已启用") {
        objResult = updateDBState(strTableP0606, objSelRowP0606.bh, "False", "kmdm");
        if (objResult != null && objResult.ReturnCode == "0") {
            $("#" + strTableP0606).setCell(objSelRowP0606.bh, "zt", "False");
            changeComboZt("TKjkm");
            changePageZt("p0606", "1");
        }

    }
    else {
        objResult = updateDBState(strTableP0606, objSelRowP0606.bh, "True", "kmdm");
        if (objResult != null && objResult.ReturnCode == "0") {
            $("#" + strTableP0606).setCell(objSelRowP0606.bh, "zt", "True");
            changeComboZt("TKjkm");
            changePageZt("p0606", "1");
        }
    }

    //释放内存
    objRow = null;
}


 