import Pagination from '../pagination';
import { tpl } from './shortPaginationTpl'
const ShortPagination = (($) => {
    const DefaultConfig = {
        tpl: tpl,
        current: 1,
        total: 18,
        pageChange: function(param) {
            console.log('页码变成' + param.current + '了');
            setTimeout(()=>{
                this.update(param)
            },0);
            
        }
    }
    
    class ShortPagination extends Pagination {
        constructor(config) {
            super(config);
        }
        _getConfig(config) {
            return $.extend(true,DefaultConfig, config);
        }
        _addEvent() {
            let _this = this;
            this._$el.on('click.page', '.J_pre', (e) => {
                this.prePage();
            })
            this._$el.on('click.page', '.J_next', (e) => {
                this.nextPage();
            })
            this._$el.on('change.page', '.J_currentPage', function(e) {
                let num = parseInt($(this).val()),
                    totalPage = _this.getTotalPage();
                if (isNaN(num)) {
                    num = 1;
                } else if (num < 1) {
                    num = 1;
                } else if (num > totalPage) {
                    num = totalPage;
                }
                _this.goPage(num);
                $(this).val(num);
            })
        }
        
        update(param) {
            console.log(param);
            let html = this._renderDom(param);
            this._$el.html(html);
        }
        
        getCurrentPage() {
            return parseInt(this._$el.find('.J_currentPage').val());
        }
        
        prePage() {
            this.goPage(this.getCurrentPage() - 1);
        }
        
        nextPage() {
            this.goPage(this.getCurrentPage() + 1);
        }
        
        goPage(num) {
            let totalPage = this.getTotalPage();
            if (num < 1) {
                num = 1;
            } else if (num > totalPage) {
                num = totalPage;
            }
            let args = {
                current: num
            }
            let param = {...this._config, ...args };
            this._config.pageChange.call(this, param);
        }
        
        getTotalPage() {
            return parseInt(this._$el.find('.J_totalPage').html());
        }
        
        dispose(){
            this._$el.off('click.page');
            this._$el.off('change.page');
            this._$el.html('');
            
        }
    }
    return ShortPagination;
})($);
export default ShortPagination;