/*************************
*功能：采购退货单
*创建人：张振华
*时间：2010-08-15
*备注：此时间是加注释时间
*************************/

//层div，tableID。
//表示是新增修改。
//总的记录数，保存要删除的代码。
//行索引，列索引。
//修改时候的记录，要比较的数据。
var strDivP010102 = "divP010102";
var strTableTp010102 = "Tp010102";
var strOptrTypeP010102 = "1";
var strRecordsP010102;
var strDeletHtml = "";
var iRowP010102 = -1;
var iColP010102 = -1;
var objRecords;
var objCompDataP010102 = [];
var iFristIndexP010102 = 999999999;

//下拉框自定义元素
function my_inputP010102(value, options) {
    var strHtml = "<select class=\"easyui-combobox\" name=\"" + options.name + "\"  id=\"" + options.id + "\">";
    return $(strHtml);
}

//下拉框取值
function my_valueP010102(ele) {
    return ele.combobox('getText');
}

//格式化数量
function formatterP010102Count(cellValue) {

    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化单价
function formatterP010102Price(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strPricePlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化金额
function formatterP010102Money(cellValue) {

    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化是否带票
function formatterP010102A3(cellValue) {
    if (cellValue == "False" || cellValue == "否") {
        return "否";
    }
    else if (cellValue == "True" || cellValue == "是") {
        return "是";
    }
    else {
        return "&nbsp;"
    }
}

//改变状态
function ChangeZtP010102(rowId) {
    var objTable = $("#" + strTableTp010102);
    if (objTable.getCell(rowId, "mxguid") == "") {
        objTable.setCell(rowId, "flag", "0");

    }
    objTable = null;
}

//修改Data change状态
function dataChangeStateP010102(bState) {
    $("#" + strDivP010102 + "_divform").data("change", bState);
}

//创建树形
function createGridP010102(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: [],
        rownumbers: true,
        rowNum: 9999,
        sortname: "id",
        autowidth: true,
        //footerrow: true,
        cellEdit: true,
        cellsubmit: "clientArray",
        gridview: true,
        rownumWidth: 30
    }).parent(".ui-jqgrid-bdiv").siblings();

    //增加表头‘序号’列
    $("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
}

//点头部表格添加空行
function AddEmptyRowP010102() {
    for (var i = iFristIndexP010102; i < iFristIndexP010102 + strRecordsP010102; i++) {
        $("#" + strTableTp010102).addRowData(i, { flag: "" });
    }
}

//添加一行数据
function addNewRowP010102() {
    $("#" + strTableTp010102).addRowData(++strRecordsP010102 + iFristIndexP010102, { id: strRecordsP010102, cz: "" });
    AddCzHtmlP010102();
}

//计算合计
function GetTotalP010102() {

    var objTable = $("#" + strTableTp010102);

    var arrIds = objTable.getDataIDs();
    var strTotal = 0;

    for (var i = 0; i < arrIds.length; i++) {
        var strTemp = parseFloat(objTable.getCell(arrIds[i], "b3"));
        if (!isNaN(strTemp)) {
            strTotal += strTemp;
        }
    }
    objTable.footerData("set", { rn: "合计", b3: parseFloat(strTotal).toFixed(window.parent.strPricePlace) });
    objTable = null;
    arrIds = null;
}

//表格添加删除按钮
function AddCzHtmlP010102() {

    $("#" + strTableTp010102 + " tr:visible").hover(function () {
        var strButtonHtml = "";
        strButtonHtml += "<a  style=\"color:red;text-decoration:none;cursor:pointer;\" onclick=\"deleteData_P010102(" + this.id + ");return false;\">删除</a> ";
        $("#" + strTableTp010102).jqGrid("setCell", this.id, "cz", strButtonHtml);
    }, function () {
        $("#" + strTableTp010102).jqGrid("setCell", this.id, "cz", "&nbsp;");
    });
}

//根据开票单位获得开票税率
function getdkslbyKpdw(strKpdwdm) {

    //开票单位
    var objKpxx = window.parent._arrCmbKpdw;

    //根据开票单位查找开票税率
    for (var i = 0; i < objKpxx.Records.length; i++) {
        if (objKpxx.Records[i].dm == strKpdwdm) {
            return { dksl: objKpxx.Records[i].dksl, mc: objKpxx.Records[i].mc };
        }
    }

}

//释放内存
function freeMemory_P010102() {
    //strOptrTypeP010102 = null;
    iRowP010102 = null;
    iColP010102 = null;
    objRecords = null;
    objCompDataP010102 = null;
}

//strType:新增(1),点击修改弹出(2),退货(3)
function showDialog_P010102(strType) {

    try {
        //清除浮动层
        $("div .operate").hide();

        //存操作标志位
        strOptrTypeP010102 = strType;

        //弹出对话框空层
        creatDiv_ShowDlgHaveTbl(strDivP010102, "采购退货单", false, true, strTableTp010102, "900");

        //往对话框中添加数据
        if (strOptrTypeP010102 == "1") {
            creatDialogItem_P010102("");
        }
        else if (strOptrTypeP010102 == "2") {
            $("#idMust", "#" + strDivP010102).append("NO:" + objSelRowP0101.djbh);
            creatDialogItem_P010102(objSelRowP0101.djbh);
        }
        else if (strOptrTypeP010102 == "3") {
            creatDialogItem_P010102(objSelRowP0101.djbh);
        }
    }
    catch (e) {
        proceError("P010102", e.ToString());
    }
}

//往弹出层里添加内容
function creatDialogItem_P010102(strDjbh) {

    //取列配置
    var config = ParseData(window.external.DoOptions(strTableTp010102, "GetTableConfig", "{}"));

    //根据单据编号取数据
    objRecords = { Records: [] };
    if (strDjbh != "") {
        objRecords = ParseData(window.external.DoOptions(strTableTp010102, 'GetList', '{djbh:' + strDjbh + ' }'));
    }

    //常用变量定义
    var i = 0;
    var strForm = "";
    var Item;

    //取下拉框的值
    var objGys = getComboboxData('Tgysxx');
    objGys.push({ "mc": "<font color='red'>添加供应商</font>", "dm": "-1" });
    var objZy = getComboboxData('TZyxx');
    objZy.push({ "mc": "<font color='red'>添加经手人</font>", "dm": "-1" });
    var objCk = getComboboxData('TCkxx');
    objCk.push({ "mc": "<font color='red'>添加仓库</font>", "dm": "-1" });
    var objKmdm = getComboboxData('TKjkm');
    objKmdm.push({ "dm": "-1", "mc": "<font color='red'>添加账户</font>" });

    //创建头部，表格
    strForm += "<ul>";
    for (i = 0; i < config.pageHeader.length; i++) {

        Item = config.pageHeader[i];
        if (Item.hidden == true) {
            continue;
        }
        if (Item.edittype == "text") {
            strForm += "<li class='inp_70'>";
            strForm += Item.xsmc + "：";
            strForm += "<input type='text'  name='" + Item.index + "' ";
            strForm += " value=''  ";
            strForm += " id='" + Item.index + "'";
            strForm += " maxlength='50'   onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>";
            strForm += "</li>";
        }
        else if (Item.edittype == "select") {
            strForm += "<li>";
            strForm += Item.xsmc + "：";
            strForm += "<select name='" + Item.index + "' id='" + Item.index + "'";
            strForm += " class='easyui-combobox' >";
            strForm += "</select>";
            strForm += "</li>";
        }

    }
    strForm += "</ul>";
    strForm += "<table id=" + strTableTp010102 + "></table>";
    $("#" + strDivP010102 + "_divform").append(strForm);

    //供应商赋值
    $("#gysdm", "#" + strDivP010102).combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth120,
        listWidth: _iWidth120,
        onChange: function (newValue, oldValue) {
            dataChangeStateP010102(true);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "供应商", table: "Tflxx" }, { jquery: $("#gysdm", "#" + strDivP010102), model: "Tgysxx" });
                ciho.showTypeDialog();
            }
        }
    });
    if (objGys != "{}") {
        $("#gysdm", "#" + strDivP010102).combobox("loadData", objGys);
    }

    //职员赋值
    $("#zydm", "#" + strDivP010102).combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth120,
        listWidth: _iWidth120,
        onChange: function (newValue, oldValue) {
            dataChangeStateP010102(true);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "职员", table: "Tflxx" }, { jquery: $("#zydm", "#" + strDivP010102), model: "TZyxx" });
                ciho.showTypeDialog();
            }
        }
    });
    if (objZy != "{}") {
        $("#zydm", "#" + strDivP010102).combobox("loadData", objZy);
    }

    //仓库赋值
    $("#ckdm", "#" + strDivP010102).combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth120,
        listWidth: _iWidth120,
        onChange: function (newValue, oldValue) {
            dataChangeStateP010102(true);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "仓库", table: "Tflxx" }, { jquery: $("#ckdm", "#" + strDivP010102), model: "TCkxx" });
                ciho.showTypeDialog();
            }
        }
    });
    if (objCk != "{}") {
        if (objCk.length == 2) {
            objCk[0].selected = true;
        }
        $("#ckdm", "#" + strDivP010102).combobox("loadData", objCk);
    }

    //给单据日期绑定控件和赋默认值
    $("#djdate", "#" + strDivP010102).val(getNowDate());
    inputAndBindData("djdate", null, "#" + strDivP010102);
    $("#djdate", "#" + strDivP010102).blur(function () {
        $("#" + strDivP010102 + "_divform").data("change", true);
    });

    //加载表格列配置
    var objTable = $("#" + strTableTp010102);
    createGridP010102(strTableTp010102, config.colNames, config.colModel);
    $("#" + strTableTp010102).setGridHeight(46);

    //设置表格属性
    objTable.setColProp("b1", { formatter: formatterP010102Count });
    objTable.setColProp("b2", { formatter: formatterP010102Price });
    objTable.setColProp("b3", { formatter: formatterP010102Money });
    objTable.setColProp("a3", { formatter: formatterP010102A3 });
    objTable.setColProp("tpsl", { formatter: formatterP010102Count });
    objTable.setColProp("tpdj", { formatter: formatterP010102Price });
    objTable.setColProp("tpje", { formatter: formatterP010102Money });
    objTable.setColProp("spmc", { edittype: "custom" });
    objTable.setColProp("spmc", { editoptions: { custom_element: my_inputP010102, custom_value: my_valueP010102} });
    objTable.setColProp("kpdwmc", { edittype: "custom" });
    objTable.setColProp("kpdwmc", { editoptions: { custom_element: my_inputP010102, custom_value: my_valueP010102} });
    objTable.setColProp("a3", { edittype: "custom" });
    objTable.setColProp("a3", { editoptions: { custom_element: my_inputP010102, custom_value: my_valueP010102} });

    //给表格加事件
    objTable.setGridParam({ afterEditCell: afterEditCell_P010102 });
    objTable.setGridParam({ afterSaveCell: afterSaveCell_P010102 });

    //加载数据
    if (objRecords.Records == "{}") {
        objRecords.Records = [];
    }
    strRecordsP010102 = 1 - objRecords.Records.length;

    loadTreeData(strTableTp010102, objRecords.Records);

    resizeGridScroll(strTableTp010102);
    //计算合计
    //GetTotalP010102();

    //不足五行补齐空行
    if (strRecordsP010102 <= 1) {
        AddEmptyRowP010102();
    }

    //添加操作
    // AddCzHtmlP010102();

    //头部添加行按钮
    //$(".ui-jqgrid-htable th:visible:last").html('<img id="btnAddP010102"  style="cursor:pointer"  title="添加行" src="../../Content/Default/images/edit_add.gif" onclick="addNewRowP010102() "/>');

    //底部开始
    strForm = "<ul>"
    for (i = 0; i < config.pageFooter.length; i++) {
        Item = config.pageFooter[i];
        if (Item.hidden == true) {
            continue;
        }
        if (Item.edittype == "text") {
            if (config.pageFooter[i]["name"] == "a9") {
                strForm += "<li class='bz'>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' ";
                strForm += " value=''";
                strForm += " id='" + Item.index + "' onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>";
                strForm += "</li>";
            }
            else {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' ";
                strForm += " value=''";
                strForm += " id='" + Item.index + "' onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>";
                strForm += "</li>";
            }
        }
        else if (Item.edittype == "select") {
            strForm += "<li>";
            strForm += Item.xsmc + "：";
            strForm += "<select name='" + Item.index + "' id='" + Item.index + "'";
            strForm += " class='easyui-combobox' >";
            strForm += "</select>";
            strForm += "</li>";
        }
    }
    strForm += "</ul>";
    $("#" + strDivP010102 + "_divform").append(strForm);

    //设置底部日期，默认值，事件
    inputAndBindData("sfkdate", null, "#" + strDivP010102);
    $("#sfkdate", "#" + strDivP010102).blur(function () {
        $("#" + strDivP010102 + "_divform").data("change", true);
    });

    //制单人赋值
    $("#czydm", "#" + strDivP010102).val(window.parent.objLoginInfo.name);
    $("#czydm", "#" + strDivP010102).attr("disabled", true);

    //金额赋值
    $("#a5", "#" + strDivP010102).autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");

    //设置会计科目的值
    $("#kmdm", "#" + strDivP010102).combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth120,
        listWidth: _iWidth120,
        panelHeight: getComHeight(objKmdm),
        onChange: function (newValue, oldValue) {
            dataChangeStateP010102(true);
        },
        onSelect: function (strRecord) {
            if (strRecord.dm == "-1") {
                return;
            }
            //带出金额
            if ($("#a5", "#" + strDivP010102).val() == "") {
                var objIDs = $("#" + strTableTp010102).getDataIDs();
                var strB3 = $("#" + strTableTp010102).getCell(objIDs[0], "b3");

                if (strB3) {
                    $("#a5", "#" + strDivP010102).val(strB3);
                }
                else {
                    $("#a5", "#" + strDivP010102).val(parseFloat(0).toFixed(window.parent.strMoneyPlace));
                }
            }
            //带出付款日期
            var strData = $("#djdate", "#" + strDivP010102).val();
            if (checkDatePattern(strData) == false) {
                strData = "";
            }
            $("#sfkdate", "#" + strDivP010102).val(strData);
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "账户", table: "Tflxx" }, { jquery: $("#kmdm", "#" + strDivP010102), model: "Tkjkm" });
                ciho.showTypeDialog();
            }
        }
    });
    if (objKmdm != "{}") {
        $("#kmdm", "#" + strDivP010102).combobox("loadData", objKmdm);
    }

    //加载默认数据：2是修改的时候，3是点退货进来
    if (strOptrTypeP010102 == "2") {
        $("#djdate", "#" + strDivP010102).attr("disabled", true);
        $("#djdate", "#" + strDivP010102).val(objRecords.djdate);
        $("#kmdm", "#" + strDivP010102).combobox('setValue', objRecords.kmdm);
        if ($("#kmdm", "#" + strDivP010102).combobox('getValue') == objRecords.kmdm) {
            $("#kmdm", "#" + strDivP010102).combobox('setText', objRecords.zhmc);
        }

        if (!isNaN(parseFloat(objRecords.a5))) {
            $("#a5", "#" + strDivP010102).val(parseFloat(objRecords.a5).toFixed(window.parent.strMoneyPlace));
        }
        else {
            $("#a5", "#" + strDivP010102).val(objRecords.a5);
        }
        $("#sfkdate", "#" + strDivP010102).val(objRecords.sfkdate);
        $("#a9", "#" + strDivP010102).val(escape(objRecords.a9));
        $("#czydm", "#" + strDivP010102).val(objRecords.czymc);

    }
    else if (strOptrTypeP010102 == "3") {
        $("#djdate", "#" + strDivP010102).attr("disabled", false);
        $("#djdate", "#" + strDivP010102).val(getNowDate());
    }
    if (strOptrTypeP010102 == "2" || strOptrTypeP010102 == "3") {

        $("#gysdm", "#" + strDivP010102).combobox('setValue', objRecords.gysdm);
        if ($("#gysdm", "#" + strDivP010102).combobox('getValue') == objRecords.gysdm) {
            $("#gysdm", "#" + strDivP010102).combobox('setText', objRecords.gysmc);
        }
        $("#zydm", "#" + strDivP010102).combobox('setValue', objRecords.zydm);
        if ($("#zydm", "#" + strDivP010102).combobox('getValue') == objRecords.zydm) {
            $("#zydm", "#" + strDivP010102).combobox('setText', objRecords.zymc);
        }
        $("#ckdm", "#" + strDivP010102).combobox('setValue', objRecords.ckdm);
        if ($("#ckdm", "#" + strDivP010102).combobox('getValue') == objRecords.ckdm) {
            $("#ckdm", "#" + strDivP010102).combobox('setText', objRecords.ckmc);
        }

        //记录原始值
        var strData = [];
        strData.push("spdm");
        strData.push("b1");
        strData.push("b2");

        objCompDataP010102 = getCompareData(strTableTp010102, strData);
    }

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

    //下拉框的层序
    $(".layoutpanel").css("z-index", "50000009");

    setTimeout(function () { $("#a5", "#" + strDivP010102).focus(); }, 100);
    //释放内存
    config = null;
    objGys = null;
    objZy = null;
    objKmdm = null;
    objCk = null;
    objTable = null;
}

