﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/ipsa-config-default.js"/>
/// <reference path="../core/ipsa-prototype.js"/>
/// <reference path="../core/ipsa.js"/>
/// <reference path="../core/ipsa-control-core.js"/>

/***************************************************************************
jQuery repeat
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.
2.
3.
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {
    ///#region 设置
    ///#endregion

    $.fn.repeat = function (options, template, datasource, totalrecord, fnpagechanged) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>
        var rpt = this, ops = rpt.data('ops'), isreload = hasValue(ops), id = rpt.attr('id');
        ops = $.extend(ops || {
            usepager: true
                , showtoppager: false
                , simplepager:true
                , ajax: null
                , fnBuilded: null
                , pagesize: iconfig.grid.pagesize
                , pageindex: 1
                , totalrecord: 0
        }, options);

        if (template)
            $.templates(id, template);

        if (isreload)
            rpt.html('');
        bindRepeater(isreload, id, rpt, datasource, totalrecord, ops, fnpagechanged);
        return rpt;
    };

    ///#region 公共设置方法
    function bindRepeater(isreload, id, rpt, datas, totalrecord, ops, fnpagechanged) {
        if (datas)
            buildRepeater(isreload, id, rpt, datas, totalrecord, ops, fnpagechanged);
        else {
            ictrcore.loading(rpt);
            ictrcore.GetDatas(ops.ajax.url, $sw.bulidSearchParam(ops.pageindex, ops.pagesize, ops.ajax.data, ops.ajax.sort),
                                        function (datas) {
                                            var rpt = $('#' + id);
                                            if (datas)
                                                buildRepeater(isreload, id, rpt, datas.rows, datas.total, ops, isreload ? null : function (pageindex, pagesize) { pageChanged(id, pageindex, pagesize); });
                                        });
        }
    }

    function buildRepeater(isreload, id, rpt, datas, totalrecord, ops, fnpagechanged) {
        if (totalrecord)
            ops.totalrecord = totalrecord;
        if (datas != null && datas.length > 0) {
            var items = $.tmpl(id, datas, { startIndex: (ops.pageindex - 1) * ops.pagesize }).appendTo(rpt);
            if (ops.fnBuilded)
                ops.fnBuilded(items, datas);
        }
        if (isreload)
            rpt.siblings('div.pager').pager({ pagesize: ops.pagesize, pageindex: ops.pageindex, totalrecord: ops.totalrecord });
        else
            buildPager(rpt, ops, fnpagechanged);

        rpt.data('ops', ops);
        ictrcore.loadingClose(rpt);
    }

    function buildPager(container, ops, fnpagechange) {
        /// <summary>生成Pager</summary>
        if (ops.usepager) {
            var pageops = { pagesize: ops.pagesize, pageindex: ops.pageindex, totalrecord: ops.totalrecord, issimple: ops.simplepager };
            container.after($(document.createElement('div')).pager(pageops, fnpagechange));
            if (ops.showtoppager)
                container.before($(document.createElement('div')).pager(pageops, fnpagechange));
        }
    }
    function pageChanged(id, pageindex, pagesize, datasource,ops,rpt) {
        /// <summary>翻页处理</summary>
        if(!rpt)
            rpt = $('#' + id);
        if(!ops)
            ops = rpt.data('ops');
        if (pagesize)
            ops.pagesize = pagesize;
        if(pageindex)
            ops.pageindex = pageindex;
        rpt.html('');
        bindRepeater(true, id, rpt, datasource, null, ops);
    }
    ///#endregion
    $.fn.repeatReload = function (options, datasource) {
        /// <summary>重新绑定grid</summary>
        /// <param name="datasource" type="Json">Json数据源</param>
        var rpt = this, id = rpt.attr('id'), ops;
        if (options) {
            ops = $.extend(rpt.data('ops'), options);
            if (options.ajax && options.ajax.url)
                ops.ajax = $.extend(ops.ajax, options.ajax);
        }
        else
            ops = rpt.data('ops');
        if (!options || !options.pageindex)
            ops.pageindex = 1;
        pageChanged(id, null, null, datasource,ops,rpt);
    }

    ///#region 公共方法
    ///#endregion
})(jQuery);
