var bLoadComplete = false;
var searchParam = {};
var cellX, cellY;

var strTableP0508 = "Tp0508";
var strPageP0508 = "divPageP0508";

//工资表维护
var strTableP050801 = "Tp050801";
var strDivP050801 = "divP050801";

//工资构成设置
var strTableP050802 = "Tp050802";
var strDivP050802 = "divP050802";

//工资支付
var strTableP050803 = "Tp050803";
var strDivP050803 = "divP050803";

//==================管理界面================

function pageLoad_P0508() {
    try {
        initInputsP0508();
        var config = ParseData(window.external.DoOptions(strTableP0508, "GetTableConfig", "{}"));
        var cNames = config.colNames;
        var cModel = config.colModel;
        createGrid(strTableP0508, cNames, cModel, [], null, null, true, alterRowDataP0508);
        bindGridEventP0508();
        addColFormater(strTableP0508);
        createPage(strPageP0508, strTableP0508);
        resizeGrid(strTableP0508, true, true);
        searchDataClick_P0508(1);
    } catch (e) { }
}

//初始化input控件
function initInputsP0508() {
    $("#sjtype,#djtypebh").combobox();
    addArrowClass();

    //绑定日期控件
    inputAndBindData("sdate", false, "", dateChange);
    inputAndBindData("edate", false, "", dateChange);

    $("#sjtype").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 102,
        onSelect: function () {
            var newValue = $(this).combobox("getValue");
            dateExtentCalculation(newValue, "sdate", "edate");
        },
        enterDown: function () {
            var newValue = $(this).combobox("getValue");
            dateExtentCalculation(newValue, "sdate", "edate");
        }
    });
    var options = [{ dm: "本月", mc: "本月" }, { dm: "上一月", mc: "上一月" }, { dm: "本周", mc: "本周" }, { dm: "过去三个月", mc: "过去三个月" }, { dm: "本年", mc: "本年"}];
    $("#sjtype").combobox("loadData", options);

    $("#djtypebh").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 62,
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($(this).combobox("getText") == "全部业务类型") {
                $(this).combobox("setText", "");
            }
        }
    });

    var djtype = [{ "dm": "", "mc": "全部业务类型" }, { "dm": "1101", "mc": "工资" }, { "dm": "1102", "mc": "支付"}];
    $("#djtypebh").combobox("loadData", djtype);
    $("#djtypebh").combobox("setValue", "");

    //设置初始化
    $("#sjtype").combobox("setValue", "本月");
    dateExtentCalculation("本月", "sdate", "edate");

    bLoadComplete = true;
    $(".layoutpanel").css("z-index", "50000009");
    options = djtype = null;
}

//设置grid的双击事件
function alterRowDataP0508(altertype, rowId) {
    try {
        var alterItem = $("#" + strTableP0508).getRowData(rowId);
        if (/^GZZF/i.test(alterItem.djbh)) {
            openGZZF(alterItem.djbh, alterItem.djdate);
        } else if (/^GZ/i.test(alterItem.djbh)) {
            openGZB(alterItem.djbh);
        }
    } catch (e) { }
}

//删除管理界面的数据
function delRowDataP0508(rowId) {
    try {
        var delItem = $("#" + strTableP0508).getRowData(rowId);
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (strRet) {//回调函数
            if (msgBox.dlgRst.Yes == strRet) {
                var s = ParseData(window.external.DoOptions(strTableP0508, "Delete", '{"djbh":"' + delItem.djbh + '","djtypebh":"' + delItem.djtypebh + '"}'));
                if (s.ReturnCode == 0) {
                    searchDataClick_P0508(2);
                } else {
                    window.parent.cihoAlert(s.MSG, msgBox.dlgBtn.Ok);
                }
            }
        });
    } catch (e) { }
}

