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

//页面全局变量的定义
var strTableP011102 = "Tp011102";               //表格ID
var strPageP011102 = "divPageP011102";    //分页层的ID
var strSelRowIDP011102 = "";                       //选择行数据ID
var objSelRowP011102 = {};                         //选择行的数据
var objPageParamP011102 = { sortfield: "djdate", orderby: "asc", where: "", flag: 1 };     //分页控件使用

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

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

//页面加载方法
function pageLoad_P011102() {
    try {
        //选择日期范围
        loadDateExtent_P011102();
        //加载供应商信息
        loadProviderData_P011102();
        //药品下拉框信息的加载
        loadDrugData_P011102();
        //获得jqGrid的列名和属性
        objConfig = ParseData(window.external.DoOptions(strTableP011102, "GetTableConfig", "{}"));
        //添加列格式化方法
        addColFormat_P011102(objConfig.colModel);
        //创建表格
        createGrid(strTableP011102, objConfig.colNames, objConfig.colModel, [], null, null, true, null);
        //添加事件
        addGridEvent_P011102();
        //调整一下表格的高度
        resizeGrid(strTableP011102, true, true);
        //创建分页
        createPage(strPageP011102, strTableP011102);
        //加载表格数据
        searchDataClick_P011102();
    } catch (e) { }
}

//查询按钮的方法
function searchDataClick_P011102() {
    try {
        var objResult = {};     //定义变量用于接收从后台获得的数据集
        //供应商
        if (!$("#cmbProviderP011102").combobox("getValue")) {
            $("#cmbProviderP011102").combobox("setValue", "");
        }
        //药品
        if (!$("#cmbDrugP011102").combobox("getValue")) {
            $("#cmbDrugP011102").combobox("setValue", "");
        }
        //查询条件的取得
        objPageParamP011102.where = mySearchWhereJson();
        objPageParamP011102.flag = 1;
        objResult = loadGridData_Page(objPageParamP011102);
        var intIDLength = $("#" + strTableP011102).getDataIDs();
        if (intIDLength.length > 0) {
            $("#" + strTableP011102).footerData("set", { rn: "合计",
                a1: parseFloat(objResult.a1).toFixed(window.parent.strCountPlace),
                jine: parseFloat(objResult.jine).toFixed(window.parent.strMoneyPlace),
                a8: parseFloat(objResult.a8).toFixed(window.parent.strMoneyPlace)
            }, false);
        }
        else {
            $("#" + strTableP011102).footerData("set", { rn: "合计",
                a1: "&nbsp;",
                jine: "&nbsp;",
                a8: "&nbsp;"
            }, false);
        }
    } catch (e) { }
}

//查看单据按钮的方法
function viewReceipt_P011102() {
    if (strSelRowIDP011102 != "") {
        if (objSelRowP011102.djbh.substr(0, 4) == "CGTH") {
            //退货单据
            showDialogP011302_fhd("divP011102_thd", objSelRowP011102.djbh);
        }
        else if (objSelRowP011102.djbh.substr(0, 2) == "CG") {
            //采购单据
            cgd(objSelRowP011102.djbh, "divP011102_cgd");
        }
    }
    else {
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

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

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

//表格事件方法
function addGridEvent_P011102() {
    $("#" + strTableP011102).setGridParam({
        onSelectRow: function (rowid) {
            strSelRowIDP011102 = rowid;
            objSelRowP011102 = $(this).getRowData(rowid);
        },
        gridComplete: function () {
            var arrDataRowID = $("#" + strTableP011102).getDataIDs();
            for (var intI = 0; intI < arrDataRowID.length; intI++) {
                if ($("#" + strTableP011102).getCell(arrDataRowID[intI], "djbh").substr(0, 4) == "CGTH") {
                    $("#" + arrDataRowID[intI]).css("color", "#996600");
                }
            }
        },
        ondblClickRow: function () {
            viewReceipt_P011102();
        },
        onSortCol: function (index, colindex, sortorder) {
            objPageParamP011102.sortfield = index;
            objPageParamP011102.orderby = sortorder;
            objPageParamP011102.flag = "";
            loadGridData_Page(objPageParamP011102);
            return "stop";
        }
    });
}

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



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

//选择日期范围
function loadDateExtent_P011102() {
    $("#cmbDateExtentP011102").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        panelHeight: 102,
        valueField: "dm",
        textField: "mc",
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP011102", "dateEndTimeP011102");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP011102", "dateEndTimeP011102");
        }
    });

    //加载数据
    $("#cmbDateExtentP011102").combobox("loadData", [{ dm: "本月", mc: "本月" }, { dm: "上一月", mc: "上一月" }, { dm: "本周", mc: "本周" }, { dm: "过去三个月", mc: "过去三个月" }, { dm: "本年", mc: "本年"}]);

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

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

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

    //释放内在
    objProvider = null;
}

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

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

    //释放内在
    objDrug = null;
}

//添加列格式化方法
function addColFormat_P011102(objColModel) {
    for (var intI = 0; intI < objColModel.length; intI++) {
        switch (objColModel[intI]["name"]) {
            case "a1": //数量
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
            case "a2": //单价
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strPricePlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
            case "jine": //金额
            case "a8": //应收发票金额
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
        }
    }
}

//查询条件的取得
function mySearchWhereJson() {
    var strWhereJson = "";
    //查询条件的取得
    strWhereJson += "{\"sdate\":\"" + $("#dateStartTimeP011102").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP011102").val() + "\",";
    strWhereJson += "\"gysdm\":\"" + $("#cmbProviderP011102").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbDrugP011102").combobox("getValue") + "\",";
    strWhereJson += "\"istuihuo\":\"" + $(window.parent.document).find("#idIframe-11102").attr("istuihuo") + "\"}";
    return strWhereJson.replace(/[{}]/g, "");
}

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