/*-------------------------------文档信息-----------------------------------
///功能：工资明细
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP051801 = "Tp051801";   //表格ID
var objSelRowP051801 = {};          //选择行的数据
var intSelRowIDP051801 = 0;         //选择行的ID
var strPageP051801 = "divPageP051801";
var objPageParamP051801 = { sortfield: "", orderby: "", where: "", flag: 0 };  //分页控件使用
var strBmdm = "";
//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    //$("#" + strDialog).dialog("close");
    cihoDlgClose(strDialog);
}

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

//页面加载方法
function pageLoad_P051801() {
    //选择日期范围
    dateExtent_P051801();
    //部门下拉框
    loadBmData_P051801();
    //职员下拉框
    loadZyData_P051801();
    var objConfig = ParseData(window.external.DoOptions(strTableP051801, "GetTableConfig", "{}"));
    //添加列格式化方法
    for (var i = 0; i < objConfig.colModel.length; i++) {
        if (objConfig.colModel[i].name == "yfje" || objConfig.colModel[i].name == "sfje") {
            objConfig.colModel[i].formatter = formatterP051801Money;
        }
        if (objConfig.colModel[i].name == "yuje") {
            objConfig.colModel[i].formatter = formatterYujeMoney;
        }
    }
    //创建表格
    createGrid(strTableP051801, objConfig.colNames, objConfig.colModel, [], null, null, true);
    //表格事件的添加
    addGridEvent_P051801();
    resizeGrid(strTableP051801, true, true);
    //创建分页
    createPage(strPageP051801, strTableP051801);
    //加载表格数据(触发查询按钮的点击事件)
    searchDataClick_P051801();
    objConfig = {};
}

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

//加载数据
function loadGridDataP051801() {
    var objResult = [];
    objPageParamP051801.where = getSelJson_P051801();
    objResult = loadGridData_Page(objPageParamP051801);
    //后台返回的合计
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP051801).footerData("set", { rn: "合计",
                yfje: objResult.yfje.toFixed(window.parent.strMoneyPlace),
                sfje: objResult.sfje.toFixed(window.parent.strMoneyPlace)
            }, false);
        } else {
            $("#" + strTableP051801).footerData("set", { rn: "合计", yfje: "&nbsp;", sfje: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP051801 = "";
    objSelRowP051801 = {};
}

//取得查询条件
function getSelJson_P051801() {
    //combobox的key不存在，赋值空
    if ($("#cmbBmP051801").combobox("getValue") == undefined) {
        $("#cmbBmP051801").combobox("setValue", "");
        $("#cmbZyP051801").combobox("setValue", "");
    }
    if ($("#cmbZyP051801").combobox("getValue") == undefined) {
        $("#cmbZyP051801").combobox("setValue", "");
    }
    //查询条件的取得
    var strWhereJson = "";
    //快捷日期是历史以来，传空值
    if ($("#cmbDateExtentP051801").combobox("getValue") == "历史以来") {
        strWhereJson = "\"sdate\":\"0\",";
        strWhereJson += "\"edate\":\"0\",";
    }
    else {
        strWhereJson = "\"sdate\":\"" + $("#dateStartTimeP051801").val() + "\",";
        strWhereJson += "\"edate\":\"" + $("#dateEndTimeP051801").val() + "\",";
    }
    strWhereJson += "\"bmdm\":\"" + $("#cmbBmP051801").combobox("getValue") + "\",";
    strWhereJson += "\"zydm\":\"" + $("#cmbZyP051801").combobox("getValue") + "\"";
    return strWhereJson;
}

//查看单据按钮的方法
function viewReceipt_P051801() {
    try {
        if (intSelRowIDP051801 > 0) {
            if (objSelRowP051801.djname == "工资单") {
                openP051801_gzd("divP051801", objSelRowP051801.djbh);
            }
            else if (objSelRowP051801.djname == "工资支付") {
                openP051801_Zfd("divP051801", objSelRowP051801.djbh);
            }
            else if (objSelRowP051801.djname == "提成调整") {
                showDialog_P051801_tzd("divP051801", objSelRowP051801.djbh);
            }
        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) { }
}

//返回按钮的方法
function pageBack_P051801() {
    //改变标签的Name
    $(window.parent.document).find("#span_518").text("工资查询");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-518").css("display", ""); //显示统计页面
    $(window.parent.document).find("#idIframe-51801").css("display", "none"); //隐藏明细页面
    $(window.parent.document).find("#idIframe-51801").attr("src", "");
}

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

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//选择日期范围
function dateExtent_P051801() {
    $("#cmbDateExtentP051801").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 150,
        onChange: function (newValue,oldValue) {
            if (newValue == "历史以来") {          //快捷日期是历史以来，两个日期文本框灰化
                $("#dateStartTimeP051801").attr("disabled", true);
                $("#dateEndTimeP051801").attr("disabled", true);
            }
            else {                                  //其它的不灰化
                $("#dateStartTimeP051801").attr("disabled", false);
                $("#dateEndTimeP051801").attr("disabled", false);
            }
            //dateExtentCalculation(newValue, "dateStartTimeP051801", "dateEndTimeP051801");
        },
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP051801", "dateEndTimeP051801");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP051801", "dateEndTimeP051801");
        }
    });
    //加载数据
    $("#cmbDateExtentP051801").combobox("loadData", [{ dm: "本月", mc: "本月" },
        { dm: "上一月", mc: "上一月", selected: true },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "近半年", mc: "近半年" },
        { dm: "本年", mc: "本年" },
        { dm: "上一年", mc: "上一年" },
        { dm: "历史以来", mc: "历史以来"}]);

    var strDateItem = $(window.parent.document).find("#idIframe-51801").attr("dname");
    if (strDateItem != "") {
        $("#cmbDateExtentP051801").combobox("setValue", strDateItem);
        dateExtentCalculation(strDateItem, "dateStartTimeP051801", "dateEndTimeP051801");
    }
    else {
        var strStartTime = $(window.parent.document).find("#idIframe-51801").attr("sdate");
        var strEndTime = $(window.parent.document).find("#idIframe-51801").attr("edate");
        $("#cmbDateExtentP051801").combobox("setText", "自定义");
        $("#dateStartTimeP051801").val(strStartTime);
        $("#dateEndTimeP051801").val(strEndTime);
    }
    strDateItem = null;
    strStartTime = null;
    strEndTime = null;
}

//加载部门信息的方法
function loadBmData_P051801() {
    $("#cmbBmP051801").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbBmP051801").combobox("getText") == "") {
                $("#cmbBmP051801").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbBmP051801").combobox("getText") == "全部部门") {
                $("#cmbBmP051801").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P051801(); 
        },
        onSelect: function (options) {
        //afterChange: function (newValue, oldValue) {
            if (options.dm != strBmdm) {        //选择部门关联所属的职员
                if (options.dm == "") {
                    var objZydm = getComboboxData("TZyxx2");
                    $("#cmbZyP051801").combobox("setValue", "");
                    objZydm.unshift({ "dm": "", "mc": "全部职员", "selected": true });
                    $("#cmbZyP051801").combobox("loadData", objZydm);
                    objZydm = {};
                }
                else {
                    var objRet = [];
                    var objZydm = getComboboxData("TZyxx2");
                    objRet = cmbSelectData_P051801(options.dm, objZydm, 'bmdm');
                    objRet.unshift({ "dm": "", "mc": "全部职员", "selected": true });
                    $("#cmbZyP051801").combobox("loadData", objRet);
                    objZydm = {};
                    objRet = {};
                }
            }
            strBmdm = $("#cmbBmP051801").combobox("getValue");
        }
    });

    //加载数据
    var objBm = getComboboxData('Tbmxx2');
    if (objBm == "{}") {
        objBm = [];
    }
    objBm.unshift({ "dm": "", "mc": "全部部门", "selected": true });
    $("#cmbBmP051801").combobox("loadData", objBm);
    $("#cmbBmP051801").combobox("setValue", $(window.parent.document).find("#idIframe-51801").attr("bmdm"));
    objBm = {};
}

//加载职员信息的方法
function loadZyData_P051801() {
    $("#cmbZyP051801").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbZyP051801").combobox("getText") == "") {
                $("#cmbZyP051801").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbZyP051801").combobox("getText") == "全部职员") {
                $("#cmbZyP051801").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P051801(); 
        },
        //onSelect: function (options) {
        afterChange: function (newValue,oldValue) {
            if (newValue != "") {                 //选择职员，关联它的部门
                var objZyData = getComboboxData("TZyxx2");
                for (var intI = 0; intI < objZyData.length; intI++) {
                    if (oldValue == objZyData[intI].mc) {
                        if (objZyData[intI].bmdm != $("#cmbBmP051801").combobox("getValue")) {
                            $("#cmbBmP051801").combobox("setValue", objZyData[intI].bmdm);
                        }
                    }
                }
                objZyData = {};
                strBmdm = $("#cmbBmP051801").combobox("getValue");
            }
        }
    });

    //加载数据
    var objZy = getComboboxData('TZyxx2');
    if (objZy == "{}") {
        objZy = [];
    }
    objZy.unshift({ "dm": "", "mc": "全部职员", "selected": true });
    $("#cmbZyP051801").combobox("loadData", objZy);
    $("#cmbZyP051801").combobox("setValue", $(window.parent.document).find("#idIframe-51801").attr("zydm"));
    objZy = {};
}

//部门关联对应的职员
function cmbSelectData_P051801(options, objList, strdm) {
    var objRet = [];
    for (var i = 0; i < objList.length; i++) {
        if (options == objList[i][strdm]) {
            objRet.push({ "bmdm": objList[i].bmdm, "dm": objList[i].dm, "jc": objList[i].jc, "jp": objList[i].jp, "mc": objList[i].mc, "newbh": objList[i].newbh });
        }
    }
    return objRet;
}

//表格事件的添加
function addGridEvent_P051801() {
    $("#" + strTableP051801).setGridParam({        
        onSelectRow: function (rowId) { //当前选择行数据取得
            intSelRowIDP051801 = rowId;
            objSelRowP051801 = $(this).getRowData(rowId);
        },
        ondblClickRow: function (rowId) {   //双击事件
            viewReceipt_P051801();
        }
    });
}

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

//格式化余额的方法
function formatterYujeMoney(cellvalue, options, rowObject) {
    if (!isNaN(parseFloat(cellvalue))) {
        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//创建单据grid，公共
function createDjGrid(tbId, cNames, cModel) {
    $("#" + tbId).jqGrid({
        datatype: "local",
        //height: "100%",
        autowidth: true,
        colNames: cNames,
        colModel: cModel,
        rownumbers: true,
        footerrow: true,
        sortable: false,
        rownumWidth: 30,
        cellEdit: false,
        cellsubmit: "clientArray"
    });
}

//加载窗口grid数据，并计算合计
function loadGridData(tbId, mylist) {
    var count = mylist.length > 5 ? mylist.length : 5;
    var $tbId = $("#" + tbId);
    if (tbId == "Tp050801") {
        for (var i = 0; i < count; i++) {
            if (mylist[i]) { 
                $tbId.addRowData(i + 1, mylist[i]);                                   //添加后台数据行
            }
            else {                                                                                  //添加空行
                $tbId.addRowData(i + 1, {});
            }
        }
    } else if (tbId == "Tp050803") {
        var yfhj = 0, sfhj = 0, ye = 0;
        for (var i = 0; i < count; i++) {
            if (mylist[i]) {                                                                         //添加后台数据行
                yfhj += isNaN(mylist[i].yfhj * 1) ? 0 : mylist[i].yfhj * 1;
                sfhj += isNaN(mylist[i].sfgz * 1) ? 0 : mylist[i].sfgz * 1;
                ye += isNaN(mylist[i].ye * 1) ? 0 : mylist[i].ye * 1;
                $tbId.addRowData(i + 1, mylist[i]);                                   //添加后台数据行
            }
            else {                                                                                  //添加空行
                $tbId.addRowData(i + 1, {});
            }
        }
        $tbId.footerData("set", { rn: "合计", yfhj: yfhj, sfgz: sfhj, ye: ye }, true);
    }
    $tbId.data("nextNum", count + 1);
    count = null;
    $tbId = null;
    yfhj = sfhj = ye = null;
    count = null;  
}

//创建表单元素
function createInputs(arr) {
    var strForm = "<ul>";
    for (i = 0; i < arr.length; i++) {
        var Item = arr[i];
        if (!Item.hidden) {
            if (Item.edittype == "text") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' disabled ";
                strForm += " value=''  ";
                strForm += " id='" + Item.index + "' ";
                strForm += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>";
                strForm += "</li>";
            } else if (Item.edittype == "select") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<select name='" + Item.index + "' id='" + Item.index + "' disabled";
                strForm += " class='easyui-combobox' >";
                strForm += "</select>";
                strForm += "</li>";
            }
        }
    }
    strForm += "</ul>";
    return strForm;
}
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/