/*-------------------------------文档信息-----------------------------------
///功能：预提单单据
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strTableTp050501_ytd = "Tp050501";          //gridId
var strRecordsP051102_ytd = 5;              //表格行数

//格式金额
function formatterMoney(cellValue) {

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

//格式数量
function formatterCount(cellValue) {

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

//格式化百分号
function formatPercentPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return (cellValue * 100).toFixed(window.parent.strPercentPlace) + "%";
    }
    else {
        return "&nbsp;";
    }
}

//自定义grid
function createGridP051102_ytd(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: [],
        rownumbers: true,
        rowNum: 9999,
        sortname: "id",
        autowidth: true,
        //height: "100%",
        footerrow: true,
        cellEdit: true,
        cellsubmit: "clientArray",
        gridview: true,
        rownumWidth: 30
    }); //.parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
    //增加表头‘序号’列
    //$("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
    $("#" + strTableID).setLabel("rn", "序号");
}

//添加空白行
function AddEmptyRowP051102_ytd() {
    for (var i = 999999999; i < 999999999 + strRecordsP051102_ytd; i++) {
        $("#" + strTableTp050501_ytd).addRowData(i, { flag: "" });
    }
    resizeGridScroll(strTableTp050501_ytd);
}

//打开单据
function showDialogP051102_ytd(strDialog,strDjbh) {
    $("#" + strDialog).empty();
    creatDiv_ShowDlgHaveTbl(strDialog, "预提单", false, false, strTableTp050501_ytd, "800");
    $("#idMust", $("#" + strDialog)).append("NO:" + strDjbh);
    creatDialogItem_P051102_ytd(strDjbh, strDialog);
    $("#" + strDialog).dialog("option", "position", "center");
}

//创建表单元素
function creatDialogItem_P051102_ytd(strDjbh, strDialog) {
    var config = ParseData(window.external.DoOptions(strTableTp050501_ytd, "GetTableConfig", "{}"));
    if (config == null) {
        window.parent.cihoAlert(msg.other.modelInfoFail);
        return;
    }

    if (strDjbh != "") {
        var objRecords = ParseData(window.external.DoOptions(strTableTp050501_ytd, 'GetList', "{\"djbh\":\"" + strDjbh + "\"}"));
    }

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

    }
    strForm += "</ul>";
    strForm += "<table id=" + strTableTp050501_ytd + "></table>";
    strForm += "<br/>";
    $("#" + strDialog + "_divform").append(strForm);
    $("#djtypebh", "#" + strDialog).combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth100,
        listWidth: _iWidth100
    });
    var strYwlx = [{ "dm": "0", "mc": "销售" }, { "dm": "1", "mc": "回款"}];
    $("#djtypebh", "#" + strDialog).combobox("loadData", strYwlx);
    $("#jsrmc", "#" + strDialog).combobox();
    $("#kmmc", "#" + strDialog).combobox();
    $(".layoutpanel").css("z-index", "50000009");

    var objGzbh = 0;                             //预提方式
    //预提方式：0，按数量；1，按金额
    if (objRecords.Records.length != "0") {
        objGzbh = objRecords.Records[0].ytfs;
        $("#djtypebh", "#" + strDialog).combobox("setValue", objRecords.Records[0].yttype);
        $("#ywdjbh", "#" + strDialog).val(objRecords.Records[0].ywdjbh);
        $("#jsrmc", "#" + strDialog).combobox("setText", objRecords.Records[0].jsrmc);
        $("#kmmc", "#" + strDialog).combobox("setText", objRecords.Records[0].kmmc);      

    }
    //创建表格
    for (var i = 0; i < config.colModel.length; i++) {
        config.colModel[i].editable = false;
    }
    createGridP051102_ytd(strTableTp050501_ytd, config.colNames, config.colModel);
    //$("#gbox_" + strTableTp050501_ytd).css("height", "157");
    $("#" + strTableTp050501_ytd).setGridHeight(141);
    //调整表格的自适应的高度
    resizeGridScroll(strTableTp050501_ytd);
    $("#" + strTableTp050501_ytd).setColProp("a2", { formatter: formatterMoney });
    $("#" + strTableTp050501_ytd).setColProp("a1", { formatter: formatterCount });
    $("#" + strTableTp050501_ytd).setColProp("a4", { formatter: formatterMoney });
    if (objGzbh == "1") {
        $("#" + strTableTp050501_ytd).setColProp("a3", { formatter: formatPercentPlace});
    }
    else {
        $("#" + strTableTp050501_ytd).setColProp("a3", { formatter: formatterMoney });
    }
    if (objRecords != undefined) {
        //加载表格数据
        loadTreeData(strTableTp050501_ytd, objRecords.Records);

        if (objRecords.Records == "{}") {
            objRecords.Records = "";
        }
        strRecordsP051102_ytd = objRecords.Records.length > 5 ? objRecords.Records.length - objRecords.Records.length : (5 - objRecords.Records.length);
    }
    else {
        strRecordsP051102_ytd = 5;
    }
    //合计
    GetTotalP051102_ytd();
    //空白hang
    AddEmptyRowP051102_ytd(); 
    objJsr = null;
    objKjkm = null;
    strYwlx = null;
    strForm = null;
    Item = null;
    i = null;
    config = null;
    objRecords = null;
    objGzbh = null;
}

//合计
function GetTotalP051102_ytd() {
    var objTable = $("#" + strTableTp050501_ytd);
    var arrIds = objTable.getDataIDs();
    var strSl = 0;
    var strJe = 0;
    var strYtje = 0;
    for (var i = 0; i < arrIds.length; i++) {
        var intNumSl = parseFloat(objTable.getCell(arrIds[i], "a1"));
        if (!isNaN(intNumSl)) {
            strSl += intNumSl;
        }
        var intNumJe = parseFloat(objTable.getCell(arrIds[i], "a2"));
        if (!isNaN(intNumJe)) {
            strJe += intNumJe;
        }
        var intNumYtje = parseFloat(objTable.getCell(arrIds[i], "a4"));
        if (!isNaN(intNumYtje)) {
            strYtje += intNumYtje;
        }
    }
    if (strYtje > window.parent._MaxPerBill) {
        window.parent.cihoAlert(msg.other.djhjExtent);
        return;
    }
    objTable.footerData("set", { rn: "合计",
        a1: parseFloat(strSl).toFixed(window.parent.strCountPlace),
        a2: parseFloat(strJe).toFixed(window.parent.strMoneyPlace), 
        a4: parseFloat(strYtje).toFixed(window.parent.strMoneyPlace) });
    objTable = null;
    arrIds = null;
    strSl = null;
    strJe = null;
    strYtje = null;
}


