/*--------------------------文档信息-------------------------------------------
///
///功能：    流向管理
///创建人：  王思波
///创建时间：2011年06月10日
///修改人：  --------------
///修改时间：--------------
///修改原因：--------------
///详细说明：--------------
///
-----------------------------------------------------------------------------*/

//页面全局变量
var strTableP0205 = "Tp0205";       //表格ID
var strPageP0205 = "divPageP0205";  //药品信息编辑窗口ID
var strSelRowIDP0205 = "";          //选择行的ID
var objSelRowP0205 = {};            //选择行的数据
var objParamP0205 = { sortfield: "", orderby: "", where: "", flag: 0 }; //分页控件使用

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**保存和关闭 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp020501") {
            saveData_P020501(strType);  //流向单
        }
        else if (strTable == "Tp020502") {
            saveData_P020502();         //建帐期初
        }
        else if (strTable == ciho.layal.table) {
            ciho.saveType();            //基本信息
        }
    } catch (e) { alert(e.message) }
}

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    //保存单元格内容
    $("#" + strTableP020501).saveCell(objCellPlace.iRow, objCellPlace.iCol);

    //当页面信息已修改时，弹出提示保存
    if ($("#" + strDialog + "_divform").data("change") == true) {
        window.parent.cihoAlert(msg.other.haveChange, msgBox.dlgBtn.YesNoCancel, function (result) {
            //点击按钮的处理
            if (result == msgBox.dlgRst.Yes) {
                saveMyData(strDialog, strType, strTable); //保存信息
            }
            else if (result == msgBox.dlgRst.No) {
                cihoDlgClose(strDialog);    //关闭窗口
                freeMemory_P020501();       //释放内存
            }
        });
    }
    else {
        cihoDlgClose(strDialog);    //关闭窗口
        freeMemory_P020501();       //释放内存
    }
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**保存和关闭 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/



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

//页面加载方法
function pageLoad_P0205() {
    try {
        //选择日期范围
        dateExtent_P0205();
        //加载客户信息
        loadClient_P0205();
        //药品下拉框信息的加载
        loadDrugData_P0205();

        //获得jqGrid的列名和属性
        var objConfig = ParseData(window.external.DoOptions(strTableP0205, "GetTableConfig", "{}"));
        //创建表格
        createGrid(strTableP0205, objConfig.colNames, objConfig.colModel, "{}", null, null, true, dblClickRow_P0205);
        //添加事件
        setGridEvent_P0205();
        //添加列格式化方法
        setColFormat_P0205();
        //调整表格的大小
        resizeGrid(strTableP0205, true, true);

        //创建分页
        createPage(strPageP0205, strTableP0205);

        //加载表格数据
        searchDataClick_P0205();

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

//查询按钮的方法
function searchDataClick_P0205() {
    try {
        //参数的更新
        objParamP0205.sortfield = "djdate"; //排序字段
        objParamP0205.orderby = "desc";     //排序顺序
        objParamP0205.flag = 1;             //表示点击查询按钮
        loadGridData_P0205();               //加载数据
        objParamP0205.flag = 0;             //使用完后，设置为零    

        //清空选择行信息
        strSelRowIDP0205 = "";
        objSelRowP0205 = {};
    } catch (e) { }
}

//删除按钮的方法
function deleteData_P0205() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                //删除数据信息
                var objResult = ParseData(window.external.DoOptions(strTableP0205, 'Delete', "{\"djbh\":\"" + objSelRowP0205.djbh + "\"}"));

                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    objParamP0205.flag = 2; //表示点击查询按钮
                    loadGridData_P0205();   //加载数据
                    objParamP0205.flag = 0; //使用完后，设置为零    

                    //清空选择行信息
                    strSelRowIDP0205 = "";
                    objSelRowP0205 = {};
                }
            }
        });
    } catch (e) { }
}

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

//数据行的双击事件
function dblClickRow_P0205() {
    showDialog_P020501("2");
}

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



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

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

    //属性的设置
    $("#cmbDateExtentP0205").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        panelHeight: 102,
        valueField: "dm",
        textField: "mc",
        editable: false,
        //选择事件
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0205", "dateEndTimeP0205");
        },
        //回车事件
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0205", "dateEndTimeP0205");
        }
    });

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