//单元格编辑事件
function afterEditCell_P010102(rowId, cellname, value, iRow, iCol) {

    //行索引，列索引
    iRowP010102 = iRow;
    iColP010102 = iCol;

    //table对象，编辑的Item对象
    var item = $("#" + rowId + " :input[name='" + cellname + "']", this);
    var objTable = $("#" + strTableTp010102);

    switch (cellname) {
        case "spmc":
            //药品信息，药品代码
            var objSp = getComboboxData('Tspxx');
            var strSpmc = objTable.getCell(rowId, "spdm");
            item.combobox({
                valueField: "dm",
                textField: "mc",
                width: _iWidth80
            });
            objSp.push({ "mc": "<font color='red'>添加药品</font>", "dm": "-1" });
            if (objSp != "{}") {
                item.combobox("loadData", objSp);
            }
            item.combobox("setValue", strSpmc);
            if (item.combobox("getText") == strSpmc) {
                item.combobox('setText', value);
            }
            item.combobox({
                onChange: function (newValue, oldValue) {

                    //下拉框状态改变时，改变改行的状态
                    dataChangeStateP010102(true);
                    ChangeZtP010102(rowId);
                    objTable.setCell(rowId, "a3", "否");

                    //选择药品的dm,存到dm列。选择错误则清空代码列
                    if (newValue != undefined && newValue != "-1") {
                        objTable.setCell(rowId, "spdm", newValue);
                    }
                    else {
                        objTable.setCell(rowId, "spdm", "&nbsp;");
                    }

                },
                afterChange: function (newValue, newText) {
                    if (newValue == "-1") {
                        ciho.IniData({ id: "divAddType", title: "药品", table: "Tflxx" }, { jquery: $(this), model: "Tspxx" });
                        ciho.showTypeDialog();
                    }
                }
            });
            item.combobox('focus');

            //释放内存
            objSp = null;

            break;
        case "kpdwmc":
            //开票单位信息，开票单位代码
            if (objTable.getCell(rowId, "a3") == "否" || objTable.getCell(rowId, "a3") == "") {
                objTable.restoreCell(iRow, iCol);
                return;
            }
            var objKpdw = getComboboxData('Tkpdwxx');
            var strKpdwmc = $("#" + strTableTp010102).getCell(rowId, "kpdwdm");
            objKpdw.push({ "dm": "-1", "mc": "<font color='red'>添加收票单位</font>" });
            item.combobox({
                valueField: "dm",
                textField: "mc",
                width: 80
            });
            if (objKpdw != "{}") {
                item.combobox("loadData", objKpdw);
            }
            item.combobox("setValue", strKpdwmc);
            if (item.combobox("getText") == strKpdwmc) {
                item.combobox('setText', value);
            }
            item.combobox({
                onChange: function (newValue, oldValue) {
                    ChangeZtP010102(rowId);
                    dataChangeStateP010102(true);
                    if (newValue != undefined && newValue != "-1" && newValue != "") {
                        objTable.setCell(rowId, "kpdwdm", newValue);
                        objTable.setCell(rowId, "fpsl", getdkslbyKpdw(newValue).dksl);
                    }
                    else {
                        objTable.setCell(rowId, "kpdwdm", "&nbsp;");
                        objTable.setCell(rowId, "fpsl", "&nbsp;");
                    }
                },
                afterChange: function (newValue, newText) {
                    $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
                    if (newValue == "-1") {
                        ciho.IniData({ id: "divAddType", title: "收票单位", table: "Tflxx" }, { jquery: $(this), model: "Tkpdwxx" });
                        ciho.showTypeDialog();
                    }
                }
            });
            item.combobox('focus');

            //释放内存
            objKpdw = null;

            break;
        case "a3":
            item.combobox({
                width: _iWidth80,
                panelHeight: 42,
                valueField: "dm",
                textField: "mc",
                editable: false
            });
            item.combobox("loadData", [{ dm: "是", mc: "是" }, { dm: "否", mc: "否"}]);
            item.combobox('setValue', value);
            item.combobox({
                onChange: function (newValue, oldValue) {
                    ChangeZtP010102(rowId);
                    dataChangeStateP010102(true);
                    if (newValue != undefined && newValue == "是") {
                        var objTr = objTable.getRowData(rowId);

                        if (objTable.getCell(rowId, "tpsl") == "") {
                            objTable.setCell(rowId, "tpsl", objTr.b1, { color: "Black" });
                        }
                        else {
                            objTable.setCell(rowId, "tpsl", "", { color: "Black" });
                        }

                        if (objTable.getCell(rowId, "tpdj") == "") {
                            objTable.setCell(rowId, "tpdj", objTr.b2, { color: "Black" });
                        }
                        else {
                            objTable.setCell(rowId, "tpdj", "", { color: "Black" });
                        }

                        if (objTable.getCell(rowId, "tpje") == "") {
                            objTable.setCell(rowId, "tpje", objTr.b3, { color: "Black" });
                        }
                        else {
                            objTable.setCell(rowId, "tpje", "", { color: "Black" });
                        }
                        //GetTotalP010102();
                    }
                    else {
                        objTable.setCell(rowId, "tpsl", "&nbsp;");
                        objTable.setCell(rowId, "tpdj", "&nbsp;");
                        objTable.setCell(rowId, "tpje", "&nbsp;");
                        objTable.setCell(rowId, "kpdwdm", "&nbsp;");
                        objTable.setCell(rowId, "kpdwmc", "&nbsp;");
                        objTable.setCell(rowId, "dksl", "&nbsp;");
                    }
                    dataChangeStateP010102(true);
                    ChangeZtP010102(rowId);
                }
            });
            item.combobox('focus');
            break;
        case "b1":
            item.autoNumeric({ mDec: window.parent.strCountPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
            break;
        case "b2":
            item.autoNumeric({ mDec: window.parent.strPricePlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
            break;
        case "tpsl":
            //是否退票为否时禁止编辑单元格
            if (objTable.getCell(rowId, "a3") == "否" || objTable.getCell(rowId, "a3") == "") {
                objTable.restoreCell(iRow, iCol);
                return;
            }
            item.autoNumeric({ mDec: window.parent.strCountPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
            break;
        case "tpdj":
            //是否退票为否时禁止编辑单元格
            if (objTable.getCell(rowId, "a3") == "否" || objTable.getCell(rowId, "a3") == "") {
                objTable.restoreCell(iRow, iCol);
                return;
            }
            item.autoNumeric({ mDec: window.parent.strPricePlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
            break;
        case "tpje":
            //是否退票为否时禁止编辑单元格
            if (objTable.getCell(rowId, "a3") == "否" || objTable.getCell(rowId, "a3") == "") {
                objTable.restoreCell(iRow, iCol);
                return;
            }
            item.autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
            break;
        default:

    }

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

    //给编辑的单元格里面的text添加change
    item.change(function () { dataChangeStateP010102(true); ChangeZtP010102(rowId); });

    //释放内存
    item = null;
}

//单元格保存事件
function afterSaveCell_P010102(rowid, cellname, value, iRow, iCol) {

    //table对象
    var objTable = $("#" + strTableTp010102);

    switch (cellname) {
        case "b1":
            //根据退货数量，退货单价计算金额
            var strB1 = parseFloat(objTable.getCell(rowid, "b1"));
            var strB2 = parseFloat(objTable.getCell(rowid, "b2"));
            if (!isNaN(strB1) && !isNaN(strB2)) {
                objTable.setCell(rowid, "b3", parseFloat(strB1 * strB2).toFixed(window.parent.strMoneyPlace));
            }
            else {
                objTable.setCell(rowid, "b3", "&nbsp;");
            }
            break;
        case "b2":
            //根据退货数量，退货单价计算金额
            var strB1 = parseFloat(objTable.getCell(rowid, "b1"));
            var strB2 = parseFloat(objTable.getCell(rowid, "b2"));
            if (!isNaN(strB1) && !isNaN(strB2)) {
                objTable.setCell(rowid, "b3", parseFloat(strB1 * strB2).toFixed(window.parent.strMoneyPlace));
            }
            else {
                objTable.setCell(rowid, "b3", "&nbsp;");
            }
            break;
        case "tpsl":
            //根据退票数量，退票单价计算金额
            var strTpsl = parseFloat(objTable.getCell(rowid, "tpsl"));
            var strTpdj = parseFloat(objTable.getCell(rowid, "tpdj"));
            if (!isNaN(strTpsl) && !isNaN(strTpdj)) {
                objTable.setCell(rowid, "tpje", parseFloat(strTpsl * strTpdj).toFixed(window.parent.strMoneyPlace));
            }
            else {
                objTable.setCell(rowid, "tpje", "&nbsp;");
            }
            break;
        case "tpdj":
            //根据退票数量，退票单价计算金额
            var strTpsl = parseFloat(objTable.getCell(rowid, "tpsl"));
            var strTpdj = parseFloat(objTable.getCell(rowid, "tpdj"));
            if (!isNaN(strTpsl) && !isNaN(strTpdj)) {
                objTable.setCell(rowid, "tpje", parseFloat(strTpsl * strTpdj).toFixed(window.parent.strMoneyPlace));
            }
            else {
                objTable.setCell(rowid, "tpje", "&nbsp;");
            }
            break;
        default:

    }

    //计算合计
    //GetTotalP010102();

    //释放内存
    objTable = null;
}

//删除数据
function deleteData_P010102(strID) {
    try {

        //删除的那行的数据
        var objTr = $("#" + strTableTp010102).getRowData(strID);

        //保存时取消单元格编辑
        $("#" + strTableTp010102).saveCell(iRowP010102, iColP010102);

        //判断改行是否有数据
        if (checkEmptyRow(objTr, "glsl,cz") == false) {

            //判断关联数量
            if (objTr.glsl && objTr.glsl != "0") {
                window.parent.cihoAlert(msg.del.delForbid);
                return;
            }

            //删除时提示
            window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (strRet) {
                if (msgBox.dlgRst.Yes == strRet) {
                    if (strOptrTypeP010102 == "2" && objTr.mxguid != "") {

                        //修改的时候
                        strDeletHtml += ",{\"mxguid\":\"" + objTr.mxguid + "\",";
                        strDeletHtml += "\"flag\":\"2\"}";

                        $("#" + strTableTp010102).delRowData(strID);
                    }
                    else {
                        //新增的时候
                        $("#" + strTableTp010102).delRowData(strID);

                    }

                    //重新计算合计
                    //GetTotalP010102();
                    return;
                }
            });
        }
        else {
            $("#" + strTableTp010102).delRowData(strID);
        }

        //有滚动条时改变表格宽度
        resizeGrid(strTableP0101);
    }
    catch (e) {
        proceError("P010102", e.ToString());
    }
}

//保存数据到数据库
function saveDataP010102(dialog, save) {

    //表格对象，行ID的数组，科目，金额，付款日期
    var objTable = $("#" + strTableTp010102);
    var arrIds = objTable.getDataIDs();
    var strKmdm = $("#kmdm", "#" + strDivP010102).combobox('getValue') + "";
    var strA5 = $("#a5", "#" + strDivP010102).val();
    var strSfkdate = $("#sfkdate", "#" + strDivP010102).val();
    var objNewData = [];

    //头部
    var strData = "{";
    strData += "\"djdate\":\"" + $("#djdate", "#" + strDivP010102).val() + "\"";

    //供应商数据
    var strGysValue = $("#gysdm", "#" + strDivP010102).combobox('getValue');
    if (!strGysValue) {
        if ((strOptrTypeP010102 == "2" || strOptrTypeP010102 == "3") && objRecords.Records != "{}" && $("#gysdm", "#" + strDivP010102).combobox('getText') == objRecords.Records[0].gysmc) {
            strGysValue = objRecords.Records[0].gysdm;
        }
    }
    strData += ",\"gysdm\":\"" + strGysValue + "\"";

    //经手人数据
    var strZy = $("#zydm", "#" + strDivP010102).combobox('getValue');
    if (!strZy) {
        if ((strOptrTypeP010102 == "2" || strOptrTypeP010102 == "3") && objRecords.Records != "{}" && $("#zydm", "#" + strDivP010102).combobox('getText') == objRecords.Records[0].zymc) {
            strZy = objRecords.Records[0].zydm;
        }
    }
    strData += ",\"zydm\":\"" + strZy + "\"";

    //仓库代码
    var strCk = $("#ckdm", "#" + strDivP010102).combobox('getValue');
    if (!strCk) {
        if ((strOptrTypeP010102 == "2" || strOptrTypeP010102 == "3") && objRecords.Records != "{}" && $("#ckdm", "#" + strDivP010102).combobox('getText') == objRecords.Records[0].ckmc) {
            strCk = objRecords.Records[0].ckdm;
        }
    }
    strData += ",\"ckdm\":\"" + strCk + "\"";

    //单据编号
    if (strOptrTypeP010102 == "2" || strOptrTypeP010102 == "3") {
        strData += ",\"djbh\":\"" + objSelRowP0101.djbh + "\"";
    }

    //表格的数据
    strData += ",Records:["
    for (var i = 0; i < arrIds.length; i++) {
        var objTr = objTable.getRowData(arrIds[i]);
        if (!objTr.flag) {
            continue;
        }
        if (checkEmptyRow(objTr, "flag,mxguid,glsl,fpsl")) {
            if (strOptrTypeP010102 == "2" && strDeletHtml.indexOf(objTr.mxguid) == -1) {
                if (strDeletHtml != null && strDeletHtml != "") {
                    strDeletHtml += ",{\"mxguid\":\"" + objTr.mxguid + "\",\"flag\":\"2\"}";
                }
                else {
                    strDeletHtml = ",{\"mxguid\":\"" + objTr.mxguid + "\",\"flag\":\"2\"}";
                }
            }
            continue;
        }
        var alterMsg = "";
        strData += "{";
        if (strOptrTypeP010102 == "2" && objTr.flag == "1") {
            strData += "\"mxguid\":\"" + objTr.mxguid + "\",";
        }
        strData += "\"spdm\":\"" + objTr.spdm + "\",";
        strData += "\"a1\":\"" + objTr.b1 + "\",";
        strData += "\"a2\":\"" + objTr.b2 + "\",";
        if (objTr.a3 == "是") {
            strData += "\"a3\":\"true\",";
            strData += "\"tpsl\":\"" + objTr.tpsl + "\",";
            strData += "\"tpdj\":\"" + objTr.tpdj + "\",";
            strData += "\"kpdwdm\":\"" + objTr.kpdwdm + "\",";
            strData += "\"fpsl\":\"" + objTr.fpsl + "\",";
            strData += "\"tpje\":\"" + objTr.tpje + "\",";
        } else {
            strData += "\"a3\":\"false\",";
        }
        strData += "\"flag\":\"" + objTr.flag + "\"";
        strData += "},";
    }

    //替换掉，
    if (strData.substring(strData.length - 1, strData.length) != "[") {
        strData = strData.substr(0, strData.length - 1);
    }

    //要删除的数据
    if (strDeletHtml != null) {
        strData += strDeletHtml;
    }
    strData += "]"

    //付款账户
    if (!strKmdm) {
        if ((strOptrTypeP010102 == "2" || strOptrTypeP010102 == "3") && objRecords.Records != "{}" && $("#kmdm", "#" + strDivP010102).combobox('getText') == objRecords.Records[0].zhmc) {
            strKmdm = objRecords.Records[0].kmdm;
        }
    }

    //收款账户，收款金额，收款日期
    if (strKmdm && strA5 && strSfkdate) {
        strData += ",\"kmdm\":\"" + strKmdm + "\"";
        strData += ",\"a5\":\"" + strA5 + "\"";
        strData += ",\"sfkdate\":\"" + strSfkdate + "\"";
    }
    else {
        strData += ",\"kmdm\":\"\"";
        strData += ",\"a5\":\"\"";
        strData += ",\"sfkdate\":\"\"";
    }

    //制单人，备注
    strData += ",\"czydm\":\"" + window.parent.objLoginInfo.code + "\"";
    strData += ",\"a9\":\"" + trim($("#a9", "#" + strDivP010102).val()) + "\"";
    strData += "}";

    var objResult;
    if (strOptrTypeP010102 == "1") {
        //新增
        objResult = ParseData(window.external.DoOptions(strTableTp010102, "Add", strData));
    }
    else if (strOptrTypeP010102 == "2") {
        //修改
        objResult = ParseData(window.external.DoOptions(strTableTp010102, "Edit", strData));
    }
    else if (strOptrTypeP010102 == "3") {
        //删除
        objResult = ParseData(window.external.DoOptions(strTableTp010102, "Add", strData));
    }

    //弹出错误信息
    if (objResult.ReturnCode != "0") {
        window.parent.cihoAlert(objResult.MSG);
        return;
    }

    //取得最新数据
    var strData = [];
    strData.push("spdm");
    strData.push("b1");
    strData.push("b2");
    objNewData = getCompareData(strTableTp010102, strData);
    if (objNewData != objCompDataP010102) {
        window.external.DoOptions(strTableTp010102, "DoOthers", "{\"djdate\":\"" + $("#djdate", "#" + strDivP010102).val() + "\"}")
    }

    //保存之后
    if (save == "2") {
        //保存新增，赋初始值
        $("#" + strTableTp010102).clearGridData();
        strRecordsP010102 = 1;
        AddEmptyRowP010102();
        //AddCzHtmlP010102();
        $(":input", $("#" + strDivP010102)).val("");
        $("#gysdm", "#" + strDivP010102).combobox('setValue', '');
        $("#zydm", "#" + strDivP010102).combobox('setValue', '');
        var objCk = $("#ckdm", "#" + strDivP010102).combobox("getData");
        if (objCk.length == 2) {
            $("#ckdm", "#" + strDivP010102).combobox('setValue', objCk[0].dm);
        }
        else {
            $("#ckdm", "#" + strDivP010102).combobox('setValue', '');
        }
        $("#kmdm", "#" + strDivP010102).combobox('setValue', '');
        $("#czydm", "#" + strDivP010102).val(window.parent.objLoginInfo.name);
        $("#" + strTableTp010102).footerData('set', { b3: "" })
        $("#djdate", "#" + strDivP010102).val(getNowDate());
        $("#djdate", "#" + strDivP010102).attr("disabled", false);
        $("#idMust", "#" + strDivP010102).empty();
        $("#a5", "#" + strDivP010102).focus();
    }
    else {
        cihoDlgClose(dialog);
    }

    //释放内存
    objTable = null;
    objRecords = null;
    strOptrTypeP010102 = "1";
    strDeletHtml = "";
    objCompDataP010102 = [];
    iRowP010102 = -1;
    iColP010102 = -1;

    //重新加载数据
    loadGridData_P0101();

    //设定状态
    $("#" + strDivP010102 + "_divform").data("change", false);

}

//验证表单
function checkForm(dialog, save) {

    var bAddTable = false;

    //检查单据日期
    if ($("#djdate ", "#" + strDivP010102).val() == "") {
        window.parent.cihoAlert(msg.dt.inputThDt, null, function () {
            $("#djdate", "#" + strDivP010102).focus();
        });
        return false;
    }

    //验证日期
    if (checkDatePattern($("#djdate", "#" + strDivP010102).val()) == false) {
        window.parent.cihoAlert(msg.dt.dtTypeErr, msgBox.dlgBtn.Ok, function () { $("#djdate", "#" + strDivP010102).select(); });
        return false;
    }

    //验证早于起始时间
    if ($("#djdate ", "#" + strDivP010102).val().replace(/-/g, "/") < window.parent._startDate) {
        window.parent.cihoAlert(msg.other.thrqExtent, null, function () {
            $("#djdate", "#" + strDivP010102).select();
        });
        return false;
    }

    //检查供应商
    var strGysValue = $("#gysdm", "#" + strDivP010102).combobox('getValue');
    if (!strGysValue) {
        //修改时，验证数据是否是停用的数据
        if (strOptrTypeP010102 == "2" && $("#gysdm", "#" + strDivP010102).combobox('getText') != objRecords.gysmc) {
            window.parent.cihoAlert(msg.sz.gys.empOrNotExit, null, function () {
                $("#gysdm", "#" + strDivP010102).combobox('focus');
            });
            return false;
        }
        //新增的时候
        if (strOptrTypeP010102 == "1") {
            window.parent.cihoAlert(msg.sz.gys.empOrNotExit, null, function () {
                $("#gysdm", "#" + strDivP010102).combobox('focus');
            });
            return false;
        }
    }

    //检查经手人
    var strZy = $("#zydm", "#" + strDivP010102).combobox('getValue');
    if (!strZy) {

        //修改时，验证数据是否是停用的数据
        if (strOptrTypeP010102 == "2" && $("#zydm", "#" + strDivP010102).combobox('getText') != objRecords.zymc) {
            window.parent.cihoAlert(msg.sz.jsr.empOrNotExit, null, function () {
                $("#zydm", "#" + strDivP010102).combobox('focus');
            });
            return false;
        }
        //新增的时候
        if (strRecordsP010102 == "1") {
            window.parent.cihoAlert(msg.sz.jsr.empOrNotExit, null, function () {
                $("#zydm", "#" + strDivP010102).combobox('focus');
            });
            return false;
        }
    }

    //检查仓库
    var strCk = $("#ckdm", "#" + strDivP010102).combobox('getValue');
    if (!strCk) {

        //修改时，验证数据是否是停用的数据
        if (strOptrTypeP010102 == "2" && $("#ckdm", "#" + strDivP010102).combobox('getText') != objRecords.ckmc) {
            window.parent.cihoAlert(msg.sz.ck.empOrNotExit, null, function () {
                $("#ckdm", "#" + strDivP010102).combobox('focus');
            });
            return false;
        }
        //新增的时候
        if (strRecordsP010102 == "1") {
            window.parent.cihoAlert(msg.sz.ck.empOrNotExit, null, function () {
                $("#ckdm", "#" + strDivP010102).combobox('focus');
            });
            return false;
        }
    }

    //判断表格里的数据是否合法
    var objTable = $("#" + strTableTp010102);
    var arrIds = objTable.getDataIDs();
    for (var i = 0; i < arrIds.length; i++) {
        var objTr = objTable.getRowData(arrIds[i]);
        var alterMsg = "";
        if (!objTr.flag) {
            continue;
        }
        //        if (checkEmptyRow(objTr, "flag,mxguid,glsl,fpsl")) {
        //            continue;
        //        }
        bAddTable = true;
        var strB3 = parseFloat(objTr.b3);
        if (!isNaN(strB3) && strB3 > window.parent._MaxPerBill) {
            window.parent.cihoAlert(msg.other.djhjExtent);
            return false;
        }
        if (!objTr.spdm || objTr.spdm == "-1") {
            // alterMsg = msg.other.dnh + (i + 1) + msg.sz.yp.lnEmpOrNotExit;
            alterMsg = msg.cx.lxglYp;
            window.parent.cihoAlert(alterMsg);
            return false;
        }
        if (!objTr.b1) {
            // alterMsg = msg.other.dnh + (i + 1) + msg.sz.yp.lnAmountEmp;
            alterMsg = msg.sz.yp.ypslEmp;
            window.parent.cihoAlert(alterMsg);
            return false;
        }
        if (!objTr.b2) {
            // alterMsg = msg.other.dnh + (i + 1) + msg.sz.yp.lnPriceEmp;
            alterMsg = msg.sz.yp.ypPrice;
            window.parent.cihoAlert(alterMsg);
            return false;
        }
        if (objTr.a3 == "是") {
            if (!objTr.tpsl) {
                // alterMsg = msg.other.dnh + (i + 1) + msg.other.TpSlNoEmpty;
                alterMsg = msg.other.TpSlNoEmptyOneRow;
                window.parent.cihoAlert(alterMsg);
                return false;
            }
            if (!objTr.tpdj) {
                //alterMsg = msg.other.dnh + (i + 1) + msg.other.TpDjNoEmpty;
                alterMsg = msg.other.TpDjNoEmptyOneRow;
                window.parent.cihoAlert(alterMsg);
                return false;
            }
            if (!objTr.kpdwdm) {
                // alterMsg = msg.other.dnh + (i + 1) + msg.sz.kp.SpNoEmpty;
                alterMsg = msg.sz.kp.SpNoEmptyOneRwo;
                window.parent.cihoAlert(alterMsg);
                return false;
            }
            if (parseFloat(objTr.tpsl) * parseFloat(objTr.tpdj) == 0) {
                window.parent.cihoAlert(msg.other.tpMoneyOrNotExit);
                return false;
            }
        }

    }

    //表格里一条数据没有
    if (!bAddTable) {
        window.parent.cihoAlert(msg.other.djNotExit);
        return false;
    }

    //对收款账户的特殊处理
    var strKmdm = $("#kmdm", "#" + strDivP010102).combobox('getValue');
    var strA5 = $("#a5", "#" + strDivP010102).val();
    var strSfkdate = $("#sfkdate", "#" + strDivP010102).val();

    //任何一项为空就都不为空
    if (strKmdm || strA5 || strSfkdate) {
        if (!strKmdm) {
            window.parent.cihoAlert(msg.other.inputSkzh, null, function () {
                $("#kmdm", "#" + strDivP010102).combobox('focus');
            });
            return false;
        }
        if (!strA5) {
            window.parent.cihoAlert(msg.other.inputSkJe, null, function () {
                $("#a5", "#" + strDivP010102).focus();
            });
            return false;
        }
        if (!strSfkdate) {
            window.parent.cihoAlert(msg.dt.inputSkDt, null, function () {
                $("#sfkdate", "#" + strDivP010102).focus();
            });
            return false;
        }
    }

    //起始结束时间
    if (strSfkdate) {
        strSfkdate = strSfkdate.replace(/-/g, "/");
        if (strSfkdate < window.parent._startDate) {
            window.parent.cihoAlert(msg.other.skriExtent, null, function () {
                $("#sfkdate", "#" + strDivP010102).select();
            });
            return false;
        }
    }
    //var strB3 = $("#" + strTableTp010102).footerData('get').b3;
    var strB3 = $("#" + strTableTp010102).getCell(arrIds[0], "b3");
    //大于总金额
    if (parseFloat(strB3) > window.parent._MaxPerBill || parseFloat(strA5) > window.parent._MaxPerBill) {
        window.parent.cihoAlert(msg.other.skriExtent);
        return false;
    }

    //收款金额不一致
    if (window.parent.bShowMsgOfMoney == true && strA5 != "" && parseFloat(strB3) != parseFloat(strA5)) {
        window.parent.cihoAlert(msg.other.difMoney, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                saveDataP010102(dialog, save);
                return false;
            }
            else if (result == msgBox.dlgRst.No) {
                return false;
            }
        });
        return false;
    }


    //释放内存
    objTable = null;
    strGysValue = null;
    strZy = null;
    strCk = null;
    arrIds = null;
    strKmdm = null;
    strA5 = null;
    strSfkdate = null;
}

//保存方法
function saveMyDataTp010102(dialog, save) {

    var objTable = $("#" + strTableTp010102);
    if (iRowP010102 != null && iColP010102 != null) {
        objTable.saveCell(iRowP010102, iColP010102);
    }

    var arrIds = objTable.getDataIDs();
    for (var i = 0; i < arrIds.length; i++) {
        var objTr = objTable.getRowData(arrIds[i]);
        if (objTr.flag == "") {
            continue;
        }
        var alterMsg = "";
        if ($("#" + strDivP010102 + "_divform").data("change") != false && strOptrTypeP010102 == "2" && objTr.glsl != 0 && objTr.b1 != objTr.glsl) {
            alterMsg = msg.other.pihaoNoSame;
            window.parent.cihoAlert(alterMsg, null, function (result) {
                if (result == msgBox.dlgRst.Ok) {
                    if (checkForm(dialog, save) == false) {
                        return;
                    }

                    saveDataP010102(dialog, save);
                    return;
                }
            });
            return;
        }
    }
    //如果表格里都没有关联数量，直接保存
    if (checkForm(dialog, save) == false) {
        return;
    }
    saveDataP010102(dialog, save);

    //释放内存
    objTable = null;
}