//查询按钮的方法
function searchDataClick_P0508(flag) {
    try {
        var sdate = $("#sdate").val();
        var edate = $("#edate").val();
        var djtypebh = $("#djtypebh").combobox("getValue");

        if (flag == 1) {
            searchParam.sortfield = "djdate";
            searchParam.orderby = "desc";
        }
        searchParam.flag = flag;
        searchParam.where = '"sdate":"' + sdate + '","edate":"' + edate + '","djtypebh":"' + djtypebh + '"';
        var s = loadGridData_Page(searchParam);
        searchParam.flag = 0;
        if (s.Records.length == 0) {
            s.hjje = "&nbsp;";
        }
        $("#" + strTableP0508).footerData("set", { rn: "合计", je: s.hjje }, true);
    } catch (e) { }
}

//设置grid的参数
function bindGridEventP0508() {
    $("#" + strTableP0508).setGridParam({
        onSelectRow: function (rowId) {
            try {
                selRowId = rowId;
                var selItem = $(this).getRowData(rowId);
                if (selItem.djbh) {
                    var strBtns = '<a  onclick="alterRowDataP0508(2,' + rowId + ');return false; ">修改</a>-<a onclick="delRowDataP0508(' + rowId + ');return false; ">删除</a>';
                    createFloatButton.call(this, rowId, strBtns);
                }
                selItem = null;
            } catch (e) { }
        },
        onSortCol: function (index, colindex, sortorder) {
            try {
                searchParam.sortfield = index;
                searchParam.orderby = sortorder;
                loadGridData_Page(searchParam);
                $("div.operate").hide();
                return "stop";
            } catch (e) { }
        }
    });
}

//关闭对话框
function closeMyDlg(dlgId, strType, tbId) {
    try {
        if (dlgId == "divAdd") {
            if ($("#" + dlgId).data("change")) {
                window.parent.cihoAlert(msg.other.haveChange, msgBox.dlgBtn.YesNoCancel, function (result) {
                    if (result == msgBox.dlgRst.Yes) {
                        saveGZGC(tbId, 1, dlgId);
                    } else if (result == msgBox.dlgRst.No) {
                        cihoDlgClose(dlgId);
                    }
                });
            } else {
                cihoDlgClose(dlgId);
            }
        } else if (tbId == "Tp050802") {
            var r = updateOrder();
            cihoDlgClose(dlgId);
        } else {
            var isChange = $("#" + dlgId).data("change");
            if (isChange) {
                window.parent.cihoAlert(msg.other.haveChange, msgBox.dlgBtn.YesNoCancel, function (strRet) {
                    if (msgBox.dlgRst.Yes == strRet) {
                        saveMyData(dlgId, strType, tbId);
                    }
                    else if (msgBox.dlgRst.No == strRet) {
                        cihoDlgClose(dlgId);
                    }
                });
            } else {
                cihoDlgClose(dlgId);
            }
        }
    } catch (e) { }
}

//日期范围修改方法
function dateChange() {
    if ($("#sjtype").combobox("getText") != "自定义") {
        $("#sjtype").combobox("setValue", "");
        $("#sjtype").combobox("setText", "自定义");
    }
}
//==================管理、单据公用方法==================

//为grid的列添加格式化函数
function addColFormater(tbId) {
    var $tbId = $("#" + tbId);
    var formatMoney = { formatter: formatMoneyPlace };
    switch (tbId) {
        case "Tp0508":
            $tbId.setColProp("je", formatMoney);
            break;
        case "Tp050801":
            var zjCols = $tbId.data("zjCols");
            var jsCols = $tbId.data("jsCols");
            for (var i = 0; i < zjCols.length; i++) {
                $tbId.setColProp(zjCols[i], formatMoney);
            }
            for (var j = 0; j < jsCols.length; j++) {
                $tbId.setColProp(jsCols[j], formatMoney);
            }
            $tbId.setColProp("yfgz", formatMoney);
            zjCols = jsCols = null;
            break;
        case "Tp050803":
            //格式化   应发合计、实发工资、余额
            $tbId.setColProp("yfhj", formatMoney);
            $tbId.setColProp("sfgz", formatMoney);
            $tbId.setColProp("ye", formatMoney);
            break;
    }
    $tbId = formatMoney = null;
}

