/*--------------------------文档信息-------------------------------------------
///
///功能：    采购管理-采购返利统计-返利明细
///创建人：  王思波
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///说明：    --------------
///
-----------------------------------------------------------------------------*/

//页面全局变量的定义
var strTableP011202 = "Tp011202";               //表格ID
var strPageP011202 = "divPageP011202";    //分页层的ID
var strSelRowIDP011202 = "";                       //选择行的ID
var objSelRowP011202 = {};                         //选择行的数据
var objPageParamP011202 = { sortfield: "djdate", orderby: "desc", where: "", flag: 1 };     //分页控件使用

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


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P011202() {
    try {
        //选择日期范围
        loadDateExtent_P011202();
        //加载供应商信息
        loadProviderData_P011202();
        //药品下拉框信息的加载
        loadDrugData_P011202();
        //数据的取得
        objConfig = ParseData(window.external.DoOptions(strTableP011202, "GetTableConfig", "{}"));
        //添加列格式化方法
        addColFormat_P011202(objConfig.colModel);
        //创建表格
        createGrid(strTableP011202, objConfig.colNames, objConfig.colModel, [], null, null, true, null);
        //添加事件
        addGridEvent_P011202();
        //调整表格的高度
        resizeGrid(strTableP011202, true, true);
        //创建分页
        createPage(strPageP011202, strTableP011202);
        //加载表格数据
        searchDataClick_P011202();
    } catch (e) { }
}

//查询按钮的方法
function searchDataClick_P011202() {
    try {
        var objResult = {};     //定义变量用于接收从后台获得的数据集
        //供应商
        if (!$("#cmbProviderP011202").combobox("getValue")) {
            $("#cmbProviderP011202").combobox("setValue", "");
        }
        //药品
        if (!$("#cmbDrugP011202").combobox("getValue")) {
            $("#cmbDrugP011202").combobox("setValue", "");
        }

        // 查询条件的取得
        objPageParamP011202.where = mySecarchWhereJson();
        objPageParamP011202.flag = 1;
        objResult = loadGridData_Page(objPageParamP011202);
        var intIDLength = $("#" + strTableP011202).getDataIDs();
        if (intIDLength.length > 0) {
            $("#" + strTableP011202).footerData("set", { rn: "合计",
                fhsl: parseFloat(objResult.fhsl).toFixed(window.parent.strCountPlace),
                je: parseFloat(objResult.je).toFixed(window.parent.strMoneyPlace),
                fxje: parseFloat(objResult.fxje).toFixed(window.parent.strMoneyPlace)
            }, false);
        }
        else {
            $("#" + strTableP011202).footerData("set", { rn: "合计",
                fhsl: "&nbsp;",
                je: "&nbsp;",
                fxje: "&nbsp;"
            }, false);
        }
    } catch (e) { }
}

//查看单据按钮的方法
function viewReceipt_P011202() {
    if (strSelRowIDP011202 != "") {
        if (objSelRowP011202.djbh.substr(0, 4) == "CGFH") {
            showDialogP011202_fhd("divP011202_fhd", objSelRowP011202.djbh);
        }
        else if (objSelRowP011202.djbh.substr(0, 4) == "CGFL") {
            showDialogP011202_fxd("divP011202_fxd", objSelRowP011202.djbh);
        }
    }
    else {
        window.parent.cihoAlert(msg.other.selDataRow);
    }

}

//返回按钮的方法
function pageBack_P011202() {
    //改变标签的Name
    $(window.parent.document).find("#span_112").text("采购返利统计");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-112").css("display", ""); //显示统计页面
    $(window.parent.document).find("#idIframe-11202").attr("src", "");
    $(window.parent.document).find("#idIframe-11202").css("display", "none"); //隐藏明细页面
}

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

//表格中添加的事件
function addGridEvent_P011202() {
    $("#" + strTableP011202).setGridParam({
        onSelectRow: function (rowid) {
            strSelRowIDP011202 = rowid;
            objSelRowP011202 = $(this).getRowData(rowid);
        },
        ondblClickRow: function () {
            viewReceipt_P011202();
        },
        onSortCol: function (index, colindex, sortorder) {
            objPageParamP011202.sortfield = index;
            objPageParamP011202.orderby = sortorder;
            objPageParamP011202.flag = "";
            loadGridData_Page(objPageParamP011202);
            return "stop";
        }
    });

}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/



