/*--------------------------文档信息-------------------------------------------
///功能：      进项发票统计
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0411 = "Tp0411";     //表格ID
var objSelRowP0411 = {};            //选择行的数据
var intSelRowIDP0411 = 0;         //选择行的ID
var objWhereJsonP0411 = {};     //记录查询时的条件
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载的事件
function pageLoad_P0411() {
    try {
        //查询类型
        loadSearchType_P0411();
        //选择日期范围
        loadDateExtent_P0411();
        //加载供应商信息
        loadGysData_P0411();
        //药品下拉框信息的加载
        loadSpData_P0411();
        //加载数据
        searchDataClick_P0411();
    } catch (e) { }
}

//查询按钮的事件
function searchDataClick_P0411() {
    try {
        var strWhereJson = "";      //存放查询条件
        var objConfig = {};              //存放列配置
        var objResult = {};              //存放查询的结果

        if (!$("#cmbGysP0411").combobox("getValue")) {  //当前选择的客户不存在，赋值为【全部供应商】！
            $("#cmbGysP0411").combobox("setValue", "");
        }
        if (!$("#cmbSpP0411").combobox("getValue")) { // "当前选择的药品不存在，赋值为【全部药品】！"
            $("#cmbSpP0411").combobox("setValue", "");
        }
        //清空选择行信息
        intSelRowIDP0411 = 0;
        objSelRowP0411 = {};
        //查询条件的取得
        strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0411").val() + "\",";
        strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0411").val() + "\",";
        strWhereJson += "\"gysdm\":\"" + $("#cmbGysP0411").combobox("getValue") + "\",";
        strWhereJson += "\"spdm\":\"" + $("#cmbSpP0411").combobox("getValue") + "\"}";

        //记录查询条件
        objWhereJsonP0411 = ParseData(strWhereJson.replace("}",
        ",\"dname\":\"" + $("#cmbDateExtentP0411").combobox("getValue") + "\",\"dname1\":\"" + $("#cmbDateExtentP0411").combobox("getText") + "\",\"gysmc\":\"" + $("#cmbGysP0411").combobox("getText") + "\",\"spmc\":\"" + $("#cmbSpP0411").combobox("getText") + "\"}"));
        objConfig = ParseData(window.external.DoOptions(strTableP0411, "GetTableConfig", "{}"));

        //格式化数据
        setData_P0411(objConfig.colModel);

        //创建表格
        objResult = ParseData(window.external.DoOptions(strTableP0411, "GetList", strWhereJson));
        createGrid_P0411(strTableP0411, objConfig.colNames, objConfig.colModel, objResult.Records);

        //释放内存
        strWhereJson = null;
        objConfig = null;
        objResult = null;
    } catch (e) { }
}

//查看明细按钮事件
function viewDetail_P0411() {
    if (intSelRowIDP0411 > 0) {
        //改变标签的Name 
        $(window.parent.document).find("#span_411").text("往来明细");

        //选择的日期
        $(window.parent.document).find("#idIframe-41102").attr("dname", objWhereJsonP0411.dname);
        $(window.parent.document).find("#idIframe-41102").attr("sdate", objWhereJsonP0411.sdate);
        $(window.parent.document).find("#idIframe-41102").attr("edate", objWhereJsonP0411.edate);

        //供应商
        $(window.parent.document).find("#idIframe-41102").attr("gysdm", objSelRowP0411.gysdm ? objSelRowP0411.gysdm : objWhereJsonP0411.gysdm);
        $(window.parent.document).find("#idIframe-41102").attr("gysmc", objSelRowP0411.gysdm ? objSelRowP0411.gysmc : objWhereJsonP0411.gysmc);

        //药品
        $(window.parent.document).find("#idIframe-41102").attr("spdm", objSelRowP0411.spdm ? objSelRowP0411.spdm : objWhereJsonP0411.spdm);
        $(window.parent.document).find("#idIframe-41102").attr("spmc", objSelRowP0411.spdm ? objSelRowP0411.spmc : objWhereJsonP0411.spmc);

        //改变标签Iframe的Src
        $(window.parent.document).find("#idIframe-411").css("display", "none"); //隐藏统计页面
        $(window.parent.document).find("#idIframe-41102").css("display", ""); //显示明页面
        $(window.parent.document).find("#idIframe-41102").attr("src", "04/p041102.htm");

        //调整iframe的高度
        $(window.parent.document).find("#idIframe-41102").load(function () {
            var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
            $(this).height(iHeight);
        });
    }
    else {
        //"请选择数据！"
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

//查询类型的选择事件
function selectSearchType_P0411(strValue) {
    //设置选择的查询类型
    if (strValue == "按供应商") {
        strTableP0411 = "Tp0411";
        $("#divTp0411").css("display", "");
        $("#divTp041101").css("display", "none");
    }
    else if (strValue == "按药品") {
        strTableP0411 = "Tp041101";
        $("#divTp0411").css("display", "none");
        $("#divTp041101").css("display", "");
    }
    /*--------设置默认值--------*/
    objSelRowP0411 = {};
    intSelRowIDP0411 = 0;
    //加载数据
    searchDataClick_P0411();
}

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


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

