"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

require("core-js/modules/es6.regexp.split");

require("./index.css");

var _base = require("../../utils/base.js");

var _icon = _interopRequireDefault(require("../icon"));

var _select = _interopRequireDefault(require("../select"));

var _vueNormalizeComponent = _interopRequireDefault(require("../../utils/vue-normalize-component.js"));

/* script */
var __vue_script__ = {
  name: (0, _base.createComponentName)('pagination'),
  mixins: [_base.componentMixin],
  props: {
    // 分页布局
    layout: {
      // - < [] > * ->
      type: String,
      default: '< [] >'
    },
    // 当前页数
    num: {
      type: Number,
      default: 1
    },
    // 每页条数
    pageSize: {
      type: Number,
      default: 20
    },
    // 数据总数
    total: {
      type: Number,
      default: 0
    },
    // 每页可以显示多少条
    sizes: {
      type: Array,
      default: function _default() {
        return [20, 40, 60];
      }
    },
    // 页数最多显示多少条
    pagerCount: {
      type: Number,
      default: 7
    },
    // 自定义渲染 可用于SEO
    renderItem: {
      type: Function
    }
  },
  computed: {
    pages() {
      return Math.ceil(this.total / this.pageSize);
    },

    numbers() {
      if (this.total <= this.pageSize) return [1];
      var pagerCount = this.pagerCount;
      var currentPage = Number(this.num);
      var pageCount = Number(this.pages);
      var showPrevMore = false;
      var showNextMore = false;

      if (pageCount > pagerCount) {
        if (currentPage > pagerCount - 3) {
          showPrevMore = true;
        }

        if (currentPage < pageCount - 3) {
          showNextMore = true;
        }
      }

      var array = [];

      if (showPrevMore && !showNextMore) {
        var startPage = pageCount - (pagerCount - 2);

        for (var i = startPage; i < pageCount; i += 1) {
          array.push(i);
        }
      } else if (!showPrevMore && showNextMore) {
        for (var _i = 2; _i < pagerCount; _i += 1) {
          array.push(_i);
        }
      } else if (showPrevMore && showNextMore) {
        var offset = Math.floor(pagerCount / 2) - 1;

        for (var _i2 = currentPage - offset; _i2 <= currentPage + offset; _i2 += 1) {
          array.push(_i2);
        }
      } else {
        for (var _i3 = 2; _i3 < pageCount; _i3 += 1) {
          array.push(_i3);
        }
      }

      if (showNextMore) {
        array.push('>>', this.pages);
      } else {
        array.push(this.pages);
      }

      if (showPrevMore) {
        array.unshift(1, '<<');
      } else {
        array.unshift(1);
      }

      return array;
    }

  },
  methods: {
    changeSize(value) {
      if (value === this.pageSize) return;
      this.$emit('update:size', value);
      this.$emit('change', this.num, value);
    },

    changeNum(value) {
      if (value === this.num) return;
      var num = value > this.pages ? this.pages : value;
      num = num > 0 ? num : 1;
      this.$emit('update:num', num);
      this.$emit('change', num, this.pageSize);
    },

    renderPrev() {
      var _this = this;

      var h = this.$createElement;
      var content = h("div", {
        on: {
          "click": function click() {
            return _this.changeNum(_this.num - 1);
          }
        },
        "class": this.bem('number', {
          'prev': true,
          disabled: this.num - 1 < 1
        })
      }, [h(_icon.default, {
        attrs: {
          name: "arrow-left"
        }
      })]);

      if (this.renderItem && this.num - 1 >= 1) {
        return this.renderItem(this.num - 1, 'prev', content);
      }

      return content;
    },

    renderNext() {
      var _this2 = this;

      var h = this.$createElement;
      var content = h("div", {
        "class": this.bem('number', {
          'next': true,
          disabled: this.num + 1 > this.pages
        }),
        on: {
          "click": function click() {
            return _this2.changeNum(_this2.num + 1);
          }
        }
      }, [h(_icon.default, {
        attrs: {
          name: "arrow-right"
        }
      })]);

      if (this.renderItem && this.num - 1 >= 1) {
        return this.renderItem(this.num + 1, 'next', content);
      }

      return content;
    },

    renderPager() {
      var h = this.$createElement;
      return h(_select.default, {
        "class": this.bem('pager'),
        attrs: {
          value: this.pageSize,
          options: this.sizes.map(function (size) {
            return {
              value: size,
              label: `每页${size}条`
            };
          })
        },
        on: {
          "change": this.changeSize
        }
      });
    },

    renderTotal() {
      var h = this.$createElement;
      return h("span", {
        "class": this.bem('total')
      }, ["\u5171", h("em", [this.total]), "\u6761"]);
    },

    renderJump() {
      var _this3 = this;

      var h = this.$createElement;
      return h("span", {
        "class": this.bem('jump')
      }, ["\u5230", h("gd-input", {
        style: {
          width: '60px'
        },
        on: {
          "enter": function enter(e) {
            return _this3.changeNum(Number(e.target.value));
          }
        }
      }), "\u9875"]);
    },

    renderNumber(i) {
      var _this4 = this;

      var h = this.$createElement;
      var body = i;

      if (i === '<<') {
        body = '•••';
      } else if (i === '>>') {
        body = '•••';
      }

      var isNumber = typeof i === 'number';
      var content = h("div", {
        "class": this.bem('number', {
          'active': i === this.num,
          ellipsis: !isNumber
        }),
        on: {
          "click": function click() {
            return isNumber && _this4.changeNum(i);
          }
        }
      }, [body]);

      if (this.renderItem) {
        return this.renderItem(i, isNumber ? 'number' : 'ellipsis', content);
      }

      return content;
    },

    renderNumbers() {
      var _this5 = this;

      var h = this.$createElement;
      return h("div", {
        attrs: {
          role: "ul",
          unselectable: "unselectable"
        },
        "class": this.bem('numbers')
      }, [this.numbers.map(function (value) {
        return _this5.renderNumber(value);
      })]);
    }

  },

  render() {
    var _this6 = this;

    var h = arguments[0];
    var types = this.layout.split(' ').filter(function (value) {
      return value;
    });
    var components = types.map(function (type) {
      switch (type) {
        case '-':
          return _this6.renderPager();

        case '[]':
          return _this6.renderNumbers();

        case '<':
          return _this6.renderPrev();

        case '>':
          return _this6.renderNext();

        case '*':
          return _this6.renderTotal();

        case '->':
          return _this6.renderJump();

        default:
          break;
      }

      return null;
    });
    return h("div", {
      "class": "gd-pagination"
    }, [components]);
  }

};
/* template */

/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = undefined;
/* component normalizer */

/* style inject */

/* style inject SSR */
var _default2 = (0, _vueNormalizeComponent.default)({}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = _default2;