/*************************
*功能：应收维护
*创建人：张振华
*时间：2010-08-17
*备注：
*************************/

var strBlData = []; //比较内容
var strBjData;

//往弹出层里添加内容
function tableKcForm(strId) {
    //table对象
    var objTable = $('#' + strTableNameP060701);

    //列配置
    var objNames = objTable.jqGrid('getGridParam', "colNames");
    var objIndexs = objTable.jqGrid('getGridParam', "colModel");

    //商品，库存
    var objSp = getComboboxData('Tspxx');
    var objCk = getComboboxData('TCkxx');
    objCk.push({ "dm": "-1", "mc": "<font color='red'>添加仓库</font>" });
    objSp.push({ "dm": "-1", "mc": "<font color='red'>添加药品</font>" });

    //其他变量
    var sign = "";
    var strForm = "";
    var strAppendTmp = "";

    //生成动态html
    strForm += "<ul>";
    for (var i = 1; i < objNames.length; i++) {
        if (objIndexs[i]["name"] == "c4" || objIndexs[i]["name"] == "c3" || objIndexs[i]["name"] == "c2" || objIndexs[i]["name"] == "b2"
                || objIndexs[i]["name"] == "id" || objIndexs[i]["name"] == "cz" || objIndexs[i]["name"] == "b1" || objIndexs[i]["name"] == "c1"
                || objIndexs[i]["name"] == "jldm") {
            continue;
        }
        //判断是否必填，加样式
        if (objIndexs[i]["editrules"] != null) {
            if (objIndexs[i]["editrules"].required == true) {
                sign = "<font color='red'>*</font>";
            }
            else {
                sign = "";
            }
        }
        //text框判断。
        if (objIndexs[i]["edittype"] == "text") {
            strAppendTmp += "<li>";
            strAppendTmp += sign;
            strAppendTmp += objNames[i] + "：";
            strAppendTmp += "<input type='text' value='' name='" + objIndexs[i]["name"] + "'";
            strAppendTmp += " id='" + objIndexs[i]["name"] + "' maxlength='50'"; //限制输入长度
            strAppendTmp += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>"; //限制输入，粘贴特殊字符
            strAppendTmp += "</li>";
        }
        //下拉框
        else if (objIndexs[i]["edittype"] == "select") {
            strAppendTmp += "<li>";
            strAppendTmp += sign + objNames[i] + "：";
            strAppendTmp += "<select name='" + objIndexs[i]["name"] + "' id='" + objIndexs[i]["name"] + "' ";
            strAppendTmp += " class='easyui-combobox'>";
            strAppendTmp += "</select>";
            strAppendTmp += "</li>";
        }
    }
    strForm += strAppendTmp;
    strForm += "</ul>";

    //添加到层里
    $("#" + strDialogP060701 + "_divform").append(strForm);

    //仓库代码
    $("#ckdm", "#" + strDialogP060701).combobox({
        textField: "mc",
        valueField: "dm",
        editable: true,
        onChange: function (newValue, oldValue) {
            $("#" + strDialogP060701 + "_divform").data("change", true);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "仓库", table: "Tflxx" }, { jquery: $("#ckdm", "#" + strDialogP060701), model: "TCkxx" });
                ciho.showTypeDialog();
            }
        }
    });
    if (objCk != "{}") {
        if (objCk.length == 2) {
            objCk[0].selected = true;
        }
        $("#ckdm", "#" + strDialogP060701).combobox("loadData", objCk);
    }

    //商品代码
    $("#spdm", "#" + strDialogP060701).combobox({
        textField: "mc",
        valueField: "dm",
        editable: true,
        onChange: function (newValue, oldValue) {
            $("#" + strDialogP060701 + "_divform").data("change", true);
            //带出,数量,金额
            if (newValue != undefined && objSp != "{}") {
                for (var j = 0; j < objSp.length; j++) {
                    if (objSp[j].dm == newValue) {
                        $("#c5", "#" + strDialogP060701).val(objSp[j].a8);
                        $("#c6", "#" + strDialogP060701).val(objSp[j].a9);
                    }
                }
            }
            //change结束
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "药品", table: "Tflxx" }, { jquery: $("#spdm", "#" + strDialogP060701), model: "Tspxx" });
                ciho.showTypeDialog();
            }
        }
    });
    if (objSp != "{}") {
        $("#spdm", "#" + strDialogP060701).combobox("loadData", objSp);
    }

    //绑定控件和事件
    $("#a3", "#" + strDialogP060701).blur(function () {
        var strA3 = parseFloat($(this).val());
        var strA4 = parseFloat($("#a4", "#" + strDialogP060701).val());
        if (!isNaN(strA3) && !isNaN(strA4)) {
            $("#a10", "#" + strDialogP060701).val(parseFloat(strA3 * strA4).toFixed(window.parent.strMoneyPlace));
        } else {
            $("#a10", "#" + strDialogP060701).val("");
        }

    });
    $("#a4", "#" + strDialogP060701).blur(function () {
        var strA4 = parseFloat($(this).val());
        var strA3 = parseFloat($("#a3").val());
        if (!isNaN(strA4) && !isNaN(strA3)) {
            $("#a10", "#" + strDialogP060701).val(parseFloat(strA3 * strA4).toFixed(window.parent.strMoneyPlace));
        }
        else {
            $("#a10", "#" + strDialogP060701).val("");
        }

    });
    $("#a3", "#" + strDialogP060701).autoNumeric({ mDec: window.parent.strPricePlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
    $("#a4", "#" + strDialogP060701).autoNumeric({ mDec: window.parent.strCountPlace, aSep: "", aNeg: '-' }).focus(function () { this.select(); }).css("ime-mode", "disabled");
    $("#a10", "#" + strDialogP060701).attr("disabled", true);
    $("#c6", "#" + strDialogP060701).attr("disabled", true);
    $("#c5", "#" + strDialogP060701).attr("disabled", true);

    //修改时，加载value
    if (strSaveSign == '2') {
        for (var i = 0; i < objIndexs.length; i++) {
            var objTr = objTable.getRowData(strId);
            strRcordsP060703 = objTr;                     //修改时保存一下数据集
            if (objIndexs[i].name == "ckdm" && objTr.ckdm != undefined) {
                $("#ckdm", "#" + strDialogP060701).combobox("setValue", objTr.ckdm);
                $("#ckdm", "#" + strDialogP060701).combobox("setText", objTr.b1);
            }
            else if (objIndexs[i].name == "spdm" && objTr.spdm != undefined) {
                $("#spdm", "#" + strDialogP060701).combobox("setValue", objTr.spdm);
                $("#spdm", "#" + strDialogP060701).combobox("setText", objTr.c1);
            }
            else {
                $('#' + objIndexs[i].name, "#" + strDialogP060701 + "_divform").val(objTr[objIndexs[i].name]);
            }
        }
        strBlData.push("spdm");
        strBlData.push("a3");
        strBlData.push("a4");
        strBjData = getCompareData(strTableNameP060701, strBlData);
    }

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

    //设置层序
    $(".layoutpanel").css("z-index", "50000009");

    setTimeout(function () { $("#a4", "#" + strDialogP060701).focus(); }, 100);
    //释放内存
    objTable = null;
    objNames = null;
    objIndexs = null;
}

//获取保存的字符串
function GetSaveStr_Kcqc() {

    //input框的字符串
    var strJsonString = getSaveDataInfo(strDialogP060701);

    //其他控件
    var MyStr = ",\"ckdm\":" + "\"" + $("#ckdm").combobox('getValue') + "\"";
    MyStr += ",\"spdm\":\"" + $("#spdm").combobox('getValue') + "\"";

    strJsonString = strJsonString.replace("}", MyStr);
    strJsonString += ",\"kmdm\":\"" + strKmdmP0607 + "\"";

    if (strSaveSign == "2") {
        var objRow = $("#" + strTableNameP060701).getRowData(strSelRowIdP060701);
        strJsonString += ",\"jldm\":\"" + objRow.jldm + "\"";
    }
    strJsonString += "}"
    return strJsonString;
}

//验证表单
function formDataValidate_Kcqc() {
    //仓库代码
    if (!$("#ckdm").combobox('getValue')) {
        //修改时，验证数据是否是停用的数据
        if (strSaveSign == "2" && $("#ckdm", $("#" + strDialogP060701 + "_divform")).combobox('getText') == strRcordsP060703.b1) {
            $("#ckdm", $("#" + strDialogP060701 + "_divform")).combobox("setValue", strRcordsP060703.ckdm);
            $("#ckdm", $("#" + strDialogP060701 + "_divform")).combobox("setText", strRcordsP060703.b1);
        }
        else {
            window.parent.cihoAlert(msg.sz.ck.empOrNotExit, null, function () { $("#ckdm", "#" + strDialogP060701).combobox("focus"); });
            return false;
        }
    }

    //商品代码
    if (!$("#spdm").combobox('getValue')) {
        //修改时，验证数据是否是停用的数据
        if (strSaveSign == "2" && $("#spdm", $("#" + strDialogP060701 + "_divform")).combobox('getText') == strRcordsP060703.c1) {
            $("#spdm", $("#" + strDialogP060701 + "_divform")).combobox("setValue", strRcordsP060703.spdm);
            $("#spdm", $("#" + strDialogP060701 + "_divform")).combobox("setText", strRcordsP060703.c1);
        }
        else {
            window.parent.cihoAlert(msg.sz.yp.empOrNotExit, null, function () { $("#spdm", "#" + strDialogP060701).combobox("focus"); });
            return false;
        }
    }

    //验证必填项*****只验证药品数量和药品单价
    if (false == dataRequired(strTableNameP060701, strDialogP060701)) {
        return false;
    }
    //行IDs
    //var arrIds = $("#" + strTableNameP060701).jqGrid("getDataIDs");

    //名称不能重复
    //    var strMc = trim($("#a3", "#" + strDialogP060701 + "_divform").val());
    //    for (var i = 0; i < arrIds.length; i++) {

    //        if (strMc == $($("#" + strTableNameP060701).jqGrid("getCell", arrIds[i], "a3")).text() && arrIds[i] != strSelRowIdP060701) {
    //            window.parent.cihoAlert(msg.sz.qc.selectSp);
    //            $("#a3", "#" + strDialogP060701 + "_divform").focus();
    //            return false;
    //        }
    //    }


    //解决不能触发blur计算金额的问题
    var strA4 = parseFloat($("#a4", "#" + strDialogP060701).val());
    var strA5 = parseFloat($("#a3", "#" + strDialogP060701).val());

    //判断单价是否是非数字
    if (isNaN(strA4)) {
        window.parent.cihoAlert(msg.other.slNoEffecTiveNum, null, function () { $("#a4", "#" + strDialogP060701).focus(); });
        return false;
    }

    //重新计算合计
    $("#a10", "#" + strDialogP060701).val(strA4 * strA5);

    //计算金额
    var strA10 = parseFloat($("#a10", "#" + strDialogP060701).val());
    if (!isNaN(strA10)) {
        if (strA10 > window.parent._MaxPerBill) {
            window.parent.cihoAlert(msg.other.moneyOversize);
            return false;
        }
    }

    return true;

}

//保存库存期初
function saveKcqc(strType) {
    //验证
    if (formDataValidate_Kcqc() == false) {
        return;
    }

    //保存字符串
    var strRecord = GetSaveStr_Kcqc();

    //数据库操作结果
    var objResult = saveData(strTableNameP060701, strRecord, strSaveSign);

    //数据库保存失败提示
    if (objResult.ReturnCode != "0") {
        window.parent.cihoAlert(objResult.MSG);
        return;
    }

    //保存成功，前台的处理
    if (objResult.ReturnCode == "0") {
        var strTable = "#" + strTableNameP060701;
        var objTable = $(strTable);

        if (strSaveSign == "1") {
            //新增
            objTable.addRowData(objResult.id, ParseData(strRecord));
            objTable.setCell(objResult.id, "jldm", objResult.jldm);
        }
        else {
            //保存
            objTable.setRowData(objResult.id, ParseData(strRecord));
        }

        //操作列
        addCzHtmlP060701();

        //设置仓库名称，商品名称
        objTable.jqGrid("setCell", objResult.id, "b1", $("#ckdm").combobox('getText'));
        objTable.jqGrid("setCell", objResult.id, "c1", $("#spdm").combobox('getText'));

        //修改的时候计算成本
        if (strSaveSign == "2") {
            var strNewData = getCompareData(strTableNameP060701, strBlData);
            if (strBjData != strNewData) {
                window.external.DoOptions(strTableNameP060701, "DoOthers", "{}");
            }
        }

        //保存之后
        if (strType == "2") {

            //保存新增
            $(":input", $("#" + strDialogP060701 + "_divform")).val("");
            $("#spdm", "#" + strDialogP060701).combobox("setValue", "");
            var objCk = $("#ckdm", "#" + strDialogP060701).combobox("getData");
            if (objCk.length == 2) {
                $("#ckdm", "#" + strDialogP060701).combobox('setValue', objCk[0].dm);
            }
            else {
                $("#ckdm", "#" + strDialogP060701).combobox('setValue', '');
            }
            strSaveSign = "1";
            setTimeout(function () { $("#a4", "#" + strDialogP060701).focus(); }, 100);
        }
        else {

            //保存退出
            ChangeValue();
            cihoDlgClose(strDialogP060701);
        }

        //改变层的change状态
        $("#" + strDialogP060701 + "_divform").data("change", false);

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

        //释放内存
        strTable = null;
        objTable = null;
    }

}

//删除
function deletekcRow(rowId, strTableName, kmdm) {

    //取得要删除的数据
    var objCurrent = $("#" + strTableNameP060701).getRowData(rowId);

    window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
        if (result == msgBox.dlgRst.Yes) {

            //点击的改行
            var objTr = $("#" + strTableNameP060701).getRowData(rowId);

            //要删除的数据
            var strJsonData = "{\"kmdm\":\"" + strKmdmP0607 + "\",\"jldm\":\"" + objTr.jldm + "\"}";

            //删除数据库中的数据
            var objResult = ParseData(window.external.DoOptions(strTableNameP060701, 'Delete', strJsonData));

            //保存失败时弹出提示信息
            if (objResult == null || objResult.ReturnCode == undefined) {
                window.parent.cihoAlert(msg.del.delFail);
                return;
            }

            if (objResult.ReturnCode != 0) {
                window.parent.cihoAlert(objResult.MSG);
                return;
            }

            //前台的删除
            $("#" + strTableNameP060701).jqGrid("delRowData", rowId);

            //重新计算合计
            GetTotal();

            //清除浮动层的内容
            $(".operate").remove();

            //调整表格
            resizeGridScroll(strTableNameP060701);
        }
    });
}
