/*-------------------------------文档信息-----------------------------------
///功能：其他收入管理
///创建人: 翟新乐
///创建时间：2011-07-01
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0503 = "Tp0503";    //表格ID
var strPageP0503 = "divPageP0503"; // 信息编辑窗口ID
var objSelRowP0503 = {};        //选择行的数据
var intSelRowIDP0503 = 0;       //选择行的ID
var objPageParamP0503 = { sortfield: "djdate", orderby: "desc", where: "", flag: 0 };  //分页控件使用

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

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp050301") {
            //保存
            saveMyData_P050301(strType);
        } //添加分类
        else if (strTable == ciho.layal.table) {
            ciho.saveType();
        }
    } catch (e) { }
}

//修改
function updateP0503() {
    if (objSelRowP0503.djbh != null) {
        showDialog_P050301("2");
    }
    else {
        window.parent.cihoAlert(msg.edit.selEditRow);
    }
}

//删除
function deleteData_P0503() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                var strJsonData = "{\"djbh\":\"" + objSelRowP0503.djbh + "\"}";
                var objResult = ParseData(window.external.DoOptions(strTableP0503, 'Delete', strJsonData));
                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    objPageParamP0503.flag = 2;
                    loadGridDataP0503();
                    objPageParamP0503.flag = 0;
                    //resizeGrid(strTableP0503);
                }
                else {
                    window.parent.cihoAlert(msg.del.selDelRow);
                }
                strJsonData = null;
                objResult = {};
            }
        });
    } catch (e) { }
}

//关闭
function closeMyDlg(strDialog, strType, strTableName) {
    if ($("#" + strDialog + "_divform").data("change")) {
        window.parent.cihoAlert(
                msg.other.haveChange,
                 msgBox.dlgBtn.YesNoCancel,
                 function (strRet) {
                     if (msgBox.dlgRst.Yes == strRet) {
                         saveMyData(strDialog, strType, strTableName);
                     }
                     else if (msgBox.dlgRst.No == strRet) {
                         cihoDlgClose(strDialog);
                         if (strDialog != "divAddType") {
                             freeMomeyP050301();
                         }
                     }
                 });
    }
    else {//0：关闭。关闭编辑窗口
        cihoDlgClose(strDialog);
        if (strDialog != "divAddType") {
            freeMomeyP050301();
        }
    }
}

/*------------------------以下是页面私有事件方法-----------------------------*/

//页面加载方法
function pageLoad_P0503() {
    try {
        //选择日期范围
        dateExtent_P0503();
        //单位下拉框信息的加载
        loadBmdwData_P0503();
        //药品下拉框信息的加载
        loadSpData_P0503();
        //职员下拉框信息的加载
        loadZyData_P0503();
        //科目下拉框信息的加载
        loadKmxx_P0503();
        //列表数据的取得
        var objConfig = ParseData(window.external.DoOptions(strTableP0503, "GetTableConfig", "{}")); //获得jqGrid的列名和属性
        addColFormatter_P0503(objConfig.colModel);
        createGrid(strTableP0503, objConfig.colNames, objConfig.colModel, [], null, null, true);
        //添加事件
        addGridEvent_P0503();

        //设置表格的高度
        resizeGrid(strTableP0503, true, true);
        //创建分页
        createPage(strPageP0503, strTableP0503);
        searchData_P0503();
        //resizeGrid(strTableP0503);
        objConfig = {};
    } catch (e) { }
}

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

