/*-------------------------------文档信息-----------------------------------
///功能：预提支付单单据
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strTableP051102_zfd = "Tp050701";       //表格ID

//打开单据
function openYtzfd(strDlgID, djbh) {
    $("#" + strDlgID).empty();
    creatDiv_ShowDlgHaveTbl(strDlgID, "预提费用支付", false, false, strTableP051102_zfd, "700");  //创建弹出窗口
    createElements_zfd(djbh, strDlgID);
    //$("#" + strDlgID).dialog("option", "position", "center");
}

//创建窗口input元素和grid
function createElements_zfd(djbh, dlgId) {
    var config = ParseData(window.external.DoOptions(strTableP051102_zfd, "GetTableConfig", "{}"));      // 向后台请求配置信息
    var header = createInputs(config.pageHeader);            // 根据头配置 生成表单元素 
    var footer = createInputs(config.pageFooter);              //根据脚信息   生成表单元素

    $("#" + dlgId + "_divform").append(header);                   //添加头
    $("#" + dlgId + "_divform").append('<table id="' + strTableP051102_zfd + '"></table>');        //添加grid占位符
    $("#" + dlgId + "_divform").append(footer);                    //添加脚
    for (var i = 0; i < config.colModel.length; i++) {
        config.colModel[i].editable = false;
    }
    createDjGrid(strTableP051102_zfd, config.colNames, config.colModel);
    //$("#gbox_" + strTableP051102_zfd).css("height", "157");
    $("#" + strTableP051102_zfd).setGridHeight(141);

    resizeGridScroll(strTableP051102_zfd);
    //$("table.ui-jqgrid-htable th:first", "#" + dlgId).text("序号");
    $("#" + strTableP051102_zfd).setLabel("rn", "序号");
    
    $("#" + strTableP051102_zfd).setColProp("kzfje", { formatter: formatMoneyPlace });
    $("#" + strTableP051102_zfd).setColProp("bczfje", { formatter: formatMoneyPlace });
    //=================为页头和页脚赋值==============
    initInputsProp(dlgId, strTableP051102_zfd);

    var mylist = [];
    var alterdata = ParseData(window.external.DoOptions(strTableP051102_zfd, "GetList", '{ "djbh": "' + djbh + '" }'));
    $("#idMust", "#" + dlgId).append('NO:<span id="djbh">' + djbh + '</span>');
    $("#djdate", "#" + dlgId).attr("disabled", true).val(alterdata.djdate);
    $("#kmdm", "#" + dlgId).combobox("setValue", alterdata.kmdm);
    if ($("#kmdm", "#" + dlgId).combobox("getText") == alterdata.kmdm) {
        $("#kmdm", "#" + dlgId).combobox("setText", alterdata.kmmc);
    }
    $("#kmdm", "#" + dlgId).combobox("disable");
    $("#zydm", "#" + dlgId).combobox("setValue", alterdata.zydm);
    if ($("#zydm", "#" + dlgId).combobox("getText") == alterdata.zydm) {
        $("#zydm", "#" + dlgId).combobox("setText", alterdata.zymc);
    }
    $("#zydm", "#" + dlgId).combobox("disable");
    $("#fykmdm", "#" + dlgId).combobox("setValue", alterdata.fykmdm);
    if ($("#fykmdm", "#" + dlgId).combobox("getText") == alterdata.fykmdm) {
        $("#fykmdm", "#" + dlgId).combobox("setText", alterdata.fykmdmmc);
    }
    $("#fykmdm", "#" + dlgId).combobox("disable");
    $("#czydm", "#" + dlgId).attr("disabled", true).val(alterdata.czymc);
    $("#bz", "#" + dlgId).attr("disabled", true).val(escape(alterdata.bz));
    if (alterdata.Records instanceof Array) { mylist = alterdata.Records };

    loadGridData(strTableP051102_zfd, mylist);
    config = null;
    header = null;
    footer = null;
    mylist = {};
    alterdata = {};
}

//加载窗口grid数据，并计算合计
function loadGridData(strTableName, mylist) {
    $("#" + strTableName).clearGridData();
    var count = mylist.length > 5 ? mylist.length : 5;
    //添加行并为行分配id，并计算合计
    var kzfje = 0, bczfje = 0;
    for (var i = 0; i < count; i++) {
        if (mylist[i]) {                                                                         //添加后台数据行
            kzfje += parseFloat(mylist[i].kzfje);
            bczfje += parseFloat(mylist[i].bczfje);
            $("#" + strTableName).addRowData(i + 1, mylist[i]);      //添加后台数据行
        }
        else {                                                                                     //添加空行
            $("#" + strTableName).addRowData(i + 1, {});
        }
    }
    $("#" + strTableName).footerData("set", { rn: "合计", kzfje: kzfje, bczfje: bczfje }, true);
    $("#" + strTableName).data("nextNum", count + 1);
    count = null;
    kzfje = bczfje = null;
}

//创建单据grid
function createDjGrid(strTableName, cNames, cModel) {
    $("#" + strTableName).jqGrid({
        datatype: "local",
        //height: "100%",
        autowidth: true,
        colNames: cNames,
        colModel: cModel,
        rownumbers: true,
        footerrow: true,
        sortable: false,
        rownumWidth: 30,
        cellEdit: true,
        cellsubmit: "clientArray"
    });
}

//创建表单元素
function createInputs(arr) {
    var strForm = "<ul>";
    for (i = 0; i < arr.length; i++) {
        var Item = arr[i];
        if (!Item.hidden) {
            if (Item.edittype == "text") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' diabled ";
                strForm += " value=''  ";
                strForm += " id='" + Item.index + "' ";
                if (!Item.editable) {
                    strForm += " disabled='disabled'";
                }
                strForm += " 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 + "'diabled";
                strForm += " class='easyui-combobox' >";
                strForm += "</select>";
                strForm += "</li>";
            }
        }
    }
    strForm += "</ul>";
    return strForm;
}

//设置属性
function initInputsProp(dlgId, tbId) {
    $("#djdate,#kmdm,#zydm", "#" + dlgId).parent().addClass("inp_70");
    $("#bz", "#" + dlgId).parent().addClass("bz");    
    $("#kmdm", "#" + dlgId).combobox({ valueField: "dm", textField: "mc" });
    $("#fykmdm", "#" + dlgId).combobox({ valueField: "kmdm", textField: "mc" });
    $("#zydm", "#" + dlgId).combobox({ valueField: "zydm", textField: "mc" });
    $("#fykmdm", "#" + dlgId).combobox("loadData", [{ "kmdm": "", "mc": "全部费用科目" }]);
}

//格式化money
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}