/*--------------------------文档信息-------------------------------------------
///功能：      流向单的查看单据
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//定义全局变量
strTableP021403_lxd = "Tp020501";

//查看单据
function showDialogP021403_lxd(strDivP021403_lxd, strDjbh) {
    try {
        $(".operate").remove();    //清除浮动按钮层
        $("#divP021403_lxd").empty();    //清空弹出窗口内容

        //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
        creatDiv_ShowDlgHaveTbl(strDivP021403_lxd, "流向单", false, false, strTableP021403_lxd, "900");  //创建弹出窗口

        //添加表单元素到窗口中
        tableFormP021403_lxd(strDivP021403_lxd, strDjbh);

        //调用所查数据同时加载到所对应的控件
        myListP021403_lxd(strDivP021403_lxd, strDjbh);

        //重新定位dialog的位置
        //$("#" + strDivP021403_lxd).dialog("option", "position", "center");

        //释放内存
        strDivP021403_lxd = null;
        strDjbh = null;
    } catch (e) { }
}

//创建表单
function tableFormP021403_lxd(strDivP021403_lxd, strDjbh) {
    var strHeadP021403_lxd = "";          //页头默认为空
    var strTableP021403_lxd = "";         //默认没有表格
    var strDownP021403_lxd = "";         //页尾的默认为空
    var objP021403_lxd = {};                  //存放列配置
    var strPageHeader = [];                     //页头的列配置
    var strpageFooter = [];                      //页尾的列配置

    //加载编号的语句
    $("#idMust", $("#" + strDivP021403_lxd)).append("NO:" + strDjbh);

    //获得jqGrid的列名和属性
    objP021403_lxd = ParseData(window.external.DoOptions("Tp020501", 'GetTableConfig', '{}'));
    strPageHeader = objP021403_lxd.pageHeader;
    strpageFooter = objP021403_lxd.pageFooter;

    strHeadP021403_lxd += "<ul>";
    for (var i = 0; i < strPageHeader.length; i++) {         //创建页头
        if (strPageHeader[i]["name"] == "spmc" || strPageHeader[i]["name"] == "khmc") {
            continue;
        }
        else {
            strHeadP021403_lxd += "<li class='inp_150'>";
            strHeadP021403_lxd += strPageHeader[i]["xsmc"] + "：";
            strHeadP021403_lxd += "<input type='text'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
            strHeadP021403_lxd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strHeadP021403_lxd += "</li>";
        }
    }
    strHeadP021403_lxd += "</ul>";
    $("#" + strDivP021403_lxd + "_divform").append(strHeadP021403_lxd);

    strTableP021403_lxd += "<table id='Tp020501'></table>";     //创建表格
    $("#" + strDivP021403_lxd + "_divform").append(strTableP021403_lxd);

    strDownP021403_lxd += "<ul>";
    for (var i = 0; i < strpageFooter.length; i++) {        //创建页尾
        if (strpageFooter[i]["name"] == "czymc") {
            continue;
        }
        else if (strpageFooter[i]["name"] == "bz") {
            strDownP021403_lxd += "<li class='bz'>";
            strDownP021403_lxd += strpageFooter[i]["xsmc"] + "：";
            strDownP021403_lxd += "<input type='text'  value=''  name='" + strpageFooter[i]["name"] + "' disabled";
            strDownP021403_lxd += " id='" + strpageFooter[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strDownP021403_lxd += "</li>";
        }
        else {
            strDownP021403_lxd += "<li class='inp_150'>";
            strDownP021403_lxd += strpageFooter[i]["xsmc"] + "：";
            strDownP021403_lxd += "<input type='text'  value=''  name='" + strpageFooter[i]["name"] + "' disabled";
            strDownP021403_lxd += " id='" + strpageFooter[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strDownP021403_lxd += "</li>";
        }
    }
    strDownP021403_lxd += "</ul>";
    $("#" + strDivP021403_lxd + "_divform").append(strDownP021403_lxd);

    //释放内存
    strHeadP021403_lxd = null;
    strTableP021403_lxd = null;
    strDownP021403_lxd = null;
    objP021403_lxd = null;
    strPageHeader = null;
    strpageFooter = null;
}

//从后台加载所查单据数据
function myListP021403_lxd(strDivP021403_lxd, strDjbh) {
    try {
        var objP021403_lxd = {};                        //存放列配置
        var objResultP021403_lxd = {};              //存放返回的信息
        var myList = [];                                        //返回的数据集
        var count = 8;                                          //表格默认显示8行

        //获得jqGrid的列名和属性
        objP021403_lxd = ParseData(window.external.DoOptions("Tp020501", 'GetTableConfig', '{}'));
        for (var i = 0; i < objP021403_lxd.colModel.length; i++) {      //隐藏操作列
            if (objP021403_lxd.colModel[i].index == "cz") {
                objP021403_lxd.colModel[i].hidden = true;
            }
        }
        var objResultP021403_lxd = ParseData(window.external.DoOptions(strTableP021403_lxd, 'GetList', '{"djbh":"' + strDjbh + '"}'));

        //创建表格
        createGrid_P021403_lxd("Tp020501", objP021403_lxd.colNames, objP021403_lxd.colModel);

        $("#" + strTableP021403_lxd).setColProp("a1", { formatter: formatCountPlace });
        $("#" + strTableP021403_lxd).setColProp("a2", { formatter: formatMoneyPlace });
        $("#" + strTableP021403_lxd).setColProp("je", { formatter: formatMoneyPlace });

        myList = objResultP021403_lxd.Records;
        count = myList.length > 8 ? myList.length : 8;

        //添加行并为行分配id，并计算合计
        var lxsl = 0, lxje = 0;
        for (var i = 0; i < count; i++) {
            if (myList[i]) {
                if (myList[i].a1) {                                             //数量存在时
                    lxsl += parseFloat(myList[i].a1);
                }
                if (myList[i].je) {                                              //金额存在时
                    lxje += parseFloat(myList[i].je);
                }
                $("#" + strTableP021403_lxd).addRowData(i + 1, myList[i]);      //添加后台数据行
            }
            else {                                                                     //添加空行
                $("#" + strTableP021403_lxd).addRowData(i + 1, {});
            }
        }
        //调整表格自适应宽度
        resizeGridScroll(strTableP021403_lxd);

        //给流向日期赋值
        $("#djdate", "#" + strDivP021403_lxd).val(objResultP021403_lxd.djdate);
        //给流向药品赋值
        $("#spdm", "#" + strDivP021403_lxd).val(objResultP021403_lxd.spmc);
        //给配送客户赋值
        $("#khdm", "#" + strDivP021403_lxd).val(objResultP021403_lxd.khmc);
        //给制单人绑定值
        $("#czydm", "#" + strDivP021403_lxd).val(objResultP021403_lxd.czymc);
        //给备注绑定值
        $("#bz", "#" + strDivP021403_lxd).val(escape(objResultP021403_lxd.bz));
        //将最大id保存到grid
        $("#" + strTableP021403_lxd).data("nextNum", count + 1);

        //合计
        $("#" + strTableP021403_lxd).footerData("set", { rn: "合计",
            a1: lxsl.toFixed(window.parent.strCountPlace),
            je: lxje.toFixed(window.parent.strMoneyPlace)
        });

        //释放内存
        objP021403_lxd = null;
        objResultP021403_lxd = null;
        myList = null;
    } catch (e) { }
}

//数量的格式化
function formatCountPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}

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

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
-----------------------------------------------------------------*/
function createGrid_P021403_lxd(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        rownumbers: true,
        rownumWidth: 30,
        footerrow: true,
        autowidth: true
    });
    //设置表格序号
    $("#" + strTableID).setLabel("rn", "序号");
    //设置表格高度
    $("#" + strTableID).setGridHeight(225);
}