/*-------------------------------文档信息-----------------------------------
///功能：工资单单据
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strTableP_gzd = "Tp050801"; //表格ID
var strDlgID_gzd = "";  //弹出层ID

//打开单据
function openP051801_gzd(dlgId, djbh) {
    $("#" + dlgId).empty();
    var tbId = strTableP_gzd;
    strDlgID_gzd = dlgId;
    var dlgId = strDlgID_gzd;
    var $dlgId = $("#" + dlgId);
    creatDiv_ShowDlgHaveTbl(dlgId, "工资表", true, false, tbId, "950");
    // 向后台请求配置信息
    var config = ParseData(window.external.DoOptions(tbId, "GetTableConfig", '{"djbh":"' + djbh + '"}'));
    // 根据头配置 生成表单元素 
    var header = createInputs(config.pageHeader);
    //根据脚信息   生成表单元素
    var footer = createInputs(config.pageFooter);
    //var strBelong = "<a href=\"#\"  onclick='toExcelMethod();return false;'>导出</a>";//'<a href="#" title="导出" onclick="toExcelMethod();">导出</a>'
    $("#" + dlgId + "_pop_box_top").append('<a href="#" title="导出" onclick="toExcelMethod();">导出</a>');
    //添加头
    $("#" + dlgId + "_divform").append(header);
    //添加grid占位符
    $("#" + dlgId + "_divform").append('<table id="' + tbId + '"></table>');    

    for (var i = 0; i < config.colModel.length; i++) {
        config.colModel[i].editable = false;
        if (config.colModel[i].dif != "0") {
            config.colModel[i].formatter = formatMoneyGzPlace;
        }
        if (config.colModel[i].index == "yfgz") {
            config.colModel[i].formatter = formatMoneyGzPlace;
        }
    }
    createDjGrid(tbId, config.colNames, config.colModel);
    //$("#gbox_" + tbId).css("height", "157");
    $("#" + tbId).setGridHeight(141);
    //$("table.ui-jqgrid-htable th:first", "#" + dlgId).text("序号");
    $("#" + tbId).setLabel("rn", "序号");
    //添加脚
    $("#" + dlgId + "_divform").append(footer);
    var zjCols = [], jsCols = [];          //将增加类的列和减少类的列存储到数组中
    var items = config.colModel;
    for (var i = 0; i < items.length; i++) {
        if (items[i].dif == 1) { zjCols.push(items[i].index); }
        if (items[i].dif == 2) { jsCols.push(items[i].index); }
    }

    $("#" + tbId).data("zjCols", zjCols).data("jsCols", jsCols);

    initInputsProp_gzd(tbId, dlgId);

    var mylist = [];
    $("#idMust", "#" + dlgId).text("NO:" + djbh);
    var record = ParseData(window.external.DoOptions(tbId, "GetList", '{"djbh":"' + djbh + '"}'));
    if (record.ReturnCode == 0) {
        $("#djrq", "#" + dlgId).val(record.djrq).attr("disabled", true);
        $("#gzmonth", "#" + dlgId).val(record.gzmonth);
        $("#czymc", "#" + dlgId).val(record.czymc);
        $("#bz", "#" + dlgId).val(escape(record.bz));
        if (record.Records instanceof Array) {
            mylist = record.Records;
        }
        $("#" + tbId).data("data", { czydm: record.czydm, czymc: record.czymc, djbh: djbh });
    } else {
        window.parent.cihoAlert(record.MSG, msgBox.dlgBtn.Ok);
    }
    loadGridData(tbId, mylist);
    sumGridTotal(tbId);
    //调整表格的自适应的高度
    resizeGridScroll(strTableP_gzd);
    $dlgId.dialog("option", "position", "center");
    $(".layoutpanel").css("z-index", "50000009");
}

//设置样式，属性
function initInputsProp_gzd(tbId, dlgId) {
    $("#bz", "#" + dlgId).parent().addClass("bz");
    $("#gzmonth", "#" + dlgId).attr("disabled", true);
    $("#djdate,#gzmonth,#czymc", "#" + dlgId).parent().addClass("inp_70");
    $("#czymc", "#" + dlgId).attr("disabled", true);
}

//格式化money
function formatMoneyGzPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))&&parseFloat(cellValue)!="0") {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//计算footer统计信息==============在grid中调用
function sumGridTotal(tbId) {
    var $tbId = $("#" + tbId);
    var ids = $tbId.getDataIDs();
    if (tbId == "Tp050801") {
        var zjCols = $tbId.data("zjCols");                          //增加类、所有列的列名
        var jsCols = $tbId.data("jsCols");                          //减少类、所有列的列名
        var total = { rn: "合计", yfgz: 0 };

        for (var i = 0; i < ids.length; i++) {
            var item = $tbId.getRowData(ids[i]);
            if (item.zydm) {                                                                         //只计算有职员的行
                for (var j = 0; j < zjCols.length; j++) {
                    if (total[zjCols[j]] && !isNaN(total[zjCols[j]] * 1)) {
                        total[zjCols[j]] = total[zjCols[j]] * 1 + item[zjCols[j]] * 1;
                    } else if (item[zjCols[j]] * 1) {
                        total[zjCols[j]] = item[zjCols[j]] * 1;
                    } else {
                        total[zjCols[j]] = "&nbsp;";
                    }
                }
                for (var k = 0; k < jsCols.length; k++) {
                    if (total[jsCols[k]] && !isNaN(total[jsCols[k]] * 1)) {
                        total[jsCols[k]] = total[jsCols[k]] * 1 + item[jsCols[k]] * 1;
                    } else if (item[jsCols[k]] * 1) {
                        total[jsCols[k]] = item[jsCols[k]] * 1;
                    } else {
                        total[jsCols[k]] = "&nbsp;";
                    }
                }
                total.yfgz = item.yfgz * 1 + total.yfgz * 1;
            }
        }
        $tbId.footerData("set", total, true);
    }
    $tbId = null;
    ids = null;
    zjCols = null;
    jsCols = null;
    total = {};
}

//导出
function toExcelMethod() {
    makeExportData(strTableP_gzd, "工资表");
    return false;
}