/*
* author:MaoFangyun
* createTime:2015-01-15
* description:封装页面数据表的操作
* notice:需引用jquery,jsHelper_Form.js,关联的条件采用name=fCondition的form进行封装
*/

/*
*   数据表
*   config配置列表
*   @url    请求的地址
*   @id     id
*   @searchFlag 请求的标识[页面可能多个数据表,用此参数区分请求]
*   @pager  分页控件
*   @bindFun   绑定数据
*   @isAfter   是否启用(1:启用/0:不启用)
*   @operIndex 操作按钮的索引
*/
function Grid(config) {
    if (!config) {
        alert("请配置grid相关参数");
        return;
    }
    id = "#" + config.id;
    this.control = $(id);
    this.grid = $(id).find("tbody");
    this.url = config.url;
    this.searchFlag = config.searchFlag || 0;
    this.pager = config.pager || null;
    this.bindFun = config.bindFun || null;
    this.isAfter =  1;
    this.operIndex = config.operIndex || 0;
     if(config.isAfter!=undefined){
        this.isAfter =  config.isAfter;
    }
    this.init();
}

Grid.prototype = {
    init: function() {
        var self = this;
        if (this.pager) {
            this.pager.search = function() {
                self.search();
            };
        }
    },
    search: function(flag) {//flag为true时初始化分页控件的pageIndex
        var self = this;
        this.grid.empty();
        if (flag && this.pager) {
            this.pager.pageIndex = 1;
        }
        $.getJSON(this.url, this.getCondition(), function(data) {
            if (data.total > 0) {
                if (self.pager) {
                    self.pager.recordCount = data.total;
                }
                self.bindData(data.rows);

            } else {
                if (self.pager) {
                    self.pager.recordCount = 0;
                }
            }
            if (self.pager) {
                self.pager.setPager();
            }
        });
    },
    getCondition: function() {
        var parms = { action: "query", searchFlag: this.searchFlag };
        if (this.pager) {
            parms = { action: "query", searchFlag: this.searchFlag, pageSize: this.pager.pageSize, pageIndex: this.pager.pageIndex };
        }
        if ($("[name='fCondition']").length > 0) {
            return $.parseJSON(form2JsonString(fCondition, parms));
        } else {
            return parms;
        }
    },
    bindData: function(data) {
        var container = $("<div>");  
        for (var i in data) {
            var tr = $("<tr>").data("data", data[i]);
            if (this.bindFun) {
                this.bindFun(tr, data[i]);
            }
            else {
                for (var j in data[i]) {
                    tr.append($("<td>").html(data[i][j]));
                }
            }
            container.append(tr);
        }
//        var temp=$("<div>");
//        temp.append(container.children());
//        alert(temp.html());
        this.grid.append(container.children());
        if (this.isAfter == 1) {//启用绑定
            this.bindAfter(this.operIndex);
        }
    },
    bindAfter: function(operIndex) { //操作按钮索引
        if (this.grid.find("tr td:eq(" + operIndex + ")").children().length == 0) {
            this.control.find("tr").find("td:eq(" + operIndex + ")").hide(); //表身
            this.control.find("tr th:eq(" + operIndex + ")").hide(); //表头
        } else {
            var width = 0;
            this.grid.find("tr td:eq(" + operIndex + ")").children().each(function(e) {
                width += $(this).outerWidth() + 5; //2:margin-left
            });
            this.control.find("tr th:eq(" + operIndex + ")").css("width", width); //表头
            this.control.find("tr").find("td:eq(" + operIndex + ")").css("text-align", "center"); //表身
        }
    }
};