const Paginator = {
  view: function(vnode) {
    let total = vnode.attrs.total
    let currentPage = vnode.attrs.page
    let perPage = vnode.attrs.perPage || 10
    let maxPage = total / perPage + 1

    if (total <= perPage) return null

    return m('paginator', [
      currentPage > 1
        ? [
            m(m.route.Link, {
              href: vnode.attrs.base,
            }, 'First'),
            m(m.route.Link, {
              href: vnode.attrs.base + (currentPage > 2
                ? '?page=' + (currentPage - 1)
                : ''
              ),
            }, 'Previous'),
          ]
        : [ m('div'), m('div')],
      m('div', 'Page ' + currentPage),
      currentPage < maxPage
        ? [
            m(m.route.Link, {
              href: vnode.attrs.base + '?page=' + (currentPage + 1),
            }, 'Next'),
            m(m.route.Link, {
              href: vnode.attrs.base + '?page=' + maxPage,
            }, 'Last')
          ]
        : [ m('div'), m('div')],
    ])
  },
}

module.exports = Paginator
