﻿//PagingState =
//{
//    Default: ""
//}
var currentPaging;
PagingController = function(config)
{    
    //Default config
    this.config = {
        id: '',
        pageId: '',
        contentId: '',
        pageSize: '',//Number of items par page
        numberPage: '',//Number of Page display
        urlGetCountRequest: '',//Url to get count
        urlGetData: '',//Url get data for paging
        params: {},
        emptyText: " Không có bản ghi nào.",//Empty text when has no items
        tpl: '',
        beforeUpdate: null,
        afterUpdate: null
    };
    Object.extend(this.config, config || {});
    this.id = this.config.id;
    this.currentPageIndex = 0;
    this.pageHolder = document.getElementById(this.config.pageId);
    this.contentHolder = document.getElementById(this.config.contentId);
    this.params = this.config.params ? this.config.params : {};
    this.pageSize = this.config.pageSize;
    this.numberPage = this.config.numberPage;
    this.urlGetData = this.config.urlGetData;
    this.urlGetCountRequest = this.config.urlGetCountRequest;

    var host = this.config.contentId;
    var ns = this.config.id;
    var tns = this.config.id;
    if (typeof host == "string")
    {
        host = document.getElementById(host);
    }
    inherit(new AbstractController(host, ns, tns), this);
    this.provider = new PagingProvider(this.urlGetCountRequest, this.urlGetData);
    inherit(new AbstractProvider(), this.provider);
    
    this.navigate(1);
    
//    System.Environment.HistoryKeeper.register(ns, this.state, "onStateChange", this);

//    var currentState = System.Environment.HistoryKeeper.getBookmarkedState(ns);
//	currentState = currentState || (PagingState.Default + "1");    
//    this.onStateChange(currentState,true);

    System.Environment.ObjectManager.add(this);
}
//PagingController.prototype.onStateChange = function(state, mustReload)
//{
//    if(state == this.state)
//    {
//        if(!mustReload)
//            return;
//    }
//    if(state > 0)
//    {
//        this.renderLoading("xxx");
//        this.setState(state);
//        this.navigate(state);
//    }       
//    return;
//}

PagingController.prototype.navigate = function(index)
{    
    //this.setState(PagingState.Default + index);
    //function for generate content
    var fnc = function(_this, context)
    {  
     
        var response = context.responseText;
        if(response != null && response != "null")
        {
            var list = JSON.decode(response);
            if(_this.config.beforeUpdate !=null && typeof _this.config.beforeUpdate == 'function')
            {
                _this.config.beforeUpdate(list);
            }
            _this.renderViewTemplate(_this.config.tpl, list, _this.ns);
            if(_this.config.afterUpdate !=null && typeof _this.config.afterUpdate == 'function')
            {
                _this.config.afterUpdate();
            }
        }
    }    
    var start = (index - 1) * this.pageSize;
    var end = index * this.pageSize;
    var params = {};
    params.start = start;
    params.end = end;
    for(var x in this.params)
    {
        params[x] = this.params[x];
    }
    var context = {};
    context.scope = this;

    if(start < 0)
    {
        var list = [];
        this.contentHolder.innerHTML = this.config.emptyText;
        this.pageHolder.innerHTML = "";
    }
    else
        this.provider.getData(params, fnc, this);

    this.provider.getCountItems(params, function(_this, context){
        _this.count = context.responseText;
        _this.renderToPageHolder(index);
    }, this);
}

PagingController.prototype.changeParams = function(object)
{
    this.params = object;    
}
PagingController.prototype.setUrlGetData = function(url)
{
    this.urlGetData = url;
}
PagingController.setUrlGetCountRequest = function(url)
{
    this.urlGetCountRequest = url;
}
PagingController.prototype.renderToPageHolder = function(index)
{
    var html = '<p class="pagination">Trang: ';
    var xindex = Math.ceil(index/this.numberPage);
    var xmaxindex = Math.ceil(this.count/this.pageSize);
    var start = (xindex - 1) * this.numberPage + 1;
    var end = xindex * this.numberPage;
    if(xmaxindex > 0)
    {
        showControl(this.pageHolder); 
        if(xindex > 1)
        {
            html += ' <a href="javascript: setNavigate(1,\'' + this.config.id + '\')">&lt;&lt;\u0110\u1EA7u</a>';
            html += ' <a href="javascript: setNavigate(' + (eval(start) - 1) + ',\'' + this.config.id + '\')">&lt;Tr\u01B0\u1EDBc</a>';
        }
        for(var i = start; i <= end && i <= xmaxindex; i ++)
        {
            if(i == index)
                {
                  html += ' <a href="javascript: setNavigate(' + i + ',\'' + this.config.id + '\')" id = "pageindex_' + i + '" class = "in">' + i +'</a>';
                 currentPaging = index;
                }
                
            else
                html += ' <a href="javascript: setNavigate(' + i+ ',\'' + this.config.id + '\')" id = "pageindex_' + i + '">' + i +'</a>';
        }
        if(end < xmaxindex)
        {
            html += ' <a href="javascript: setNavigate(' + (eval(end) + 1) + ',\'' + this.config.id + '\')">Sau&gt;</a>';
            html += ' <a href="javascript: setNavigate(' + xmaxindex + ',\'' + this.config.id + '\')">Cu\u1ED1i&gt;&gt;</a>';
        }
        html += '</p>';
    }else{
        hideControl(this.pageHolder);   
    }
    this.pageHolder.innerHTML = html;
}
PagingProvider = function(urlGetCountRequest, urlGetData)
{
    this.urlGetCountRequest = urlGetCountRequest;
    this.urlGetData = urlGetData;
}
PagingProvider.prototype.getCountItems = function(params, callBack, scope)
{
    var context = {};
    context.callBack = callBack;
    context.scope = scope;
    System.Environment.RequestManager.sendRequest(this.urlGetCountRequest, params, this.onSuccess, this.onFailure, context);
}
PagingProvider.prototype.getData = function(params, callBack, scope)
{
    var context = {};
    context.callBack = callBack;
    context.scope = scope;
    System.Environment.RequestManager.sendRequest(this.urlGetData, params, this.onSuccess, this.onFailure, context);
}
function setNavigate(pageIndex, pagingId,isDeleted)
{
    var pagingObj = System.Environment.ObjectManager.getObj(pagingId);
    if(isDeleted == true){
         pagingObj.provider.getCountItems(pagingObj.params, 
                function(_this, context){
                        pagingObj.count = context.responseText;
                        var xmaxindex = Math.ceil(pagingObj.count/pagingObj.pageSize);        
                        pageIndex = (pageIndex > xmaxindex )? pageIndex - 1: pageIndex;
                        pagingObj.navigate(pageIndex);
                }, pagingObj);
    }
    else
        pagingObj.navigate(pageIndex);
    //pagingObj.onStateChange(PagingState.Default + pageIndex, false);
}