//加载数据的事件
function loadGridDataP0503(strFlag) {
    var objResult = [];
    if ($("#cmbDwP0503").combobox('getValue') == undefined) {
        $("#cmbDwP0503").combobox('setValue', "");
    }
    if ($("#cmbSpP0503").combobox('getValue') == undefined) {
        $("#cmbSpP0503").combobox('setValue', "");
    }
    if ($("#cmbZyP0503").combobox('getValue') == undefined) {
        $("#cmbZyP0503").combobox('setValue', "");
    } 
    if (!strFlag) {
        objPageParamP0503.where = getSelJson_P0503();
    }
    objResult = loadGridData_Page(objPageParamP0503);
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP0503).footerData("set", { rn: "合计", a5: objResult.hjje }, true);
        }
        else {
            $("#" + strTableP0503).footerData("set", { rn: "合计", a5: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP0503 = "";
    objSelRowP0503 = {};
}

//表格事件的添加
function addGridEvent_P0503() {
    $("#" + strTableP0503).setGridParam({
        onSelectRow: function (rowId) {     //选择行使用方法
            //当前选择行数据
            intSelRowIDP0503 = rowId;
            objSelRowP0503 = $(this).getRowData(rowId);
            var strBtnHtml = "";
            if (objSelRowP0503.djbh != null) {
                //浮动按钮内容
                strBtnHtml = "<a onclick=\"updateP0503()\">修改</a>-<a onclick=\"deleteData_P0503()\">删除</a>";
            }
            createFloatButton.call(this, rowId, strBtnHtml);
        },
        ondblClickRow: function (rowId) {
            updateP0503();
        },
        onSortCol: function (index, colindex, sortorder) {  //排序
            $("div.operate").hide();                    //浮动层隐藏
            objPageParamP0503.sortfield = index;        //排序字段
            objPageParamP0503.orderby = sortorder;      //排序顺序
            loadGridDataP0503(true);                        //加载数据
            return "stop";
        }
    });
}

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

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

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

//单位下拉框信息的加载
function loadBmdwData_P0503() {
    $("#cmbDwP0503").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",       
        onfocus: function () {  //获得焦点
            if ($('#cmbDwP0503').combobox('getText') == "全部单位") {
                $("#cmbDwP0503").combobox("setText", "");
            }
        },        
        onblur: function () {   //失去焦点
            if ($("#cmbDwP0503").combobox("getText") == "") {
                $("#cmbDwP0503").combobox("setValue", "");
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        },
        enterDown: function () {
            //searchData_P0503();
        }
    });
    var objKp = getComboboxData('Tkhxx2');
    if (objKp == "{}") {
        objKp = [];
    }
    objKp.unshift({ "dm": "", "mc": "全部单位", "selected": true });
    $("#cmbDwP0503").combobox("loadData", objKp);
    objKp = {};
}

//科目combobox信息
function loadKmxx_P0503() {
    var item = $("#cmbKmP0503");
    var objKjkm = ParseData(window.external.DoOptions(strTableP0503, "GetComboBoxData", "{}")).Records;
    if (objKjkm == "{}") {
        objKjkm = [];
    }
    objKjkm.unshift({ "id": "", "text": "全部科目"});
    item.cihoComboTree("loadData", objKjkm);
    item = null;
    objKjkm = {};
}

//药品combobox信息
function loadSpData_P0503() {
    $("#cmbSpP0503").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",       
        onfocus: function () {  //获得焦点
            if ($('#cmbSpP0503').combobox('getText') == "全部药品") {
                $("#cmbSpP0503").combobox("setText", "");
            }
        },        
        onblur: function () {   //失去焦点
            if ($("#cmbSpP0503").combobox("getText") == "") {
                $("#cmbSpP0503").combobox("setValue", "");
            }
        },
        enterDown: function () {
            //searchData_P0503();
        }
    });
    var objSp = getComboboxData('Tspxx2');
    if (objSp == "{}") {
        objSp = [];
    }
    objSp.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbSpP0503").combobox("loadData", objSp);
    objSp = {};
}

 //职员combobox数据
 function loadZyData_P0503() {
     $("#cmbZyP0503").combobox({
         width: _iWidth100,
         listWidth: _iWidth100,
         valueField: "dm",
         textField: "mc",         
         onfocus: function () { //获得焦点
             if ($('#cmbZyP0503').combobox('getText') == "全部职员") {
                 $("#cmbZyP0503").combobox("setText", "");
             }
         },         
         onblur: function () {  //失去焦点
             if ($("#cmbZyP0503").combobox("getText") == "") {
                 $("#cmbZyP0503").combobox("setValue", "");
             }
         },
         enterDown: function () {
             //searchData_P0503();
         }
     });
     var objZy = getComboboxData('TZyxx2');
     if (objZy == "{}") {
         objZy = [];
     }
     objZy.unshift({ "dm": "", "mc": "全部职员", "selected": true });
     $("#cmbZyP0503").combobox("loadData", objZy);
     objZy = {};
 }

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

//得到查询字符串
function getSelJson_P0503() {
    var strRecord = "";
    strRecord += "\"sdate\":\"" + $("#dateStartTimeP0503").val() + "\",";
    strRecord += "\"edate\":\"" + $("#dateEndTimeP0503").val() + "\",";
    if ($("#cmbKmP0503").cihoComboTree('getText') == "其他收入") {
        strRecord += "\"kmdm\":\"\",";
    } else {
        strRecord += "\"kmdm\":\"" + $("#cmbKmP0503").cihoComboTree('getValue') + "\",";
    }
    strRecord += "\"khdm\":\"" + $("#cmbDwP0503").combobox('getValue') + "\",";
    strRecord += "\"spdm\":\"" + $("#cmbSpP0503").combobox('getValue') + "\",";
    strRecord += "\"zydm\":\"" + $("#cmbZyP0503").combobox('getValue') + "\"";
    return strRecord;
}
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/