//为操作按钮注册事件
function bindDelEvent(tbId) {
    $("#" + tbId).delegate("tr[id]:visible", "hover", function (e) {
        if (e.type == "mouseenter") {
            var btn = '<a class="griddelbtn" onclick="delDjRow(\'' + tbId + '\',' + this.id + ');return false; ">删除</a>';
            $("#" + tbId).setCell(this.id, "cz", btn);
        }
        if (e.type == "mouseleave") {
            $("#" + tbId).setCell(this.id, "cz", " ");
        }
    });
}


//删除窗口中的grid数据
function delDjRow(tbId, rowId) {
    try {
        $("#" + tbId).saveCell(cellX, cellY);
        var delItem = $("#" + tbId).getRowData(rowId);
        if (delItem.zydm || delItem.flag) {//如果有职员代码
            window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (strRet) {
                var $tb = $("#" + tbId);
                if (msgBox.dlgRst.Yes == strRet) {
                    if (tbId == "Tp050803") {
                        $tb.delRowData(rowId);
                        if (delItem.mxguid) {
                            var records = $tb.data("records");
                            records.push('{"mxguid":"' + delItem.mxguid + '","flag":"3"}');
                            $tb.data("records", records);
                            changeZt(tbId);                                                    //改变对话框的状态
                        }
                        sumGridTotal(tbId);
                    }
                }
            }
                );
        }
        else {
            $("#" + tbId).delRowData(rowId);
            sumGridTotal(tbId);
        }
        resizeGridScroll(tbId);
    } catch (e) { }
}

//===================单据公共方法===================

//添加空行
function addNewRow(tbId) {
    try {
        $tbId = $("#" + tbId);
        var nextNum = $tbId.data("nextNum") + 1;
        $tbId.addRowData(nextNum, {});
        $tbId.data("nextNum", nextNum);
        resizeGridScroll(tbId);
        $tbId = null;
    } catch (e) { }
}

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

//格式化金额
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue)) && (cellValue * 1)) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化grid中的combobox
function my_input(value, options) {
    return $('<select name="' + options.name + '" id="' + options.id + '"></select>').addClass("easyui-combobox");
}
function my_value(ele) {
    return ele.combobox('getText');
}

//===================公共方法========================
function refreshMyCombobox(strModels) {
    for (var i = 0; i < strModels.length; i++) {
        var data;
        //这里是当前页面有的combobox
        if (strModels[i] == "TCkxx") {
            data = getComboboxData(strModels[i] + "2");
            reLoadCombobox("ckdmP0303", data);
        }
        if (strModels[i] == "Tspxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部药品" });
            reLoadCombobox("ypdmP0303", data);
        }
    }
    strModels = null;
}


//改变对话框或数据行的状态
//如果只提供表格id，则只改变窗口的状态标志
function changeZt(tbId, rowId) {
    if (tbId == "Tp050803") {
        $("#" + strDivP050803).data("change", true);
    } else {
        $("#" + strDivP050803).data("change", true);
    }
    if (rowId) {
        var $tb = $("#" + tbId);
        var item = $tb.getRowData(rowId);
        if (item.mxguid) {
            $tb.setCell(rowId, "flag", 2);                               //2代表更新
        } else {
            $tb.setCell(rowId, "flag", 1);                               //1代表新增
        }
        $tb = item = null;
    }
}

//afterSave  1：保存退出，2：保存新增
function saveMyData(dlgId, afterSave, tbId) {
    //添加分类
    if (tbId == ciho.layal.table) {
        ciho.saveType(); return;
    }

    switch (tbId) {
        case "Tp050801":
            saveGZB();
            break;
        case "Tp050802":
            saveGZGC(tbId, afterSave, dlgId);
            break;
        case "Tp050803":
            afterSaveClick();
            break;
    }
}