//创建表单
function tableYfqcForm(strTableName, strType) {
    var names = $('#' + strTableName).jqGrid('getGridParam', "colNames");
    var indexs = $('#' + strTableName).jqGrid('getGridParam', "colModel");
    var sign = "";
    var inputtype = "";

    var strForm = "";
    var strAppendTmp = "";
    strForm += "<ul>";
    for (var i = 1; i < names.length; i++) {
        //alert(indexs[i]["name"]);
        if (indexs[i]["name"] == "c2" || indexs[i]["name"] == "b3" || indexs[i]["name"] == "b2" || indexs[i]["name"] == "id"
            || indexs[i]["name"] == "a11" || indexs[i]["name"] == "b1" || indexs[i]["name"] == "c1"||indexs[i]["name"]=="jldm"
           || indexs[i]["name"] == "cz" || indexs[i]["name"] == "b2" || indexs[i]["name"] == "c4" || indexs[i]["name"] == "a10") {
            continue;
        }
        if (indexs[i]["edittype"] == "text") {
            strAppendTmp += "<li>";
            strAppendTmp += "<span>";
            strAppendTmp += sign;
            strAppendTmp += names[i] + "：";
            strAppendTmp += "</span>";
            strAppendTmp += "<lable>";
            strAppendTmp += "<input type='text'  value=''  name='" + indexs[i]["name"] + "'";
            strAppendTmp += " id='" + indexs[i]["name"] + "' maxlength='50'"; //限制输入长度
            strAppendTmp += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>"; //限制输入，粘贴特殊字符
            strAppendTmp += "</lable>";
            strAppendTmp += "</li>";
        }
        if (indexs[i]["edittype"] == "select") {
            if (indexs[i]["name"] == "gysdm") {
                //加载供应商代码及名称
                var strRet = window.external.DoOptions('Tgysxx', 'GetComboBoxData', '{}');
                var strGys = ParseData(strRet);
                strAppendTmp += "<li>";
                strAppendTmp += "<label>";
                strAppendTmp += "<select name='" + indexs[i]["name"] + "' id='" + indexs[i]["name"] + "'";
                strAppendTmp += " class='easyui-combobox'";
                strAppendTmp += " onchange=\"if ($(this).val() == -1) {openDwDlg();}\">";
                if (strGys.Records != "{}") {
                    for (var j = 0; j < strGys.Records.length; j++) {

                        strAppendTmp += "<option value='" + strGys.Records[j].gysdm + "'>" + strGys.Records[j].a3 + "</option>";

                    }
                }
                else {
                    strAppendTmp += "<option value='-1'> </option>";
                }
//                strAppendTmp += "<option value='345'>one</option>";
//                strAppendTmp += "<option value='456'>two</option>";
//                strAppendTmp += "<option value='789'>three</option>";
                strAppendTmp += "</select>";
                strAppendTmp += "</label>";
                strAppendTmp += "<div class='span'>";
                strAppendTmp += sign + names[i] + "：";
                strAppendTmp += "</div>";
                strAppendTmp += "</li>";
            }
            else if (indexs[i]["name"] == "spdm") {
            //加载商品代码及名称
            var strRet = window.external.DoOptions("Tspxx", "GetComboBoxData", "{}");
            var strSp = ParseData(strRet);
                strAppendTmp += "<li>";
                strAppendTmp += "<label>";
                strAppendTmp += "<select name='" + indexs[i]["name"] + "' id='" + indexs[i]["name"] + "'";
                strAppendTmp += " class='easyui-combobox'";
                strAppendTmp += " onchange=\"if ($(this).val() == -1) {openDwDlg();}\">";
                if (strSp.Records != "{}") {
                    for (var j = 0; j < strSp.Records.length; j++) {

                        strAppendTmp += "<option value='" + strSp.Records[j].spdm + "'>" + strSp.Records[j].a3 + "</option>";

                    }
                }

//                strAppendTmp += "<option value='2001'>one</option>";
//                strAppendTmp += "<option value='2002'>two</option>";
//                strAppendTmp += "<option value='2003'>three</option>";
                strAppendTmp += "</select>";
                strAppendTmp += "</label>";
                strAppendTmp += "<div class='span'>";
                strAppendTmp += sign + names[i] + "：";
                strAppendTmp += "</div>";
                strAppendTmp += "</li>";

            }
        }

    }
    strForm += strAppendTmp;
    strForm += "</ul>";
    $("#gridRecord_divform").append(strForm);
    $("#gysdm").combobox();
    $('#spdm').combobox();

    $(".layoutpanel").css("z-index", "50000009");
    
    //修改时，加载value
    if (strType.toString() == '2') {
        for (var i = 0; i < indexs.length; i++) {
            $('#' + indexs[i].name, "#gridRecord_divform").val(jQuery('#' + strTableName).jqGrid('getCell', selRowId, i));
        }
    }
    //form change设定
    //form change设定
    $(":input", $("#alterRecord_divform")).change(function() {
    $("gridRecord_divform").data("change", true);
    });
}