//格式化表格中的数据方法
function setData_P0411(objP0411) {
    for (var i = 0; i < objP0411.length; i++) {
        if (objP0411[i].name == "qcje" || objP0411[i].name == "ysje" ||
            objP0411[i].name == "yisje" || objP0411[i].name == "wsje") {    //格式化成money的格式
            objP0411[i].formatter = formatterP0411Money;
        }
        else if (objP0411[i].name == "qcsl" || objP0411[i].name == "yssl" ||
                    objP0411[i].name == "yissl" || objP0411[i].name == "wssl") {    //格式化成整数
            objP0411[i].formatter = formatterP0411Count;
        }
    }
}

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

//给查询类型加载数据方法
function loadSearchType_P0411() {
    $("#cbmSearchTypeP0411").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        panelHeight: 42,
        editable: false,
        onSelect: function (option) {
            selectSearchType_P0411(option.dm);
        },
        enterDown: function () {
            selectSearchType_P0411($("#cbmSearchTypeP0411").combobox("getValue"));
        }
    });
    //加载数据
    $("#cbmSearchTypeP0411").combobox("loadData", [{ dm: "按供应商", mc: "按供应商", selected: true }, { dm: "按药品", mc: "按药品"}]);
}

//选择日期范围
function loadDateExtent_P0411() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0411", "dateEndTimeP0411");

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

//给供应商加载数据方法
function loadGysData_P0411() {
    $("#cmbGysP0411").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbGysP0411").combobox("getText") == "") {
                $("#cmbGysP0411").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbGysP0411").combobox("getText") == "全部供应商") {
                $("#cmbGysP0411").combobox("setText", "");
            }
        },
        enterDown: function () { }
    });

    //加载数据
    var objDrug = getComboboxData("Tgysxx2");
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部供应商", "selected": true });
    $("#cmbGysP0411").combobox("loadData", objDrug);
    //释放内存
    objDrug = null;
}

//给药品加载数据方法
function loadSpData_P0411() {
    $("#cmbSpP0411").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbSpP0411").combobox("getText") == "") {
                $("#cmbSpP0411").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbSpP0411").combobox("getText") == "全部药品") {
                $("#cmbSpP0411").combobox("setText", "");
            }
        },
        enterDown: function () { }
    });

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

