/*--------------------------文档信息-------------------------------------------
///功能：      药品出入库明细
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP031201 = "Tp031201";               //表格ID
var strPageP031201 = "divPageP031201";    //分页层的ID
var objSelRowP031201 = {};                          //选择行的数据
var intSelRowIDP031201 = 0;                        //选择行的ID
var objPageParamP031201 = { sortfield: "", orderby: "", where: "", flag: 1 };     //分页控件使用

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

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//页面加载的的事件
function pageLoad_P031201() {
    try {
        //选择日期范围
        loadDateExtent_P031201();
        //加载药品信息
        loadSpData_P031201();
        //给仓库加载数据
        loadCk_P031201();
        objConfig = ParseData(window.external.DoOptions(strTableP031201, "GetTableConfig", "{}"));
        //格式化表格中的数据
        setData_P031201(objConfig.colModel);
        //创建表格
        createGrid(strTableP031201, objConfig.colNames, objConfig.colModel, [], null, null, true, null);
        //添加事件
        addGridEvent_P031201();
        //调整表格的大小
        resizeGrid(strTableP031201, true, true);
        //创建分页
        createPage(strPageP031201, strTableP031201);
        //加载数据
        searchDataClick_P031201();
    } catch (e) { }
}

//查询按钮事件
function searchDataClick_P031201() {
    try {
        var strWhereJson = "";      //存放查询条件
        var objConfig = {};             //存放类配置对象
        var objResult = {};              //存放查询结果
        if (!$("#cmbSpP031201").combobox("getValue")) { // "当前选择的药品不存在，
            window.parent.cihoAlert(msg.sz.yp.empOrNotExit, null, function () { $("#cmbSpP031201").focus(); });
            return false;
        }
        if (!$("#cmbCkP031201").combobox("getValue")) { // "当前选择的仓库不存在，赋值为【全部仓库】！"
            $("#cmbCkP031201").combobox("setValue", "");
        }

        //查询条件的取得
        objPageParamP031201.where = mySearchWhereJson();
        objPageParamP031201.flag = 1;
        objResult = loadGridData_Page(objPageParamP031201);
        var intIDLength = $("#" + strTableP031201).getDataIDs();
        if (intIDLength.length > 0) {
            $("#" + strTableP031201).footerData("set", { rn: "合计",
                sl: parseFloat(objResult.sl).toFixed(window.parent.strCountPlace),
                je: parseFloat(objResult.je).toFixed(window.parent.strMoneyPlace)
            }, false);
        }
        else {
            $("#" + strTableP031201).footerData("set", { rn: "合计",
                sl: "&nbsp;",
                je: "&nbsp;"
            }, false);
        }
        //释放内存
        objResult = null;
    } catch (e) { }
}

//返回按钮的事件
function pageBack_P031201() {
    //改变标签的Name
    $(window.parent.document).find("#span_312").text("药品出入库统计");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-312").css("display", ""); //显示统计页面
    $(window.parent.document).find("#idIframe-31201").css("display", "none"); //隐藏明细页面
    $(window.parent.document).find("#idIframe-31201").attr("src", "");
}

//查看单据按钮的事件
function viewReceipt_P031201() {
    if (intSelRowIDP031201 > 0) {
        if (objSelRowP031201.djbh.substr(0, 4) == "CGTH") { //采购退货单
            showDialogP011302_fhd("divP031201_cgthd", objSelRowP031201.djbh);
        }
        else if (objSelRowP031201.djbh.substr(0, 4) == "CGFH") {//采购反货单
            showDialogP011202_fhd("divP031201_cgfhd", objSelRowP031201.djbh);
        }
        else if (objSelRowP031201.djbh.substr(0, 4) == "ZDXS" || objSelRowP031201.djbh.substr(0, 4) == "PSXS") {  //终端/配送销售单
            showDialogP021103_xsd("divP031201_zdxsd", objSelRowP031201.djbh);
        }
        else if (objSelRowP031201.djbh.substr(0, 4) == "XSTH") {//销售退货单 
            showDialogP021103_thd("divP031201_xsthd", objSelRowP031201.djbh);
        }
        else if (objSelRowP031201.djbh.substr(0, 2) == "YK" || objSelRowP031201.djbh.substr(0, 4) == "PSYK") { //移库单
            showDialogP031201_ykd("divP031201_ykd", objSelRowP031201.djbh);
        }
        else if (objSelRowP031201.djbh.substr(0, 2) == "BS") {  //报损单
            showDialogP031201_bsd("divP031201_bsd", objSelRowP031201.djbh);
        }
        else if (objSelRowP031201.djbh.substr(0, 2) == "BY") { //报溢单
            showDialogP031201_byd("divP031201_byd", objSelRowP031201.djbh);
        }
        else if (objSelRowP031201.djbh.substr(0, 2) == "CG") {  //采购单
            cgd(objSelRowP031201.djbh, "divP031201_cgd");
        }
    }
    else {
        //"请选择数据！"
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

//表格中的事件
function addGridEvent_P031201() {
    $("#" + strTableP031201).setGridParam({
        onSelectRow: function (rowid) {
            intSelRowIDP031201 = rowid;
            objSelRowP031201 = $(this).getRowData(rowid);
        },
        gridComplete: function () {
            var arrDataRowID = $("#" + strTableP031201).getDataIDs();
            for (var intI = 0; intI < arrDataRowID.length; intI++) {
                if ($("#" + strTableP031201).getCell(arrDataRowID[intI], "djbh").substr(0, 4) == "CGTH"
                || $("#" + strTableP031201).getCell(arrDataRowID[intI], "djbh").substr(0, 4) == "ZDXS"
                || $("#" + strTableP031201).getCell(arrDataRowID[intI], "djbh").substr(0, 4) == "PSXS"
                || $("#" + strTableP031201).getCell(arrDataRowID[intI], "djbh").substr(0, 4) == "PSYK"
                || $("#" + strTableP031201).getCell(arrDataRowID[intI], "djbh").substr(0, 2) == "BS"
                || $("#" + strTableP031201).getCell(arrDataRowID[intI], "djname").substr(0, 2) == "移出") {
                    $("#" + arrDataRowID[intI]).css("color", "#996600");
                }
            }
        },
        ondblClickRow: function () {
            viewReceipt_P031201();
        },
        onSortCol: function (index, colindex, sortorder) {
            objPageParamP031201.sortfield = index;
            objPageParamP031201.orderby = sortorder;
            objPageParamP031201.flag = "";
            loadGridData_Page(objPageParamP031201);
            return "stop";
        }
    });
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法  开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//格式化表格中的数据方法
function setData_P031201(objP031201) {
    for (var i = 0; i < objP031201.length; i++) {
        if (objP031201[i].name == "sl") {   //数量的格式化
            objP031201[i].formatter = formatterP031201Count;
        }
        if (objP031201[i].name == "je" || objP031201[i].name == "dj") { //money的格式化
            objP031201[i].formatter = formatterP031201Money;
        }
        if (objP031201[i].name == "xh") {   //隐藏序号
            objP031201[i].hidden = true;
        }
    }
}

//日期范围修改方法
function dateTimeChange_P031201() {
    if ($("#cmbDateExtentP031201").combobox("getText") != "自定义") {
        $("#cmbDateExtentP031201").combobox("setValue", "");
        $("#cmbDateExtentP031201").combobox("setText", "自定义");
    }
}

//选择日期范围方法
function loadDateExtent_P031201() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP031201", "dateEndTimeP031201");

    //添加选择事件
    $("#cmbDateExtentP031201").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        panelHeight: 102,
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP031201", "dateEndTimeP031201");
        },
        enterDown: function () {
            dateExtentCalculation($("#cmbDateExtentP031201").combobox("getValue"), "dateStartTimeP031201", "dateEndTimeP031201");
        }
    });
    //加载数据
    $("#cmbDateExtentP031201").combobox("loadData",
    [{ dm: "本月", mc: "本月", selected: true },
    { dm: "上一月", mc: "上一月" },
    { dm: "本周", mc: "本周" },
    { dm: "过去三个月", mc: "过去三个月" },
    { dm: "本年", mc: "本年"}]);
    var strDateItem = $(window.parent.document).find("#idIframe-31201").attr("dname");
    if (strDateItem != "") {
        $("#cmbDateExtentP031201").combobox("setValue", strDateItem);
        dateExtentCalculation(strDateItem, "dateStartTimeP031201", "dateEndTimeP031201");
    }
    else {
        var strStartTime = $(window.parent.document).find("#idIframe-31201").attr("sdate");
        var strEndTime = $(window.parent.document).find("#idIframe-31201").attr("edate");
        $("#cmbDateExtentP031201").combobox("setText", "自定义");
        $("#dateStartTimeP031201").val(strStartTime);
        $("#dateEndTimeP031201").val(strEndTime);
    }
}

//给药品加载数据方法
function loadSpData_P031201() {
    $("#cmbSpP031201").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc"
    });
    //加载数据
    var objDrug = getComboboxData("Tspxx2");
    var strDrugID = $(window.parent.document).find("#idIframe-31201").attr("spdm");
    var strDrugText = $(window.parent.document).find("#idIframe-31201").attr("spmc");
    if (objDrug == "{}") {
        objDrug = [];
    }
    $("#cmbSpP031201").combobox("loadData", objDrug);
    //设定默认值
    $("#cmbSpP031201").combobox("setValue", strDrugID);
    $("#cmbSpP031201").combobox("setText", strDrugText);
    //释放内存
    objDrug = null;
}

//给仓库绑定值方法
function loadCk_P031201() {
    $("#cmbCkP031201").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCkP031201").combobox("getText") == "") {
                $("#cmbCkP031201").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCkP031201").combobox("getText") == "全部仓库") {
                $("#cmbCkP031201").combobox("setText", "");
            }
        },
        enterDown: function () { }
    });
    //加载数据
    var objDrug = getComboboxData("TCkxx2");
    var strDrugID = $(window.parent.document).find("#idIframe-31201").attr("ckdm");
    var strDrugText = $(window.parent.document).find("#idIframe-31201").attr("ckmc");
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部仓库", "selected": true });
    $("#cmbCkP031201").combobox("loadData", objDrug);
    //    //设定默认值
    $("#cmbCkP031201").combobox("setValue", strDrugID);
    $("#cmbCkP031201").combobox("setText", strDrugText);
    //释放内存
    objDrug = null;
}

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

//money格式化方法
function formatterP031201Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

//当基本资料数据有修改时 刷新相关的combobox方法
function refreshMyCombobox() {
    var strModels = $("#idRefreshCombobox").val();
    var data = getComboboxData(strModels + "2");

    if (strModels == "Tspxx") {
        reLoadCombobox("cmbSpP031201", data);
    }
    if (strModels == "TCkxx") {
        data.unshift({ dm: "", mc: "全部仓库", "selected": true });
        reLoadCombobox("cmbCkP031201", data);
    }
}

//查询条件的取得
function mySearchWhereJson() {
    var strWhereJson = "";                  //存放查询条件
    //查询条件的取得
    strWhereJson += "{\"sdate\":\"" + $("#dateStartTimeP031201").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP031201").val() + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbSpP031201").combobox("getValue") + "\",";
    strWhereJson += "\"ckdm\":\"" + $("#cmbCkP031201").combobox("getValue") + "\"}";
    return strWhereJson.replace(/[{}]/g, "");
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法  结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
