/*-------------------------------文档信息-----------------------------------
///功能：采购退货统计明细
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP011302 = "Tp011302";    //表格ID
var objSelRowP011302 = {};        //选择行的数据
var intSelRowIDP011302 = 0;       //选择行的ID
var strPageP011302 = "divPageP011302";
var objPageParamP011302 = { sortfield: "djdate", orderby: "asc", where: "", flag: 0 };  //分页控件使用

//页面加载方法
function pageLoad_P011302() {
    try {
        //选择日期范围
        dateExtent_P011302();
        //加载供应商信息
        loadGysData_P011302();
        //药品下拉框信息的加载
        loadYpData_P011302();

        var objConfig = ParseData(window.external.DoOptions(strTableP011302, "GetTableConfig", "{}"));
        //格式化数子列
        addColFormatter_P011302(objConfig.colModel);
        createGrid(strTableP011302, objConfig.colNames, objConfig.colModel, [], null, null, true);
        //表格事件的添加
        addGridEvent_P011302();
        resizeGrid(strTableP011302, true, true);
        //创建分页
        createPage(strPageP011302, strTableP011302);
        //查询数据
        searchData_P011302();
        //resizeGridScroll(strTableP011302);
        objConfig = {};
    } catch (e) { }
}

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

//加载数据的事件
function loadGridDataP011302(strFlag) {
    var objResult = [];
    if ($("#cmbGysP011302").combobox("getValue") == undefined) {
        $("#cmbGysP011302").combobox("setValue", "");
    }
    if ($("#cmbYpP011302").combobox("getValue") == undefined) {
        $("#cmbYpP011302").combobox("setValue", "");
    }
    if (strFlag==true) {
        objPageParamP011302.where = getSelJson_P011302();
    }
    objResult = loadGridData_Page(objPageParamP011302);
    //后台返回的合计
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP011302).footerData("set", { rn: "合计",
                sl: objResult.sl.toFixed(window.parent.strCountPlace),
                je: objResult.je.toFixed(window.parent.strMoneyPlace)
            }, false);
        } else {
            $("#" + strTableP011302).footerData("set", { rn: "合计", sl: "&nbsp;", je: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP011302 = "";
    objSelRowP011302 = {};
}

//得到查询字符串
function getSelJson_P011302() {
    //查询条件的取得
    var strWhereJson = "";
    strWhereJson = "\"startDate\":\"" + $("#dateStartTimeP011302").val() + "\",";
    strWhereJson += "\"endDate\":\"" + $("#dateEndTimeP011302").val() + "\",";
    strWhereJson += "\"gysdm\":\"" + $("#cmbGysP011302").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbYpP011302").combobox("getValue") + "\"";
    return strWhereJson;
}

//查看单据按钮的方法
function viewReceipt_P011302() {
    if (intSelRowIDP011302 > 0) {
        showDialogP011302_fhd("divP011302", objSelRowP011302.djbh);
    }
    else {
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

//返回统计页面
function pageBack_P011302() {
    //改变标签的Name
    $(window.parent.document).find("#span_113").text("采购退货统计");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-11302").css("display", "none");
    $(window.parent.document).find("#idIframe-113").css("display", "block");
    $(window.parent.document).find("#idIframe-11302").attr("src", "");  
}

//时间范围改变的事件
function dateTimeChange_P011302() {
    if ($("#cmbDateExtentP011302").combobox("getText") != "自定义") {
        $("#cmbDateExtentP011302").combobox("setValue", "");
        $("#cmbDateExtentP011302").combobox("setText", "自定义");
    }
}

//选择日期范围
function dateExtent_P011302() {
    //添加选择事件
    $("#cmbDateExtentP011302").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 102,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP011302", "dateEndTimeP011302");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP011302", "dateEndTimeP011302");
            //searchData_P011302();
        }
    });
    //加载数据
    $("#cmbDateExtentP011302").combobox("loadData", [{ dm: "本月", mc: "本月" }, { dm: "上一月", mc: "上一月" }, { dm: "过去三个月", mc: "过去三个月" }, { dm: "本年", mc: "本年" }, { dm: "上一年", mc: "上一年"}]);
    //设置默认日期为本月
    $("#cmbDateExtentP011302").combobox("setValue", $(window.parent.document).find("#idIframe-11302").attr("dname"));
    $("#cmbDateExtentP011302").combobox("setText", $(window.parent.document).find("#idIframe-11302").attr("dname"));    
    $("#dateStartTimeP011302").val($(window.parent.document).find("#idIframe-11302").attr("sdate"));
    $("#dateEndTimeP011302").val($(window.parent.document).find("#idIframe-11302").attr("edate"));    
}

//供应下拉框信息的加载
function loadGysData_P011302() {
    $("#cmbGysP011302").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbGysP011302").combobox("getText") == "") {
                $("#cmbGysP011302").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbGysP011302").combobox("getText") == "全部供应商") {
                $("#cmbGysP011302").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchData_P011302();
        }
    });

    //加载数据
    var objProvider = getComboboxData('Tgysxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部供应商"});
    $("#cmbGysP011302").combobox("loadData", objProvider);
    $("#cmbGysP011302").combobox("setValue", $(window.parent.document).find("#idIframe-11302").attr("gysdm"));
    $("#cmbGysP011302").combobox("setText", $(window.parent.document).find("#idIframe-11302").attr("gysmc"))
    objProvider = {};
}

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

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


//添加列格式化方法
function addColFormatter_P011302(objColModel) {
    var intI = 0;
    for (intI = 0; intI < objColModel.length; intI++) {
        if (objColModel[intI]["name"] == "je" || objColModel[intI]["name"] == "dj") {
            objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                if (cellvalue != "" && cellvalue != 0) {
                    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                }
                else {
                    return "&nbsp;";
                }
            }
        }
        if (objColModel[intI]["name"] == "sl") {
            objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                if (cellvalue != "" && cellvalue != 0) {
                    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
                }
                else {
                    return "&nbsp;";
                }
            }
        }
    }
    intI = null;
}

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

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

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

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