
function cgd(cgdbh, strDivP010101) {
    var strTableP010101 = "Tp010101";
    var alterState;

    creatDiv_ShowDlgHaveTbl(strDivP010101, "采购单", false, false, strTableP010101,900);  //创建弹出窗口

    var config = ParseData(window.external.DoOptions("Tp010101", "GetTableConfig", "{}"));      // 向后台请求配置信息
    for (var i = 0; i < config.colModel.length; i++) {
        if (config.colModel[i]["index"] == "cz") {
            config.colModel[i]["hidden"] = true;
        }
    }
    var header = createInputs(config.pageHeader);            // 根据头配置 生成表单元素 
    var footer = createInputs(config.pageFooter);              //根据脚信息   生成表单元素
    $("#idMust", $("#" + strDivP010101)).append("NO:" + cgdbh);
    $("#" + strDivP010101 + "_divform").append(header);                   //添加头
    $("#" + strDivP010101 + "_divform").append('<table id="' + strTableP010101 + '"></table>');        //添加grid占位符
    $("#" + strDivP010101 + "_divform").append(footer);                    //添加脚
    var alterdata = ParseData(window.external.DoOptions("Tp010101", "GetList", '{"djbh":"' + cgdbh + '"}'));
    if (!isNaN(parseFloat(alterdata.a11))) {
        $("#a11", "#" + strDivP010101 + "_divform").val(parseFloat(alterdata.a11).toFixed(window.parent.strMoneyPlace));
    }
    if (!isNaN(parseFloat(alterdata.jiner))) {
        $("#jiner", "#" + strDivP010101 + "_divform").val(parseFloat(alterdata.jiner).toFixed(window.parent.strMoneyPlace));
    }
    $("#a9", "#" + strDivP010101 + "_divform").val(escape(alterdata.a9));
    $("#cgtime", "#" + strDivP010101 + "_divform").val(alterdata.djdate).attr("disabled", true);
    $("#czydm", "#" + strDivP010101 + "_divform").val(alterdata.czymc);
    $("#sfkdate", "#" + strDivP010101 + "_divform").val(alterdata.sfkdate);

    $("#gysdm", "#" + strDivP010101 + "_divform").combobox();
    $("#zydm", "#" + strDivP010101 + "_divform").combobox();
    $("#kmdm", "#" + strDivP010101 + "_divform").combobox();
    $("#ckdm", "#" + strDivP010101 + "_divform").combobox();

    $("#gysdm", "#" + strDivP010101 + "_divform").combobox("setText", alterdata.gysmc);
    $("#zydm", "#" + strDivP010101 + "_divform").combobox("setText", alterdata.zymc);
    $("#kmdm", "#" + strDivP010101 + "_divform").combobox("setText", alterdata.kmmc);
    $("#ckdm", "#" + strDivP010101 + "_divform").combobox("setText", alterdata.ckmc);

    $("#gysdm", "#" + strDivP010101 + "_divform").combobox('disable');
    $("#zydm", "#" + strDivP010101 + "_divform").combobox('disable');
    $("#kmdm", "#" + strDivP010101 + "_divform").combobox('disable');
    $("#ckdm", "#" + strDivP010101 + "_divform").combobox('disable');
    $(":text[name]", "#" + strDivP010101 + "_divform").attr("disabled", true);

    var cNames = config.colNames;
    var cModel = config.colModel;

    $("#" + strTableP010101).jqGrid({
        datatype: "local",
        //height: "100%",
        autowidth: true,
        colNames: cNames,
        colModel: cModel,
        rownumbers: true,
        footerrow: true,
        rownumWidth: 30
    });

    $("#" + strTableP010101).setColProp("a1", { formatter: formatCountPlace });
    $("#" + strTableP010101).setColProp("a2", { formatter: formatPricePlace });
    $("#" + strTableP010101).setColProp("a4", { formatter: formatCountPlace });
    $("#" + strTableP010101).setColProp("a5", { formatter: formatPricePlace });
    $("#" + strTableP010101).setColProp("sunmoney", { formatter: formatMoneyPlace });
    $("#" + strTableP010101).setColProp("a6", { formatter: formatMoneyPlace });

    var mylist = alterdata.Records;
    //根据数据库返回数据的条数确定要产生的id

    var count = mylist.length > 5 ? mylist.length : 5;

    //添加行并为行分配id，并计算合计
    var ypsl = 0, ypje = 0, kpsl = 0, kpje = 0;
    for (var i = 0; i < count; i++) {
        if (mylist[i]) {                                                            //添加后台数据行
            ypsl += parseFloat(mylist[i].a1);
            ypje += parseFloat(mylist[i].sunmoney);
            if (mylist[i].daipiao == "是") {//计算带票合计
                kpsl += parseFloat(mylist[i].a4);
                kpje += parseFloat(mylist[i].a6);
            }
            $("#" + strTableP010101).addRowData(i + 1, mylist[i]);      //添加后台数据行
            if (mylist[i].daipiao == "否") {
                $("#" + strTableP010101).setCell(i + 1, "a4", "", { color: "gray" });
                $("#" + strTableP010101).setCell(i + 1, "a5", "", { color: "gray" });
                $("#" + strTableP010101).setCell(i + 1, "a6", "", { color: "gray" });
            }
        }
        else {                                                                             //添加空行
            $("#" + strTableP010101).addRowData(i + 1, {});
        }
    }
    //将最大id保存到grid
    $("#" + strTableP010101).data("nextNum", count + 1);
    //$("table.ui-jqgrid-htable th:first", "#" + strDivP010101).text("序号");
    $("#" + strTableP010101).setLabel("rn", "序号");
    //将合计展示到grid底部
    $("#" + strTableP010101).footerData("set", { rn: "合计", a1: ypsl.toFixed(window.parent.strCountPlace), sunmoney: ypje.toFixed(window.parent.strMoneyPlace), a4: kpsl.toFixed(window.parent.strCountPlace), a6: kpje.toFixed(window.parent.strMoneyPlace) });
    $("#" + strDivP010101).dialog("option", "position", "center");
    //$("#gbox_" + strTableP010101).css("height", "157px");
    $("#" + strTableP010101).setGridHeight(141);
    resizeGridScroll(strTableP010101);
}

//创建表单元素
function createInputs(arr) {
    var strForm = "<ul>";
    for (i = 0; i < arr.length; i++) {
        var Item = arr[i];
        if (!Item.hidden) {
            if (Item.edittype == "text") {
                if (Item["name"] == "a9") {
                    strForm += "<li class='bz'>";
                    strForm += Item.xsmc + "：";
                    strForm += "<input type='text'  name='" + Item.index + "' ";
                    strForm += " value=''  ";
                    strForm += " id='" + Item.index + "'>";
                    strForm += "</li>";
                }
                else {
                    strForm += "<li>";
                    strForm += Item.xsmc + "：";
                    strForm += "<input type='text'  name='" + Item.index + "' ";
                    strForm += " value=''  ";
                    strForm += " id='" + Item.index + "'>";
                    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>";
    return strForm;
}
function formatCountPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}
function formatPricePlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strPricePlace);
    }
    else {
        return "&nbsp;";
    }
}
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}