(function($) {
    'use strict';

    var con;
    var options;
    var curDatas;
    var element;
    var obj;
    /**
     * Datatable class
     * 
     * @param object element           DOM element or jquery element
     * @param object options           Datatable options
     */
    var mytable = function(element, options) {
        this.element = element;
        this.options = options;
        this.pager = options.pager;
        this.con = $(element);
        this.obj = this;
        $(element).html('');
        $(element).append('<div class="row"><div id="my_table" data-checkable="true" data-sortable="true"></div></div>'
                           + '<div id="kkpager" class="row" style="text-align:center"></div>');
        this.getRemoteData(element,options,this);
        return this;       
    };


    // default options
    mytable.DEFAULTS = {
        container:"",
        pages:'',
        curr:'',
        groups:'',
        first:1,
        last:1,
        prev:1,
        next:1,
        jump:null
    };

    // Get options
    mytable.prototype = {
        getRemoteData:function(element,options,obj){
            if(!element) element = this.element;
            if(!options) options = this.options;
            var url = options.url;
            var p_cur = 1;
            var pager = options.pager;
            this.getServerData(p_cur, options, element, obj);
        },
        getRemoteData1:function(p_cur){
            this.getServerData(p_cur, this.options, this.element, this.obj);
        },
        getServerData:function(p_cur, options, element, obj){
            var url = options.url;
            var pager = options.pager;
            var page_size = pager.page_size;
            var param = options.param;
            // var obj = this.obj;
            common_post_service(url+"/"+p_cur+"/"+page_size, param, function(data) {
                // $(element).html("");
                element.tt_data=data.list;
                options.tableData=data.list;
                obj.setTable(data.list,element, options,p_cur); 
                if(obj.options.pager){
                    options.pager = {
                        page_size:10,
                        p_cur : data.pageNum, //页号
                        p_pages:data.pages, //总页码
                        p_totalRecords:data.total
                        // 'cb':'getRemoteData
                    }   
                    obj.setPager();
                }
            }, function(error) {
                alert(error);
            });
        },
        setTable:function(datas, element, options,p_cur) {
            var cols = options.cols;
            var tableData = datas;
            // var pager = options.pager;
            clearTable(cols);
            var rowData = formatTableData(tableData, cols, p_cur);

            // 使用data参数更新数据：
            $('#my_table').datatable('load',{
                    cols: cols,
                    rows:rowData
                }
            );
            function formatTableData(data,cols,p_cur){
                var rows = [];
                for(var i=0;i<data.length;i++){
                    data[i].num=(p_cur-1)*10+i+1;
                    rows.push(genRow(data[i],cols));
                }
                return rows;
            }

            function genRow(obj,cols){
                var row = {checked: false, data:null};
                var dArr=[];
                for(var i=0;i<cols.length;i++){
                    if(cols[i].oper!=null) {
                        var oper = cols[i].oper;
                        var operStr="";
                        for(var j=0;j<oper.length;j++){
                            operStr+='<a href="#" onclick="'+oper[j].func+'('+obj[cols[i].field]+')"><i class="icon icon-edit"></i>'+oper[j].text+'</a> ';
                        }
                        dArr.push(operStr);
                    }else{
                        dArr.push(obj[cols[i].field]);
                    }
                }
                row.data=dArr;
                return row;
            }
            function clearTable(cols){
                $('#my_table').datatable( {
                    checkable:false,
                    sortable:false,
                    data: {
                        cols: cols,
                        rows:[]
                    }
                });
            }
        },
        setPager:function(data) {
            var pager = this.options.pager;
            var p_cur = pager.p_cur;
            var p_pages = pager.p_pages;
            var p_totalRecords = pager.p_totalRecords;
            var obj=this;
            kkpager.generPageHtml({
                pno : p_cur,
                total : p_pages,
                totalRecords : p_totalRecords,
                mode : 'click',
                click : function(n){
                    obj.options.pager.p_cur = n;
                    this.selectPage(n);
                    obj.getRemoteData1(n);//调用获取数据方法
                    return false;
                }
            },true);
        }
    };

    $.fn.mytable = function(options) {
        if (typeof options == 'string') {
            switch (options) {
                case 'getTableData': 
                    return this[0].tt_data; 
            }
         }   else{
        return this.each(function() {
            // var $this = $(this);
                new mytable(this,options);
            //  $(this).attr('tableData',mytable.tableData);
            });
        }
    };


    $.fn.mytable.Constructor = mytable;
}(jQuery));