//客户下拉框信息的加载
function loadClient_P0205() {
    //属性的设置
    $("#cmbClientP0205").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        //内容变更前事件
        afterChange: function (newValue, newText) {
            $("#cmbClientP0205").combobox("setText", newText.replace(/&nbsp;/g, ""));
        },
        //获得焦点事件
        onfocus: function () {
            if ($("#cmbClientP0205").combobox("getText") == "全部客户") {
                $("#cmbClientP0205").combobox("setValue", undefined);
                $("#cmbClientP0205").combobox("setText", "");
            }
        },
        //失去焦点事件
        onblur: function () {
            if ($("#cmbClientP0205").combobox("getText") == "") {
                $("#cmbClientP0205").combobox("setValue", "");
            }
        }
    });

    //加载数据
    var objClient = getComboboxData("Tkhxx2");
    objClient.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbClientP0205").combobox("loadData", objClient);

    //释放内存
    objClient = null;
}

//药品下拉框信息的加载
function loadDrugData_P0205() {
    //属性的设置
    $("#cmbDrugP0205").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        //获得焦点事件
        onfocus: function () {
            if ($("#cmbDrugP0205").combobox("getText") == "全部药品") {
                $("#cmbDrugP0205").combobox("setValue", undefined);
                $("#cmbDrugP0205").combobox("setText", "");
            }
        },
        //失去焦点事件
        onblur: function () {
            if ($("#cmbDrugP0205").combobox("getText") == "") {
                $("#cmbDrugP0205").combobox("setValue", "");
            }
        }
    });

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

    //释放内存
    objDrug = null;
}

//加载表格数据
function loadGridData_P0205() {
    var objDataSet = {}; //数据集

    //客户
    if ($("#cmbClientP0205").combobox("getValue") == undefined) {
        $("#cmbClientP0205").combobox("setValue", "");
    }
    //药品
    if ($("#cmbDrugP0205").combobox("getValue") == undefined) {
        $("#cmbDrugP0205").combobox("setValue", "");
    }

    //查询条件
    objParamP0205.where = getWhereJson_P0205();
    //加载数据
    objDataSet = loadGridData_Page(objParamP0205);
    //合计数据的计算
    if (objDataSet && objDataSet.Records.length > 0) {
        $("#" + strTableP0205).footerData("set", { rn: "合计", sl: objDataSet.sl, jine: objDataSet.jine }, true);
    }
    else {
        $("#" + strTableP0205).footerData("set", { rn: "合计", sl: "&nbsp;", jine: "&nbsp;" }, false);
    }

    //释放内存
    objDataSet = null;
}

//表格事件的添加
function setGridEvent_P0205() {
    $("#" + strTableP0205).setGridParam({
        //选择行事件
        onSelectRow: function (rowid) {
            //页面全局变量
            strSelRowIDP0205 = rowid;
            objSelRowP0205 = $(this).getRowData(rowid);

            //浮动按钮内容
            createFloatButton.call(this, rowid, "<a onclick=\"showDialog_P020501('2')\">修改</a>-<a onclick=\"deleteData_P0205()\">删除</a>");
        },
        //排序事件
        onSortCol: function (index, colindex, sortorder) {
            $("div.operate").hide();            //隐藏浮动按钮
            objParamP0205.sortfield = index;
            objParamP0205.orderby = sortorder;
            loadGridData_Page(objParamP0205);
            return "stop";
        }
    });
}

//添加列格式化方法
function setColFormat_P0205() {
    //数量
    $("#" + strTableP0205).setColProp("sl", {
        formatter: function (cellvalue, options, rowObject) {
            if (cellvalue != "") {
                return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
            }
            else {
                return "0";
            }
        }
    });
    //金额
    $("#" + strTableP0205).setColProp("jine", {
        formatter: function (cellvalue, options, rowObject) {
            if (cellvalue != "") {
                return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
            }
            else {
                return "0";
            }
        }
    });
}

//查询条件json字符串的取得
function getWhereJson_P0205() {
    var strWhereJson = "";
    strWhereJson += "\"sdate\":\"" + $("#dateStartTimeP0205").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0205").val() + "\",";
    strWhereJson += "\"khdm\":\"" + $("#cmbClientP0205").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbDrugP0205").combobox("getValue") + "\"";
    return strWhereJson;
}

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