/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//选择日期范围
function loadDateExtent_P011202() {
    $("#cmbDateExtentP011202").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        panelHeight: 102,
        valueField: "dm",
        textField: "mc",
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP011202", "dateEndTimeP011202");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP011202", "dateEndTimeP011202");
        }
    });
    //加载数据
    $("#cmbDateExtentP011202").combobox("loadData", [{ dm: "本月", mc: "本月" }, { dm: "上一月", mc: "上一月" }, { dm: "本周", mc: "本周" }, { dm: "过去三个月", mc: "过去三个月" }, { dm: "本年", mc: "本年"}]);

    //设置默认日期为本月
    $("#cmbDateExtentP011202").combobox("setValue", $(window.parent.document).find("#idIframe-11202").attr("dname"));
    $("#cmbDateExtentP011202").combobox("setText", $(window.parent.document).find("#idIframe-11202").attr("dname"));
    $("#dateStartTimeP011202").val($(window.parent.document).find("#idIframe-11202").attr("sdate"));
    $("#dateEndTimeP011202").val($(window.parent.document).find("#idIframe-11202").attr("edate"));
}

//供应下拉框信息的加载
function loadProviderData_P011202() {
    //添加变更事件
    $("#cmbProviderP011202").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbProviderP011202").combobox("getText") == "") {
                $("#cmbProviderP011202").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbProviderP011202").combobox("getText") == "全部供应商") {
                $("#cmbProviderP011202").combobox("setValue", "-1");
                $("#cmbProviderP011202").combobox("setText", "");
            }
        }
    });

    //供应商信息的取得
    var objProvider = getComboboxData("Tgysxx2");
    //加载数据
    objProvider.unshift({ "dm": "", "mc": "全部供应商" });
    $("#cmbProviderP011202").combobox("loadData", objProvider);
    //设定默认值
    $("#cmbProviderP011202").combobox("setValue", $(window.parent.document).find("#idIframe-11202").attr("gysdm"));
    $("#cmbProviderP011202").combobox("setText", $(window.parent.document).find("#idIframe-11202").attr("gysmc"));

    //释放内存
    objProvider = null;
}

//药品下拉框信息的加载
function loadDrugData_P011202() {
    //添加变更事件
    $("#cmbDrugP011202").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbDrugP011202").combobox("getText") == "") {
                $("#cmbDrugP011202").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbDrugP011202").combobox("getText") == "全部药品") {
                $("#cmbDrugP011202").combobox("setValue", "-1");
                $("#cmbDrugP011202").combobox("setText", "");
            }
        }
    });

    //药品信息的取得
    var objDrug = getComboboxData("Tspxx2");
    //加载数据
    objDrug.unshift({ "dm": "", "mc": "全部药品" });
    $("#cmbDrugP011202").combobox("loadData", objDrug);
    //设定默认值
    $("#cmbDrugP011202").combobox("setValue", $(window.parent.document).find("#idIframe-11202").attr("spdm"));
    $("#cmbDrugP011202").combobox("setText", $(window.parent.document).find("#idIframe-11202").attr("spmc"));

    //释放内存
    objDrug = null;
}

//添加列格式化方法
function addColFormat_P011202(objColModel) {
    for (var intI = 0; intI < objColModel.length; intI++) {
        switch (objColModel[intI]["name"]) {
            case "fhsl": //返货数量
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (rowObject.djbh != undefined && rowObject.djbh.substr(0, 4) == "CGFH") {
                        if (cellvalue != "") {
                            return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
                        }
                        else {
                            return "0";
                        }
                    }
                    else {
                        return "&nbsp;";
                    }
                }
                break;
            case "dj": //单价
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (rowObject.djbh != undefined && rowObject.djbh.substr(0, 4) == "CGFH") {
                        if (cellvalue != "") {
                            return parseFloat(cellvalue).toFixed(window.parent.strPricePlace);
                        }
                        else {
                            return "0";
                        }
                    }
                    else {
                        return "&nbsp;";
                    }
                }
                break;
            case "je":      //金额
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (rowObject.djbh != undefined && rowObject.djbh.substr(0, 4) == "CGFH") {
                        if (cellvalue != "") {
                            return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                        }
                        else {
                            return "0";
                        }
                    }
                    else {
                        return "&nbsp;";
                    }
                }
                break;
            case "fxje":    //返现金额
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (rowObject.djbh != undefined && rowObject.djbh.substr(0, 4) == "CGFL") {
                        if (cellvalue != "") {
                            return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                        }
                        else {
                            return "0";
                        }
                    }
                    else {
                        return "&nbsp;";
                    }
                }
                break;
        }
    }
}

//查询条件的取得
function mySecarchWhereJson() {
    strWhereJson = "";                      //存放查询条件的变量
    //查询条件的取得
    strWhereJson += "{\"sdate\":\"" + $("#dateStartTimeP011202").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP011202").val() + "\",";
    strWhereJson += "\"gysdm\":\"" + $("#cmbProviderP011202").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbDrugP011202").combobox("getValue") + "\"}";
    return strWhereJson.replace(/[{}]/g, "");
}

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