import Pagination from '../pagination';
import { tpl } from './longPaginationTpl';
import { debug } from 'util';
import Message from '../message';
const LongPagination = (($) => {
    const DefaultConfig = {
        tpl: tpl,
        current: 1,
        total: 18,
        middleDisplay: 5,
        totalItem: 2345,
        pageChange: function(args) {
            let defaultArgs = {
                current: parseInt(this.getCurrentPage())
            }
            let param = {...this._config, ...defaultArgs, ...args };
            console.log('页码变成' + param.current + '了,每页' + param.eachPageNum + '条');
            this.update(param);
        }
    }
    class LongPagination extends Pagination {
        constructor(config) {
            super(config);
            this._initChild(this._config);
        }
        _getConfig(config) {
            let result = $.extend(true, {},DefaultConfig, config);
            result = $.extend(true, { pageArr: this._getPageArr(result) }, result);
            return result;
        }
        _initChild(param) {
            let _this = this;
            let node = this._$el.find('.J_childSelect')[0];
        }
        _addEvent() {
            let _this = this;
            this._$el.on('click.page', '.J_prevPage', (e) => {
                this.prePage();
            })
            this._$el.on('click.page', '.J_nextPage', (e) => {
                this.nextPage();
            })
            this._$el.on('click.page', '.J_firstPage', (e) => {
                this.goFirstPage();
            })
            this._$el.on('click.page', '.J_lastPage', (e) => {
                this.goLastPage();
            })
            this._$el.on('click.page', '.J_search', (e) => {
                let num = this._$el.find('.J_pageNum').val();
                if (/^[0-9]*$/.test(num)) {
                    let val = parseInt(num);
                    val = val ? val : 1;
                    this.goPage(val);
                } else {
                    Message.error('请输入正确的页数');
                }
            })
            this._$el.on('click.page', '.J_page', function() {
                let page = parseInt($(this).html());
                _this.goPage(page);
            })
        }
        
        update(param) {
            param = $.extend(true,{},DefaultConfig, param);
            param.pageArr = this._getPageArr(param);
            let html = this._renderDom(param);
            this._$el.html(html);
        }
        
        getCurrentPage() {
            return parseInt(this._$el.find('.J_currentPage').html());
        }
        
        prePage() {
            this.goPage(this.getCurrentPage() - 1);
        }
        
        nextPage() {
            this.goPage(this.getCurrentPage() + 1);
        }
        
        goPage(num) {
            let totalPage = this.getTotalPage(),
                currentPage = this.getCurrentPage();
            if (num < 1) {
                num = 1;
            } else if (num > totalPage) {
                num = totalPage;
            }
            if (num === currentPage) {
                return;
            }
            let args = {
                current: num
            }
            let param = {...this._config, ...args };
            this._config.pageChange.call(this, param);
        }
        
        getTotalPage() {
            return parseInt(this._$el.find('.J_totalPage').html());
        }
        
        _getPageArr(config) {
            let { total, current, middleDisplay } = config;
            let halfMiddle = Math.floor(middleDisplay / 2),
                arr = [];
            for (var i = 1; i < current; i++) {
                if (i === 1) {
                    arr.push(i);
                } else if (current - i <= halfMiddle) {
                    arr.push(i);
                } else {
                    arr.push('...')
                }
            }
            arr.push(current);
            for (var i = current + 1; i <= total; i++) {
                if (i === total) {
                    arr.push(i);
                } else if (i - current <= halfMiddle) {
                    arr.push(i);
                } else {
                    arr.push('...')
                }
            }
            var result = [];
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] !== '...') {
                    result.push(arr[i]);
                } else if (arr[i] === '...' && arr[i + 1] !== '...') {
                    result.push(arr[i]);
                }
            }
            return result;
        }
        
        dispose() {
            this._$el.off('click.page')
            this._$el.html('');
        }
    }
    return LongPagination;
})($);
export default LongPagination;