//计算合计的方法
function totalCompute_P0411() {
    var strQcMoney = parseFloat($("#" + strTableP0411).getCol("qcje", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var strQcCount = parseFloat($("#" + strTableP0411).getCol("qcsl", false, "sum")).toFixed(window.parent.strCountPlace);
    var strYingsMoney = parseFloat($("#" + strTableP0411).getCol("ysje", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var strYsCount = parseFloat($("#" + strTableP0411).getCol("yssl", false, "sum")).toFixed(window.parent.strCountPlace);
    var strYisMoney = parseFloat($("#" + strTableP0411).getCol("yisje", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var strYisCount = parseFloat($("#" + strTableP0411).getCol("yissl", false, "sum")).toFixed(window.parent.strCountPlace);
    var strWsMoney = parseFloat($("#" + strTableP0411).getCol("wsje", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var strWsCount = parseFloat($("#" + strTableP0411).getCol("wssl", false, "sum")).toFixed(window.parent.strCountPlace);
    var intIDLength = $("#" + strTableP0411).getDataIDs();
    if ($("#cbmSearchTypeP0411").combobox("getValue") == "按客户") {
        if (intIDLength.length > 0) {                       //表格中有记录时，合计行中的数据正常格式化
            $("#" + strTableP0411).footerData("set", { rn: "合计",
                qcje: strQcMoney,
                ysje: strYingsMoney,
                yisje: strYisMoney,
                wsje: strWsMoney
            }, true);
        }
        else {                                                          //表格中没有记录时，合计行中的数据格式化成空
            $("#" + strTableP0411).footerData("set", { rn: "合计",
                qcje: "&nbsp;",
                ysje: "&nbsp;",
                yisje: "&nbsp;",
                wsje: "&nbsp;"
            }, false);
        }
    }
    else {
        if (intIDLength.length > 0) {                       //表格中有记录时，合计行中的数据正常格式化
            $("#" + strTableP0411).footerData("set", { rn: "合计",
                qcsl: strQcCount,
                qcje: strQcMoney,
                yssl: strYsCount,
                ysje: strYingsMoney,
                yissl: strYisCount,
                yisje: strYisMoney,
                wssl: strWsCount,
                wsje: strWsMoney
            }, true);
        }
        else {                                                   //表格中没有记录时，合计行中的数据格式化成空
            $("#" + strTableP0411).footerData("set", { rn: "合计",
                qcsl: "&nbsp;",
                qcje: "&nbsp;",
                yssl: "&nbsp;",
                ysje: "&nbsp;",
                yissl: "&nbsp;",
                yisje: "&nbsp;",
                wssl: "&nbsp;",
                wsje: "&nbsp;"
            }, false);
        }
    }

}

//money的格式化方法
function formatterP0411Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

//数量的格式化方法
function formatterP0411Count(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
}

//当基本资料数据有修改时 刷新相关的combobox方法
function refreshMyCombobox(strModels) {
    if ($(window.parent.document).find('#idIframe-41102').length > 0) {
        $(window.parent.document.getElementById("idIframe-41102").contentWindow.document.getElementById("idRefreshCombobox")).val(strModels[0]);
        $(window.parent.document.getElementById("idIframe-41102").contentWindow.document.getElementById("idRefreshCombobox")).click();
    }
    for (var i = 0; i < strModels.length; i++) {
        var data = getComboboxData(strModels[i] + "2");

        if (strModels[i] == "Tgysxx") {
            data.unshift({ dm: "", mc: "全部供应商", "selected": true });
            reLoadCombobox("cmbGysP0411", data);
        }
        if (strModels[i] == "Tspxx") {
            data.unshift({ dm: "", mc: "全部药品", "selected": true });
            reLoadCombobox("cmbSpP0411", data);
        }
    }
}

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

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
*****④objDataRecords：表格中需要加载的数据
-----------------------------------------------------------------*/
function createGrid_P0411(strTableID, objColNames, objColModel, objDataRecords) {
    $("#" + strTableID).GridUnload();       //删除旧表格
    if (objDataRecords == "{}") {              //当前数据源为空时，设置数据源变量为空对象
        objDataRecords = [];
    }
    //生成表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        sortname: "id",
        colNames: objColNames,
        colModel: objColModel,
        data: objDataRecords,
        rownumbers: true,
        autowidth: true,
        footerrow: true,
        gridview: true,
        rownumWidth: 30,
        cellsubmit: "clientArray",
        rowNum: objDataRecords.length,
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0411 = rowId;
            objSelRowP0411 = $(this).getRowData(rowId);
        },
        ondblClickRow: function () {
            viewDetail_P0411();
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");

    //设置行号列的名称
    //$("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
    $("#" + strTableID).setLabel("rn", "序号");

    //设置表格的高度
    //调整表格的大小
    resizeGrid(strTableP0411, true, false);
    //有滚动条时改变表格宽度
    resizeGridScroll(strTableP0411);
    //合计数据的计算
    totalCompute_P0411();
}

