import React from 'react'
// 用于解决数据过多，导致取不到数据的问题
export function dealTotal(currentPage, pageSize, total) {
    // 先缓存一下total，this.realTotal里保存真实的总数
    this.realTotal = total;
    // const result = (total - (total % (5 * pageSize))) / (5 * pageSize);

    // 计算currentPage在第几个区间里
    const i = parseInt(currentPage / 5, 10);
    // console.log(i);
    if (this.realTotal > 5 * pageSize) {
        // 确定上限
        if (currentPage * pageSize >= this.realTotal) {
            this.total = this.realTotal;
        } else if (currentPage < 5 * (i + 1) && currentPage >= 5 * (i)) {
            // 取较小的值，防止出bug，多出来的页
            this.total = Math.min(5 * (i + 1) * pageSize, this.realTotal);
        }
        // if (currentPage < 5) {
        //     this.total = 5 * pageSize;
        // } else if (currentPage < 10 && currentPage >= 5) {
        //     this.total = 2 * 5 * pageSize;
        // }
    } else {
        this.total = total;
    }
    this.showTotal = (_totalCount) => `共${this.realTotal}条数据`;
}

/**
 * @Description: 新的分页器构造函数
 * @Author: Oliver
 * @Modified by: 娄通略
 * @Date: 2020-08-14 15:26:44
 * @Param: currentPage:当前是第几页;pageSize:每页显示多少条;共有多少条数据
*/
export class CustomPagination {
    // 用于解决数据过多，导致取不到数据的问题
    // realTotal = 0
    constructor(currentPage, pageSize, total) {
        this.showSizeChanger = false; // 是否可以改变 pageSize
        // this.showQuickJumper = true; // 是否可以快速跳转至某页
        this.hideOnSinglePage = true; // 只有一页时是否隐藏分页器
        this.pageSize = pageSize; // 每页的条数
        this.current = currentPage;  // 当前页数
        // this.total = total;   // 数据总数
        // this.showTotal = (totalCount) => `共${totalCount}条数据`;

        // 用于解决数据过多，导致取不到数据的问题，初始最多只展示5个页码按钮
        dealTotal.call(this, currentPage, pageSize, total);

        this.itemRender = (current, type, originalElement) => {
            if (type === 'prev') {
                return (
                    <a className="ant-pagination-item-link" style={{ padding: '0 6px' }}>上一页</a>
                );
            }
            if (type === 'next') {
                return <a className="ant-pagination-item-link" style={{ padding: '0 6px' }}>下一页</a>;
            }
            return originalElement;
        }
    }
}


/**
 * @Description: 简单的分页器
 * @Author: Oliver
 * @Modified by: 娄通略
 * @Date: 2020-11-20 18:31:11
 * @Param: 无
*/
export class Pagination {
    // 用于解决数据过多，导致取不到数据的问题
    // realTotal = 0
    constructor(currentPage, pageSize, total) {
        this.showSizeChanger = false; // 是否可以改变 pageSize
        // this.showQuickJumper = true; // 是否可以快速跳转至某页
        this.hideOnSinglePage = true; // 只有一页时是否隐藏分页器
        this.pageSize = pageSize; // 每页的条数
        this.current = currentPage;  // 当前页数
        // this.total = total;   // 数据总数
        // this.showTotal = (totalCount) => `共${totalCount}条数据`;
        // this.itemRender = (current, type, originalElement) => {
        //     if (type === 'prev') {
        //         return (
        //             <a className="ant-pagination-item-link" style={{ padding: '0 6px' }}>上一页</a>
        //         );
        //     }
        //     if (type === 'next') {
        //         return <a className="ant-pagination-item-link" style={{ padding: '0 6px' }}>下一页</a>;
        //     }
        //     return originalElement;
        // }
    }
}

export const simplePagination = {
    pageSize: 1,
    hideOnSinglePage: false,  // 只有一页时是否隐藏分页器
}

/**
* @Description: 基本分页器构造函数
* @Author: Oliver
* @Modified by: 娄通略
* @Date: 2020-08-14 15:26:44
* @Param: currentPage:当前是第几页;pageSize:每页显示多少条;共有多少条数据
*/
export class BasePagination {
   constructor(currentPage, pageSize, total) {
       this.showSizeChanger = false; // 是否可以改变 pageSize
       this.showQuickJumper = true; // 是否可以快速跳转至某页
       this.hideOnSinglePage = true; // 只有一页时是否隐藏分页器
       this.pageSize = pageSize; // 每页的条数
       this.total = total;   // 数据总数
       this.current = currentPage;  // 当前页数
       this.showTotal = (totalCount) => `共${totalCount}条数据`;
       this.itemRender = (current, type, originalElement) => {
           if (type === 'prev') {
               return (
                   <a className="ant-pagination-item-link" style={{ padding: '0 6px' }}>上一页</a>
               );
           }
           if (type === 'next') {
               return <a className="ant-pagination-item-link" style={{ padding: '0 6px' }}>下一页</a>;
           }
           return originalElement;
       }
   }
}