
//生成表单，修改时，添加数据
function tableKcForm(strTableName, strType) {
    var names = $('#' + strTableName).jqGrid('getGridParam', "colNames");
    var indexs = $('#' + strTableName).jqGrid('getGridParam', "colModel");
    var sign = "";
    //表单开始
    var strForm = "";
    var strAppendTmp = "";
    strForm += "<ul>";
    //|| indexs[i]["name"] == "a2" || indexs[i]["name"] == "a1" 
    for (var i = 1; i < names.length; i++) {
        strAppendTmp = "";
        if (indexs[i]["name"] == "c4" || indexs[i]["name"] == "c3" || indexs[i]["name"] == "c2" || indexs[i]["name"] == "b2"
                || indexs[i]["name"] == "id" || indexs[i]["name"] == "cz" || indexs[i]["name"] == "b1" || indexs[i]["name"] == "c1"
                ||indexs[i]["name"]=="jldm") {
            continue;
        }
        //text框判断。
        if (indexs[i]["edittype"] == "text") {
            if (indexs[i]["name"] == "a3" || indexs[i]["name"] == "a4") {
                strAppendTmp += "<li>";
                strAppendTmp += "<label>";
                strAppendTmp += "<input type='text' value='' name='" + indexs[i]["name"] + "'";
                strAppendTmp += " id='" + indexs[i]["name"] + "' maxlength='50'>"; //限制输入长度
                //strAppendTmp += " onkeypress='return checkNum(event)' onpaste='return numPaste()'>"; //限制输入，粘贴特殊字符
                strAppendTmp += "</label>";
                strAppendTmp += "<div class='span'>";
                strAppendTmp += sign;
                strAppendTmp += names[i] + "：";
                strAppendTmp += "</div>";
                strAppendTmp += "</li>";
            }
            else if (indexs[i]["name"] == "c5" || indexs[i]["name"] == "c6" || indexs[i]["name"] == "a5") {
                strAppendTmp += "<li>";
                strAppendTmp += "<label>";
                strAppendTmp += "<input type='text' value='' name='" + indexs[i]["name"] + "' disabled";
                strAppendTmp += " id='" + indexs[i]["name"] + "'>"; //限制输入长度
                //strAppendTmp += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>"; //限制输入，粘贴特殊字符
                strAppendTmp += "</label>";
                strAppendTmp += "<div class='span'>";
                strAppendTmp += sign;
                strAppendTmp += names[i] + "：";
                strAppendTmp += "</div>";
                strAppendTmp += "</li>";
            }
            else {
                strAppendTmp += "<li>";
                strAppendTmp += "<label>";
                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 += "</label>";
                strAppendTmp += "<div class='span'>";
                strAppendTmp += sign;
                strAppendTmp += names[i] + "：";
                strAppendTmp += "</div>";
                strAppendTmp += "</li>";
            }
        }
        //下拉框
        else if (indexs[i]["edittype"] == "select") {
            if (indexs[i]["name"] == "ckdm") {
                var strRet = window.external.DoOptions('TCkxx', 'GetList', '{}');
                var strCk = ParseData(strRet);
                strAppendTmp += "<li>";
                strAppendTmp += "<label>";
                strAppendTmp += "<select name='" + indexs[i]["name"] + "' id='" + indexs[i]["name"] + "'";
                strAppendTmp += "class='easyui-combobox'>";
                if (strCk.Records != "{}") {
                    for (var j = 0; j < strCk.Records.length; j++) {
                        strAppendTmp+="<option value='"+strCk.Records[j].ckdm+"'>"+strCk.Records[j].a3+"</option>";
                    }
                }
//                strAppendTmp += "<option value='10001' selected>仓库1</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', 'GetList', '{}');
                var strSp = ParseData(strRet);
                strAppendTmp += "<li>";
                strAppendTmp += "<label>";
                strAppendTmp += "<select name='" + indexs[i]["name"] + "' id='" + indexs[i]["name"] + "'";
                strAppendTmp += "class='easyui-combobox'>";

                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' selected>感康</option>";
                strAppendTmp += "</select>";
                strAppendTmp += "</label>";
                strAppendTmp += "<div class='span'>";
                strAppendTmp += sign + names[i] + "：";
                strAppendTmp += "</div>";
                strAppendTmp += "</li>";
            }
        }
        strForm += strAppendTmp;
    } //FOR循环结束
    strForm += "</ul>";
    $("#gridRecord_divform").append(strForm);
    $("#ckdm").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设定
    $(":input", $("#gridRecord_divform")).change(function () {
        $("#gridRecord_divform").data("change", true);
    });
}
function saveKcqc(strTableName, saveType, kmdm) {
    //同步保存数据
    var strRet = saveKcData(strTableName, kmdm);
    if ("0" == strRet) {
        window.parent.cihoAlert("保存成功！");
        var i = $("#a3").val();
        var j = $("#a4").val();
        var K_forA5 = i * j;
        $("#" + strTableName).jqGrid("setCell", selRowId, "a5", K_forA5);
        $("#" + strTableName).jqGrid("setCell", selRowId, "b1", $("[id$=ckdm] option:selected", $("#gridRecord_divform")).text());
        $("#" + strTableName).jqGrid("setCell", selRowId, "c1", $("[id$=spdm] option:selected", $("#gridRecord_divform")).text());
//        strButtonHtml = "";
//        strButtonHtml += "<a href=\"#\" style=\"color:red;text-decoration:none;\" onclick=\"UpdatePop('TKcqc','"+selRowId+"')\">修改</a>";
//        strButtonHtml += "|";
//        strButtonHtml += "<a href=\"#\" style=\"color:red;text-decoration:none;\" onclick=\"DeletPop(this,'" + strTableName + "','"+selRowId+"');\">删除</a>";
//        $("#" + strTableName).jqGrid("setCell", selRowId, "cz", strButtonHtml);
        //数据标志为没有change
        $("#gridRecord_divform").data("change", false);

        //保存新增
        if (saveType == '2') {
            selRowId = "0";
            savesign = "1";
            $(":input", $("#gridRecord_divform")).val("");

        }
        //退出
        else if (saveType == '0') {
            $('#gridRecord').dialog('close');
        }
    }
}
function saveKcData(strTableName, kmdm) {
    var strRecord = "";
    //strRecord += "{";
    //获取要保存的数据。6623eaff-6389-47c5-a2ae-5ebcbecee8ed
    if (savesign == "1") {
        strRecord = "{'kmdm':'" + kmdm + "',";
        //strRecord += "\"kmdm\":\""+kmdm+"\",";
    }
    else {
        strRecord = "{'kmdm':'" + kmdm + "',\"jldm\":\"" + $("#" + strTableName).jqGrid("getCell", selRowId, "jldm") + "\",";
        //strRecord = "{\"kmdm\":\"" + kmdm + "\",";
        //strRecord += "\"id\":\"" + $("#"+strTableName).jqGrid("getCell",selRowId,"id") + "\",";
    }

    $(":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("setCell", selRowId, "b1", $("[id$=ckdm] option:selected", $("#gridRecord_divform")).text());
//            $("#" + strTableName).jqGrid("setCell", selRowId, "c1", $("[id$=spdm] option:selected", $("#gridRecord_divform")).text());
            $("#" + 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);
            $("#" + strTableName).jqGrid("setCell", selRowId, "jldm", s.jldm);
        }
    }
    else {//更新
        s = ParseData(window.external.DoOptions(strTableName,'Edit', strRecord));
        if ("0" == s.ReturnCode) {

            //更新数据到jqGrid
            $("#" + strTableName).jqGrid("setRowData", selRowId, ParseData(strRecord));
        }
    }
    //alert(s.ReturnCode + "__ReturnCode");
    return s.ReturnCode;
}

        /*-----------------------------------------------------------------------------
        //功能介绍：删除记录
        //参   数：
        strRow：要删除的数据的ID
        strTableName：jqGrid的ID，同时是该页面对应的模块的类名
        //返回 值：
        -----------------------------------------------------------------------------*/
function deletekcRow(rowId, strTableName,kmdm) {
    if (confirm("确定要删除数据【" + rowId + "】吗？")) {
        //组织删除数据字符串
        var strDelGuid = "{'kmdm':'" + kmdm + "',";
        //strDelGuid += "\"kmdm\":\"" + kmdm + "\","
        strDelGuid += '"jldm":"';
        strDelGuid += $("#" + strTableName).jqGrid("getCell", rowId, "jldm");
       //strDelGuid += "\"id\":\"" + $("#"+strTableName).jqGrid("getCell",rowId,"id") + "\"";
        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);
    }
}