/*-------------------------------文档信息-----------------------------------
///功能：采购退货统计
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strTableP0113 = "Tp0113";         //要生成的grid。
var objSelRowP0113 = {};        //选择行的数据
var intSelRowIDP0113 = 0;       //选择行的Id         
var objWherejson = {};      //记录查询时的条件

//加载页面数据
function pageLoad_P0113() {
    loadSearchType_P0113();
    //日期下拉框加载
    dateExtent_P0113();
    //加载供应商
    loadGysData_P0113();
    //加载药品
    loadYpData_P0113();
    //查询数据
    searchData_P0113();
}

//加载表格数据
function createTable_P0113(strjson) {
    try {
        $("#" + strTableP0113).GridUnload(strTableP0113);
        var objConfig = ParseData(window.external.DoOptions(strTableP0113, "GetTableConfig", "{}"));
        //添加列格式化方法
        for (var i = 0; i < objConfig.colModel.length; i++) {

            if (objConfig.colModel[i].name == "sl") {
                objConfig.colModel[i].formatter = formatterP0113Count;
            }
            if (objConfig.colModel[i].name == "je" || objConfig.colModel[i].name == "dj") {
                objConfig.colModel[i].formatter = formatterP0113Money;
            }
            if (objConfig.colModel[i].name == "szbl") {
                objConfig.colModel[i].formatter = formatterP0113Percent;
            }
        }
        //创建表格
        var objResult = ParseData(window.external.DoOptions(strTableP0113, "GetList", strjson));
        createGrid(strTableP0113, objConfig.colNames, objConfig.colModel, objResult.Records, null, gridComplete_P0113, true, showDetail);
        //表格事件的添加
        addGridEvent_P0113(strTableP0113);
        resizeGrid(strTableP0113, true, false);
        resizeGridScroll(strTableP0113);
        objConfig = {};
        objResult = {};
    } catch (e) { }
}

//查询按钮的方法
function searchData_P0113() {
    try {
        //combobox的key不存在，赋值空
        if ($("#cmbGysP0113").combobox("getValue") == undefined) {
            $("#cmbGysP0113").combobox("setValue", "");
        }
        if ($("#cmbYpP0113").combobox("getValue") == undefined) {
            $("#cmbYpP0113").combobox("setValue", "");
        }
        //查询条件的取得
        var strWhereJson = "";
        strWhereJson = "{\"startDate\":\"" + $("#dateStartTimeP0113").val() + "\",";
        strWhereJson += "\"endDate\":\"" + $("#dateEndTimeP0113").val() + "\",";
        strWhereJson += "\"gysdm\":\"" + $("#cmbGysP0113").combobox("getValue") + "\",";
        strWhereJson += "\"spdm\":\"" + $("#cmbYpP0113").combobox("getValue") + "\"}";
        //记录查询条件
        objWherejson = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0113").combobox("getText") + "\",\"gysmc\":\"" + $("#cmbGysP0113").combobox("getText") + "\"}"));
        createTable_P0113(strWhereJson);

        strWhereJson = null;
        intSelRowIDP0113 = "";
        objSelRowP0113 = {};
    } catch (e) { }
}

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

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

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

//查询类型
function loadSearchType_P0113() {
    $("#cmbSelBy").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 42,
        onSelect: function (option) {
            searchType_P0113(option.dm);
        },
        enterDown: function () {
            searchType_P0113($(this).combobox("getValue"));
        }
    });
    $("#cmbSelBy").combobox("loadData", [{ dm: "按供应商", mc: "按供应商", selected: true }, { dm: "按药品", mc: "按药品"}]);
}

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

//添加事件
function addGridEvent_P0113(strTableID) {
    $("#" + strTableID).setGridParam({
        onSelectRow: function (rowId) {
            intSelRowIDP0113 = rowId;
            objSelRowP0113 = $(this).getRowData(rowId);
        }
    })
}

//合计
function gridComplete_P0113() {
    var intNumSl = $("#" + strTableP0113).getCol("sl", false, "sum");
    var intNumJe = $("#" + strTableP0113).getCol("je", false, "sum");
    var arrIds = $("#" + strTableP0113).getDataIDs();
    if (arrIds.length == 0) {
        $("#" + strTableP0113).footerData("set", { rn: "合计", sl: "&nbsp;", je: "&nbsp;" }, false);
    }
    else {
        $("#" + strTableP0113).footerData("set", { rn: "合计", sl: intNumSl, je: intNumJe }, true);
    }
    arrIds = null;
    intNumJe = null;
    intNumSl = null;
}

//改变tab标签的名称，切换iframe中的内容。
function showDetail() {
    try {
        if (intSelRowIDP0113 > 0) {
            //改变标签的Name
            $(window.parent.document).find("#span_113").text("采购退货明细");

            //选择的日期
            $(window.parent.document).find("#idIframe-11302").attr("dname", objWherejson.dname);
            $(window.parent.document).find("#idIframe-11302").attr("sdate", objWherejson.startDate);
            $(window.parent.document).find("#idIframe-11302").attr("edate", objWherejson.endDate);
            //供应商代码
            $(window.parent.document).find("#idIframe-11302").attr("gysdm", objSelRowP0113.gysdm ? objSelRowP0113.gysdm : objWherejson.gysdm);
            $(window.parent.document).find("#idIframe-11302").attr("gysmc", objSelRowP0113.gysdm ? objSelRowP0113.gysmc : objWherejson.gysmc);
            //药品代码
            $(window.parent.document).find("#idIframe-11302").attr("spdm", objSelRowP0113.spdm ? objSelRowP0113.spdm : objWherejson.spdm);
            //改变标签Iframe的Src；
            $(window.parent.document).find("#idIframe-113").css("display", "none");
            $(window.parent.document).find("#idIframe-11302").css("display", "block");
            $(window.parent.document).find("#idIframe-11302").attr("src", "01/p011302.htm");
            $(window.parent.document).find("#idIframe-11302").load(function () {
                //表格高度
                var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
                $(this).height(iHeight);
            });

        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) { }
}

//日期范围的选择方法
function dateExtent_P0113() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0113", "dateEndTimeP0113");

    //添加选择事件
    $("#cmbDateExtentP0113").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 102,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0113", "dateEndTimeP0113");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0113", "dateEndTimeP0113");
            //searchData_P0113();
        }
    });
    //加载数据
    $("#cmbDateExtentP0113").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "本年", mc: "本年" },
        { dm: "上一年", mc: "上一年"}]);
}

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

    //加载数据
    var objProvider = getComboboxData('Tgysxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部供应商", "selected": true });
    $("#cmbGysP0113").combobox("loadData", objProvider);
    objProvider = {};
}

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

    //加载数据
    var objDrug = getComboboxData('Tspxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbYpP0113").combobox("loadData", objDrug);
    objDrug = {};
}

//数据查询方式：按供应商，按药品
function searchType_P0113(strValue) {
    try {        
        if (strValue == "按供应商") {   //按供应商查询
            strTableP0113 = "Tp0113";
            $("#divTp0113").css("display", "");
            $("#divTp011301").css("display", "none");
        }        
        else if (strValue == "按药品") {   //按药品查询
            strTableP0113 = "Tp011301";
            $("#divTp0113").css("display", "none");
            $("#divTp011301").css("display", "");
            //strType = 2;
        }
        /*---------------------设置默认值------------------------*/
        objSelRowP0113 = {};
        intSelRowIDP0113 = "";
        searchData_P0113();
        strValue = null;
    } catch (e) { }
}

//导出数据的方法
function exportData() {
    makeExportData(strTableP0113, "采购退货统计");
}