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

//销售单的查看单据
function showDialogP021103_xsd(strDivP021103_xsd, strDjbh) {
    try {
        $(".operate").remove();                             //清除浮动按钮层
        $("#" + strDivP021103_xsd).empty();      //清空弹出窗口内容

        //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
        creatDiv_ShowDlgHaveTbl(strDivP021103_xsd, "销售单", false, false, strTableP021103_xsd);  //创建弹出窗口

        //添加表单元素到窗口中
        tableFormP021103_xsd(strDivP021103_xsd, strDjbh)

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

        //在窗口中的元素添加完毕后，给dialog定位
        //$("#" + strDivP021103_xsd).dialog("option", "position", "center");

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

//创建表单
function tableFormP021103_xsd(strDivP021103_xsd, strDjbh) {
    var strHeadP021103_xsd = "";            //存放页头内容
    var strTablep021103_xsd = "";            //存放表格内容
    var strDownP021103_xsd = "";            //存放页尾内容
    var objP021103_xsd = {};                    //存放表格中的列配置
    var strPageHeader = [];                       //存放页头的列配置
    var strpageFooter = [];                        //存放页尾的列配置

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

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

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

    strTablep021103_xsd = "<table id='Tp020101'></table>";          //创建表格
    $("#" + strDivP021103_xsd + "_divform").append(strTablep021103_xsd);

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

    //释放内存
    strHeadP021103_xsd = null;
    strTablep021103_xsd = null;
    strDownP021103_xsd = null;
    objP021103_xsd = null;
    strPageHeader = null;
    strpageFooter = null;
}

//从后台加载所查单据数据
function myListP021103_xsd(strDivP021103_xsd, strDjbh) {
    try {
        var objP021103_xsd = {};                //存放类配置
        var objResultP021103_xsd = {};      //存放返回信息
        var myList = [];        //初始默认返回的数据为空
        var count = 5;          //初始默认显示5条数据

        //获得jqGrid的列名和属性
        objP021103_xsd = ParseData(window.external.DoOptions(strTableP021103_xsd, 'GetTableConfig', '{}'));
        objResultP021103_xsd = ParseData(window.external.DoOptions(strTableP021103_xsd, 'GetList', '{"djbh":"' + strDjbh + '"}'));

        //创建表格
        createGrid_P021103_xsd(strTableP021103_xsd, objP021103_xsd.colNames, objP021103_xsd.colModel);

        //格式化表格中的数据
        $("#" + strTableP021103_xsd).setColProp("a1", { formatter: formatCountPlace });
        $("#" + strTableP021103_xsd).setColProp("a2", { formatter: formatMoneyPlace });
        $("#" + strTableP021103_xsd).setColProp("je", { formatter: formatMoneyPlace });
        $("#" + strTableP021103_xsd).setColProp("a4", { formatter: formatCountPlace });
        $("#" + strTableP021103_xsd).setColProp("a5", { formatter: formatMoneyPlace });
        $("#" + strTableP021103_xsd).setColProp("kpje", { formatter: formatMoneyPlace });

        myList = objResultP021103_xsd.Records;                  //把从后台获取的数据赋给【myList】
        count = myList.length > 5 ? myList.length : 5;            //表格最少显示5条数据

        //添加行并为行分配id，并计算合计
        var ypsl = 0, ypje = 0, kpsl = 0, kpje = 0;
        for (var i = 0; i < count; i++) {
            if (myList[i]) {
                if (myList[i].a1) {
                    ypsl += parseFloat(myList[i].a1);
                }
                if (myList[i].je) {
                    ypje += parseFloat(myList[i].je);
                }
                if (myList[i].a4) {
                    kpsl += parseFloat(myList[i].a4);
                }
                if (myList[i].kpje) {
                    kpje += parseFloat(myList[i].kpje);
                }
                //添加后台数据行
                if (myList[i].dp) {
                    if (myList[i].dp == "True") {                           //true时给其赋值为【是】
                        myList[i].dp = "是";
                    }
                    else if (myList[i].dp == "False") {                     //false时给其赋值为【否】
                        myList[i].dp = "否";
                    }
                    $("#" + strTableP021103_xsd).addRowData(i + 1, myList[i]);      //给表格加载数据
                }
            }
            else {                                                                            //添加空行
                $("#" + strTableP021103_xsd).addRowData(i + 1, {});
            }
        }

        //调整表格自适应的宽度
        resizeGridScroll(strTableP021103_xsd);

        //给返货日期绑定值
        $("#djdate", $("#" + strDivP021103_xsd + "_divform")).val(objResultP021103_xsd.djdate);
        //给客户绑定值
        $("#khdm", $("#" + strDivP021103_xsd + "_divform")).val(objResultP021103_xsd.khmc);
        //给经销人绑定值
        $("#zydm", $("#" + strDivP021103_xsd + "_divform")).val(objResultP021103_xsd.zymc);
        if (objResultP021103_xsd.xslx == "201") {
            $("#xslx", $("#" + strDivP021103_xsd + "_divform")).val("终端销售");
        }
        if (objResultP021103_xsd.xslx == "202") {
            $("#xslx", $("#" + strDivP021103_xsd + "_divform")).val("配送销售");
        }
        //给销售类型绑定值
        $("#ckdm", $("#" + strDivP021103_xsd + "_divform")).val(objResultP021103_xsd.ckmc);
        //给运输单位绑定值
        $("#ysdw", $("#" + strDivP021103_xsd + "_divform")).val(objResultP021103_xsd.ysdw);
        //给药品单位绑定值
        $("#ysdh", $("#" + strDivP021103_xsd + "_divform")).val(objResultP021103_xsd.ysdh);
        //给制单人绑定值
        $("#czydm", $("#" + strDivP021103_xsd + "_divform")).val(objResultP021103_xsd.czymc);
        //给备注绑定值
        $("#bz", $("#" + strDivP021103_xsd + "_divform")).val(escape(objResultP021103_xsd.bz));

        //将最大id保存到grid
        $("#" + strTableP021103_xsd).data("nextNum", count + 1);
        //将合计展示到grid底部
        $("#" + strTableP021103_xsd).footerData("set", { rn: "合计",
            a1: ypsl.toFixed(window.parent.strCountPlace),
            je: ypje.toFixed(window.parent.strMoneyPlace),
            a4: kpsl.toFixed(window.parent.strMoneyPlace),
            kpje: kpje.toFixed(window.parent.strMoneyPlace)
        });

        //释放内存
        objP021103_xsd = null;
        objResultP021103_xsd = null;
        myList = null;
        count = 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_P021103_xsd(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        rownumbers: true,
        rownumWidth: 30,
        footerrow: true,
        autowidth: true
    });
    $("#" + strTableID).setLabel("rn", "序号");
    //设置表格高度
    $("#" + strTableID).setGridHeight(141);
}