/*-----------------------------------------------------------------------------
//功能介绍：保存数据
//参   数：
strTableName, jqGrid名称
saveType，	保存类型
0：退出
3：保存新增
4：保存
//返回 值：
-----------------------------------------------------------------------------*/
function SaveYfqc(strTableName, saveType,kmdm) {
    //保存数据到DB
    var strRet = saveYfqcData(strTableName, kmdm);
    //设定selet控件的选中的值到jqGrid中
    $("#" + strTableName).jqGrid("setCell", selRowId, "b1", $("[id$=gysdm] option:selected").text());
    $("#" + strTableName).jqGrid("setCell", selRowId, "c1", $("[id$=spdm] option:selected").text());
//    $("#" + strTableName).jqGrid("setCell", selRowId, "gysdm", $("[id$=gysdm] option:selected", $("# ")).text());
//    $("#" + strTableName).jqGrid("setCell", selRowId, "spdm", $("[id$=spdm] option:selected", $("#gridRecord_divform")).text());
    if ("0" == strRet) {
        //数据标志为没有change
        $("#gridRecord_divform").data("change", false);

        //保存新增
        if (saveType == '2') {
            selRowId = "0";
            savesign = "1";
            //清空元素中的数据
            $(":input", $("#gridRecord_divform")).val("");
        }
        //退出
        else if (saveType == '0') {
        $('#gridRecord').dialog('close');
        }
    }
}
/*-----------------------------------------------------------------------------
//功能介绍：保存数据
//参   数：
//strTableName  表名，同时是该页面对应的模块的类名
//url           保存数据的URL
//saveType:
//          3：保存新增
//          4：保存
//          0：退出
//返回 值：NULL
-----------------------------------------------------------------------------*/
function saveYfqcData(strTableName, kmdm) {
    var strRecord = "";
    //获取要保存的数据。
    if (savesign == "1") {
        strRecord = "{'kmdm':'"+kmdm+"',";
    }
    else {
        strRecord = "{'kmdm':'"+kmdm+"',\"jldm\":\"" + $("#" + strTableName).jqGrid("getCell", selRowId, "jldm") + "\",";
    }
    $(":input", $("#gridRecord_divform")).each(function() {
        if ($(this).attr("name") != "" && $(this).attr("name") != null) {
            strRecord += "\"" + $(this).attr("name") + "\":\"" + trim($(this).val()) + "\",";
        }
    });
    //去掉最后的'，'
    strRecord = strRecord.substring(0, strRecord.length - 1);
    strRecord += "}";

    var s = "";
    //初次保存，savesign：1---insert数据 ；savesign：2---update数据
    if (savesign == "1") {
        //插入
        s = ParseData(window.external.DoOptions(strTableName, 'Add', strRecord));
        selRowId = s.id;
        if ("0" == s.ReturnCode) {
            savesign = "2";
            //插入数据到jqGrid
            $("#" + strTableName).jqGrid("addRowData", selRowId, ParseData(strRecord));
            var strCz = "<a href=\"#\" style=\"color:red;text-decoration:none;\" onclick=\"UpdatePop('" + strTableName + "','" + selRowId + "');\">修改</a>|<a href=\"#\" style=\"color:red;text-decoration:none;\"  onclick=\"DeletPop(this,'" + strTableName + "','" + selRowId + "');\">删除</a>";
            $("#" + strTableName).setCell(selRowId, "cz", strCz);
        }
    }
    else {//更新
        s = ParseData(window.external.DoOptions(strTableName, 'Edit', strRecord));
        if ("0" == s.ReturnCode) {
            //更新数据到jqGrid
            $("#" + strTableName).jqGrid("setRowData", selRowId, ParseData(strRecord));
            
        }
    }
    return s.ReturnCode;
}
/*-----------------------------------------------------------------------------
//功能介绍：删除记录
//参   数：
strRow：要删除的数据的ID
strTableName：jqGrid的ID，同时是该页面对应的模块的类名
//返回 值：
-----------------------------------------------------------------------------*/
function deleteYfqcRow(rowId, strTableName,kmdm) {
    if (confirm("确定要删除数据【" + rowId + "】吗？")) {
        //组织删除数据字符串
        var strDelGuid = "{'kmdm':'"+kmdm+"',";
        strDelGuid+='"jldm":"';
        strDelGuid += $("#" + strTableName).jqGrid("getCell", rowId, "jldm");
        strDelGuid += '"}';
        //delete from database
        var strRet = ParseData(window.external.DoOptions(strTableName, 'Delete', strDelGuid));

        //操作成功
        if (strRet.ReturnCode == "0") {
            //清除浮动层的内容
            $(".operate").remove();

            //从jqGrid中删除数据
            $("#" + strTableName).jqGrid("delRowData", rowId);
        }
        window.parent.cihoAlert(strRet.MSG);
    }
}