/*-------------------------------文档信息-----------------------------------
///功能：销售退货统计明细
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP021202 = "Tp021202";    //表格ID
var objSelRowP021202 = {};        //选择行的数据
var intSelRowIDP021202 = 0;       //选择行的ID
var strPageP021202 = "divPageP021202";
var objPageParamP021202 = { sortfield: "djdate", orderby: "asc", where: "", flag: 0 };  //分页控件使用

//页面加载方法
function pageLoad_P021202() {
    //选择日期范围
    dateExtent_P021202();
    //加载客户信息
    loadCustomData_P021202();
    //药品下拉框信息的加载
    loadYpData_P021202();
    var objConfig = ParseData(window.external.DoOptions(strTableP021202, "GetTableConfig", "{}"));
    //添加列格式化方法
    for (var i = 0; i < objConfig.colModel.length; i++) {

        if (objConfig.colModel[i].name == "sl") {
            objConfig.colModel[i].formatter = formatterP021202Count;
        }
        if (objConfig.colModel[i].name == "je" || objConfig.colModel[i].name == "dj") {
            objConfig.colModel[i].formatter = formatterP021202Money;
        }
    }
    //创建表格
    createGrid(strTableP021202, objConfig.colNames, objConfig.colModel, [], null, null, true);
    //表格事件的添加
    addGridEvent_P021202();
    resizeGrid(strTableP021202, true, true);
    //创建分页
    createPage(strPageP021202, strTableP021202);
    //查询数据
    searchData_P021202();
    //resizeGrid(strTableP021202);
    objConfig = {};
}

//查询按钮事件
function searchData_P021202() {
    try {
        objPageParamP021202.sortfield = "djdate"; //排序字段
        objPageParamP021202.orderby = "asc";     //排序顺序
        objPageParamP021202.flag = 1;             //表示点击查询按钮
        loadGridDataP021202(true);                     //加载数据
        objPageParamP021202.flag = 0;              //使用完后，设置为零
        //resizeGrid(strTableP021202);
    } catch (e) { }
}

//加载数据
function loadGridDataP021202(strFlag) {
    var objResult = [];
    //combobox的key不存在，赋值空
    if (!$("#cmbCustomP021202").combobox("getValue")) {
        $("#cmbCustomP021202").combobox("setValue", "");
    }
    if (!$("#cmbYpP021202").combobox("getValue")) {
        $("#cmbYpP021202").combobox("setValue", "");
    }
    if (strFlag == true) {
        objPageParamP021202.where = getSelJson_P021202();
    }
    objResult = loadGridData_Page(objPageParamP021202);
    //后台返回的合计
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP021202).footerData("set", { rn: "合计",
                sl: objResult.sl.toFixed(window.parent.strCountPlace),
                je: objResult.je.toFixed(window.parent.strMoneyPlace)
            }, false);
        } else {
            $("#" + strTableP021202).footerData("set", { rn: "合计", sl: "&nbsp;", je: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP021202 = "";
    objSelRowP021202 = {};
}

//取得查询条件
function getSelJson_P021202() {
    //查询条件的取得
    var strWhereJson = "";
    strWhereJson = "\"sdate\":\"" + $("#dateStartTimeP021202").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP021202").val() + "\",";
    strWhereJson += "\"khdm\":\"" + $("#cmbCustomP021202").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbYpP021202").combobox("getValue") + "\"";
    return strWhereJson;
}

//查看单据按钮的方法
function viewReceipt_P021202() {
    if (intSelRowIDP021202 > 0) {
        showDialogP021103_thd("divP021202", objSelRowP021202.djbh);
    }
    else {
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

//返回统计页面的方法
function pageBack_P021202() {
    //改变标签的Name
    $(window.parent.document).find("#span_212").text("销售退货统计");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-21202").css("display", "none");
    $(window.parent.document).find("#idIframe-212").css("display", "block");
    $(window.parent.document).find("#idIframe-21202").attr("src", "");
}

//日期自定义
function dateTimeChange_P021202() {
    if ($("#cmbDateExtentP021202").combobox("getText") != "自定义") {
        $("#cmbDateExtentP021202").combobox("setValue", "");
        $("#cmbDateExtentP021202").combobox("setText", "自定义");
    }
}

//选择日期范围
function dateExtent_P021202() {
    //添加选择事件
    $("#cmbDateExtentP021202").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 102,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP021202", "dateEndTimeP021202");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP021202", "dateEndTimeP021202");
            //searchData_P021202();
        }
    });
    //加载数据
    $("#cmbDateExtentP021202").combobox("loadData", [{ dm: "本月", mc: "本月" }, 
        { dm: "上一月", mc: "上一月" },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "本年", mc: "本年" }, 
        { dm: "上一年", mc: "上一年"}]);
   
    $("#cmbDateExtentP021202").combobox("setValue", $(window.parent.document).find("#idIframe-21202").attr("dname"));
    $("#cmbDateExtentP021202").combobox("setText", $(window.parent.document).find("#idIframe-21202").attr("dname"));
    $("#dateStartTimeP021202").val($(window.parent.document).find("#idIframe-21202").attr("sdate"));
    $("#dateEndTimeP021202").val($(window.parent.document).find("#idIframe-21202").attr("edate"));
}

//客户下拉框信息的加载
function loadCustomData_P021202() {
    $("#cmbCustomP021202").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCustomP021202").combobox("getText") == "") {
                $("#cmbCustomP021202").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCustomP021202").combobox("getText") == "全部客户") {
                $("#cmbCustomP021202").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchData_P021202();
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

    //加载数据
    var objProvider = getComboboxData('Tkhxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户" });
    $("#cmbCustomP021202").combobox("loadData", objProvider);
    $("#cmbCustomP021202").combobox("setValue", $(window.parent.document).find("#idIframe-21202").attr("khdm"));
    $("#cmbCustomP021202").combobox("setText", $(window.parent.document).find("#idIframe-21202").attr("khmc").replace(/&nbsp;/g,""));
    objProvider={};
}

//药品下拉框信息的加载
function loadYpData_P021202() {
    $("#cmbYpP021202").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbYpP021202").combobox("getText") == "") {
                $("#cmbYpP021202").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbYpP021202").combobox("getText") == "全部药品") {
                $("#cmbYpP021202").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchData_P021202();
        }
    });

    //加载数据
    var objDrug = getComboboxData('Tspxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部药品" });
    $("#cmbYpP021202").combobox("loadData", objDrug);
    $("#cmbYpP021202").combobox("setValue", $(window.parent.document).find("#idIframe-21202").attr("spdm"));
    objDrug={};
}

//sl格式化Count
function formatterP021202Count(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
}

//dj,je格式化money
function formatterP021202Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

//szbl格式化
function formatterP021202Percent(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return (parseFloat(cellvalue) * 100).toFixed(window.parent.strPercentPlace) + "%";
}

//添加事件
function addGridEvent_P021202() {
    $("#" + strTableP021202).setGridParam({
        //当前选择行数据取得
        onSelectRow: function (rowId) {
            intSelRowIDP021202 = rowId;
            objSelRowP021202 = $(this).getRowData(rowId);
        },
        ondblClickRow: function (rowId) {   //双击事件
            viewReceipt_P021202();
        },
        onSortCol: function (index, colindex, sortorder) {  //排序
            //$("div.operate").hide();                    //浮动层隐藏
            objPageParamP021202.sortfield = index;        //排序字段
            objPageParamP021202.orderby = sortorder;      //排序顺序
            loadGridDataP021202(false);                        //加载数据
            return "stop";
        }
    });
}

//关闭div
function closeMyDlg(dlgId, strType, strTableName) {
    cihoDlgClose(dlgId);
}