/*--------------------------文档信息-------------------------------------------
///功能：      批号箱号流向查询
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0313 = "Tp0313";     //表格ID
var objSelRowP0313 = {};            //选择行的数据
var intSelRowIDP0313 = 0;         //选择行的ID
var objWhereJsonP0313 = {};     //记录查询时的条件

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    // $("#" + strDialog).dialog("close");
    cihoDlgClose(strDialog);                //关闭弹出层
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//页面加载的事件
function pageLoad_P0313() {
    try {
        //按往来单位加载数据
        loadWldw_P0313();
        //按药品加载数据
        loadSp_P0313();
        //流入流出加载数据
        loadInTo_P0313();
        //加载数据
        searchDataClick_P0313(true);
    } catch (e) { }
}

//查询按钮的事件
function searchDataClick_P0313(flag) {
    var strWhereJson = "";        //存放查询条件
    var objConfig = {};              //存放列配置对象
    var objResult = {};              //存放查询结果

    if (!$("#cmbWldwP0313").combobox("getValue")) {  //"当前选择的往来单位不存在，请重新选择！"
        $("#cmbWldwP0313").combobox("setValue", "");
    }
    if (!$("#cmbSpP0313").combobox("getValue")) {  // "当前选择的药品不存在，请重新选择！"
        $("#cmbSpP0313").combobox("setValue", "");
    }
    //清空选择行信息
    intSelRowIDP0313 = 0;
    objSelRowP0313 = {};
    if (flag == false) {       //flag为false时说明是点击查询按钮进来的
        if ($("#cmbPhP0313").val() == "" || $("#cmbPhP0313").val() == "输入批号") {
            window.parent.cihoAlert("输入批号不能为空", null, function (result) { $("#cmbPhP0313").focus(); });
            return;
        }
    }
    strWhereJson += "{\"wldm\":\"" + $("#cmbWldwP0313").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbSpP0313").combobox("getValue") + "\",";
    strWhereJson += "\"a2\":\"" + $("#cmbPhP0313").val() + "\",";
    if ($("#cmbXhP0313").val() == "输入箱号") {
        strWhereJson += "\"a3\":\"\",";
    }
    else {
        strWhereJson += "\"a3\":\"" + $("#cmbXhP0313").val() + "\",";
    }
    strWhereJson += "\"djtypebh\":\"" + $("#cmbInToP0313").combobox("getValue") + "\"}";
    //创建列属性
    objConfig = ParseData(window.external.DoOptions(strTableP0313, "GetTableConfig", "{}"));
    //格式化表格中的数据
    setData_P0313(objConfig.colModel);
    //创建表格
    if (flag == true) {     //初始加载时不给表格传数据
        createGrid_P0313(strTableP0313, objConfig.colNames, objConfig.colModel, []);
    }
    else {
        //从后台或者去数据集
        objResult = ParseData(window.external.DoOptions(strTableP0313, "GetList", strWhereJson));

        //创建表格
        createGrid_P0313(strTableP0313, objConfig.colNames, objConfig.colModel, objResult.Records);
    }
    //释放内存
    strWhereJson = null;
    objConfig = null;
    objResult = null;
}

//查看单据的事件
function viewDetail_P0313() {
    if (intSelRowIDP0313 > 0) {
        if (objSelRowP0313.djbh.substr(0, 4) == "CGTH") { //采购退货单
            showDialogP011302_fhd("divP0313_cgthd", objSelRowP0313.djbh);
        }
        else if (objSelRowP0313.djbh.substr(0, 4) == "CGFH") { //采购反货单
            showDialogP011202_fhd("divP0313_cgfhd", objSelRowP0313.djbh);
        }
        else if (objSelRowP0313.djbh.substr(0, 4) == "ZDXS" || objSelRowP0313.djbh.substr(0, 4) == "PSXS") { //终端销售单
            showDialogP021103_xsd("divP0313_xsd", objSelRowP0313.djbh);
        }
        else if (objSelRowP0313.djbh.substr(0, 4) == "XSTH") {  //销售退货单
            showDialogP021103_thd("divP0313_xsthd", objSelRowP0313.djbh);
        }
        else if (objSelRowP0313.djbh.substr(0, 2) == "YK" || objSelRowP0313.djbh.substr(0, 4) == "PSYK") {//移库单
            showDialogP031201_ykd("divP0313_ykd", objSelRowP0313.djbh);
        }
        else if (objSelRowP0313.djbh.substr(0, 2) == "BS") {//报损单
            showDialogP031201_bsd("divP0313_bsd", objSelRowP0313.djbh);
        }
        else if (objSelRowP0313.djbh.substr(0, 2) == "BY") { //报溢单
            showDialogP031201_byd("divP0313_byd", objSelRowP0313.djbh);
        }
        else if (objSelRowP0313.djbh.substr(0, 2) == "CG") { //采购单
            cgd(objSelRowP0313.djbh, "divP0313_cgd");
        }
    }
    else {
        //"请选择数据！"
        window.parent.cihoAlert(msg.other.selDataRow);
    }

}

//去除批号中的文字事件
function clearPhText() {
    $("#cmbPhP0313").val("");
}

//清除箱号中的文字事件
function clearXhText() {
    $("#cmbXhP0313").val("");
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//格式化表格中的数据的方法
function setData_P0313(objP0313) {
    for (var i = 0; i < objP0313.length; i++) {
        if (objP0313[i].name == "a1") {     //格式化数量
            objP0313[i].formatter = formatterP0313Count;
        }
    }
}

//按往来单位加载数据方法
function loadWldw_P0313() {
    $("#cmbWldwP0313").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        editable: true,
        onblur: function () {
            if ($("#cmbWldwP0313").combobox("getText") == "") {
                $("#cmbWldwP0313").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbWldwP0313").combobox("getText") == "全部往来单位") {
                $("#cmbWldwP0313").combobox("setText", "");
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });
    //加载数据
    var objKhxx = getComboboxData("Tkhxx2");
    var objGysxx = getComboboxData("Tgysxx2");
    var objGKxx = [];
    if (objKhxx == "{}") {
        objKhxx = [];
    }
    if (objGysxx == "{}") {
        objGysxx = [];
    }
    if (objKhxx != "{}") {
        objGKxx = objGysxx.concat(objKhxx);
    }
    else {
        objGKxx = objGysxx;
    }
    objGKxx.unshift({ "dm": "", "mc": "全部往来单位", "selected": true });
    $("#cmbWldwP0313").combobox("loadData", objGKxx);
    //释放内存
    objKhxx = null;
    objGysxx = null;
    objGKxx = null;
}

//按药品加载数据方法
function loadSp_P0313() {
    $("#cmbSpP0313").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbSpP0313").combobox("getText") == "") {
                $("#cmbSpP0313").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbSpP0313").combobox("getText") == "全部药品") {
                $("#cmbSpP0313").combobox("setText", "");
            }
        },
        enterDown: function () { }
    });

    //加载数据
    var objDrug = getComboboxData("Tspxx2");
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbSpP0313").combobox("loadData", objDrug);
    //释放内存
    objDrug = null;
}

//按箱号加载数据方法
function loadInTo_P0313() {
    $("#cmbInToP0313").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        panelHeight: 62,
        editable: false
    });
    //加载数据
    $("#cmbInToP0313").combobox("loadData", [{ dm: "", mc: "全部流向", selected: true }, { dm: "1", mc: "流入" }, { dm: "2", mc: "流出"}]);
}

//合计数据的计算方法
function totalCompute_P0313() {
    //设置合计行的数据
    var strRksl = parseFloat($("#" + strTableP0313).getCol("a1", false, "sum")).toFixed(window.parent.strCountPlace);
    var intIDLength = $("#" + strTableP0313).getDataIDs();
    if (intIDLength.length > 0) {
        $("#" + strTableP0313).footerData("set", { rn: "合计", a1: strRksl }, true);
    }
    else {
        $("#" + strTableP0313).footerData("set", { rn: "合计", a1: "&nbsp;" }, false);
    }
}

//数量的格式化
function formatterP0313Count(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    else {
        return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
    }
}

//当基本资料数据有修改时 刷新相关的combobox
function refreshMyCombobox(strModels) {
    var objGysxx = getComboboxData("Tgysxx2");                  //取得供应商的combobox数据
    var objKhxx = getComboboxData("Tkhxx2");                      //取得客户的combobox的数据

    for (var i = 0; i < strModels.length; i++) {
        var data = getComboboxData(strModels[i] + "2");
        if (strModels[i] == "Tspxx") {                      //药品基本信息变动时刷新药品数据
            data.unshift({ dm: "", mc: "全部药品" });
            reLoadCombobox("cmbSpP0313", data);
        }
        if (strModels[i] == "Tkhxx") {                      //客户基本信息变动时刷新往来单位
            if (!objGysxx) {
                objGysxx = [];
            }
            data.unshift({ dm: "", mc: "全部往来单位" });
            reLoadCombobox("cmbWldwP0313", objGysxx.concat(data));
        }
        if (strModels[i] == "Tgysxx") {                 //供应商基本信息变动时刷新往来单位
            if (!objKhxx) {
                objKhxx = [];
            }
            data.unshift({ dm: "", mc: "全部往来单位" });
            reLoadCombobox("cmbWldwP0313", data.concat(objKhxx));
        }
    }

    //释放内存
    objGysxx = null;
    objKhxx = null;
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
*****④objDataRecords：表格中需要加载的数据
-----------------------------------------------------------------*/
function createGrid_P0313(strTableID, objColNames, objColModel, objDataRecords) {
    $("#" + strTableID).GridUnload();       //删除旧表格
    if (objDataRecords == "{}") {              //当前数据源为空时，设置数据源变量为空对象
        objDataRecords = [];
    }
    //生成表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        sortname: "id",
        colNames: objColNames,
        colModel: objColModel,
        data: objDataRecords,
        rownumbers: true,
        autowidth: true,
        footerrow: true,
        gridview: true,
        rownumWidth: 30,
        cellsubmit: "clientArray",
        rowNum: objDataRecords.length,
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0313 = rowId;
            objSelRowP0313 = $(this).getRowData(rowId);
        },
        ondblClickRow: function () {
            viewDetail_P0313();
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");

    //设置行号列的名称
    //$("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
    $("#" + strTableID).setLabel("rn", "序号");

    //调整表格的大小
    resizeGrid(strTableP0313, true, false);
    //有滚动条时改变表格宽度
    resizeGridScroll(strTableP0313);
    //合计数据的计算
    totalCompute_P0313();
}

