'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _pagination = require('antd/lib/pagination');

var _pagination2 = _interopRequireDefault(_pagination);

var _spin = require('antd/lib/spin');

var _spin2 = _interopRequireDefault(_spin);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _extends8 = require('babel-runtime/helpers/extends');

var _extends9 = _interopRequireDefault(_extends8);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

require('antd/lib/pagination/style');

require('antd/lib/spin/style');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

require('./Paging.scss');

var _SelectionCheckboxAll = require('./SelectionCheckboxAll');

var _SelectionCheckboxAll2 = _interopRequireDefault(_SelectionCheckboxAll);

var _utils = require('../utils/utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Paging = function (_Component) {
	(0, _inherits3.default)(Paging, _Component);

	function Paging(props, context) {
		(0, _classCallCheck3.default)(this, Paging);

		var _this = (0, _possibleConstructorReturn3.default)(this, (Paging.__proto__ || Object.getPrototypeOf(Paging)).call(this, props, context));

		_this.handleChange = function (page) {
			var $props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.props;
			var _history = $props.history,
			    loadDataForPaging = $props.loadDataForPaging,
			    show = $props.show;

			if (!show) return;
			if (_history) {
				var _getParseUrl = (0, _utils.getParseUrl)(),
				    path = _getParseUrl.path,
				    query = _getParseUrl.query;

				page = page || query.page || 1; // 不要动，就是这样

				_history && window.history.replaceState(null, null, (0, _utils.getConstructUrl)({
					path: path,
					query: (0, _extends9.default)({}, query, {
						page: page
					})
				}));
			}
			loadDataForPaging && loadDataForPaging(page || 1);
		};

		_this.bindScroll = function () {
			if (_this.wrapper) {
				_this.scrollContainer = document.querySelector('' + _this.wrapper);
			}
		};

		_this.loadDataFirst = function () {
			var $props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
			// 第一次请求
			var isEnd = $props.isEnd,
			    curPage = $props.curPage;

			if (curPage == 0) {
				// 这里使用this.props.curPage
				var nextPage = _this.props.resetPage == $props.resetPage ? _this.props.curPage : 1;
				_this.handleChange(nextPage, $props);
			}
		};

		_this.handleCheckAll = function () {
			var _this$props = _this.props,
			    rowSelection = _this$props.rowSelection,
			    curPage = _this$props.curPage,
			    dataSource = _this$props.dataSource;

			if (!rowSelection) {
				console.error('当前不属于可选择状态');
				return;
			}
			var _dataSource$itemObj = dataSource.itemObj,
			    itemObj = _dataSource$itemObj === undefined ? {} : _dataSource$itemObj;
			var onChange = rowSelection.onChange,
			    onSelectAll = rowSelection.onSelectAll,
			    onCancelAll = rowSelection.onCancelAll;

			var selectedRowKeys = [],
			    selectedRows = [];
			var curPageCheck = _this.state.checkArr[curPage] || _this.initCheckedData || {};
			for (var i = 0; i < _this.changeableRows.length; i++) {
				if (!curPageCheck[_this.changeableRows[i]]) {
					// 未选中
					var checkedRows = _this.handleSetSelect('check');
					for (var key in checkedRows) {
						if (checkedRows[key]) {
							selectedRowKeys.push(key);
							selectedRows.push(itemObj[key]);
						}
					}
					onSelectAll && onSelectAll(selectedRowKeys, selectedRows);
					onChange && onChange(selectedRowKeys, selectedRows);
					return;
				}
			}
			_this.handleSetSelect('uncheck');
			onCancelAll && onCancelAll([], []);
			onChange && onChange([], []);
			return;
		};

		_this.handleSetSelect = function (type) {
			var curPage = _this.props.curPage;

			var checkArr = {};

			if (_this.changeableRows.length === 0) {
				// 如果当前页全部不可选，则title中的勾选框不勾选
				var _checkArr = {},
				    curPageData = _this.state.checkArr[curPage];
				for (var key in curPageData) {
					_checkArr[key] = false;
				}
				_this.setState({
					checkArr: (0, _extends9.default)({}, _this.state.checkArr, (0, _defineProperty3.default)({}, curPage, (0, _extends9.default)({}, _checkArr)))
				});
			} else {
				for (var i = 0; i < _this.changeableRows.length; i++) {
					checkArr[_this.changeableRows[i]] = type === 'check';
				}
				_this.setState({
					checkArr: (0, _extends9.default)({}, _this.state.checkArr, (0, _defineProperty3.default)({}, curPage, (0, _extends9.default)({}, checkArr)))
				});
			}

			return checkArr;
		};

		_this.handleSelectChange = function (event, item) {
			var _this$props2 = _this.props,
			    rowSelection = _this$props2.rowSelection,
			    curPage = _this$props2.curPage,
			    dataSource = _this$props2.dataSource;
			var _dataSource$itemObj2 = dataSource.itemObj,
			    itemObj = _dataSource$itemObj2 === undefined ? {} : _dataSource$itemObj2;
			var onChange = rowSelection.onChange;

			var selectedRowKeys = [],
			    selectedRows = [];
			_this.setState({
				checkArr: (0, _extends9.default)({}, _this.state.checkArr, (0, _defineProperty3.default)({}, curPage, (0, _extends9.default)({}, _this.state.checkArr[curPage], (0, _defineProperty3.default)({}, item, event.target.checked))))
			}, function () {
				for (var key in _this.state.checkArr[curPage]) {
					if (_this.state.checkArr[curPage][key]) {
						selectedRowKeys.push(key);
						selectedRows.push(itemObj[key]);
					}
				}
				onChange && onChange(selectedRowKeys, selectedRows);
			});
		};

		_this.resetCheckState = function (curPage) {
			_this.setState({
				checkArr: (0, _extends9.default)({}, _this.state.checkArr, (0, _defineProperty3.default)({}, curPage, null))
			});
		};

		_this.getCheckboxProps = function (itemData) {
			var rowSelection = _this.props.rowSelection;

			return rowSelection.getCheckboxProps && rowSelection.getCheckboxProps(itemData) || {};
		};

		_this.renderTBody = function () {
			var _this$props3 = _this.props,
			    rowSelection = _this$props3.rowSelection,
			    curPage = _this$props3.curPage,
			    dataSource = _this$props3.dataSource,
			    renderRow = _this$props3.renderRow,
			    actions = _this$props3.actions,
			    rowProps = _this$props3.rowProps,
			    tHide = _this$props3.tHide,
			    listClassName = _this$props3.listClassName;
			var _dataSource$itemArr = dataSource.itemArr,
			    itemArr = _dataSource$itemArr === undefined ? {} : _dataSource$itemArr,
			    _dataSource$itemObj3 = dataSource.itemObj,
			    itemObj = _dataSource$itemObj3 === undefined ? {} : _dataSource$itemObj3;

			var curRowData = itemArr[curPage] || [];
			_this.changeableRows = [].concat((0, _toConsumableArray3.default)(curRowData));
			if (rowSelection) {
				_this.changeableRows = curRowData.filter(function (item, i) {
					return !_this.getCheckboxProps(itemObj[item]).disabled;
				});
			}
			var Tag = 'tbody';
			if (tHide) {
				Tag = 'div';
			}
			return _react2.default.createElement(
				Tag,
				{ className: listClassName || "", ref: function ref(node) {
						return _this.scrollContainer = _this.scrollContainer || node;
					} },
				curRowData.map(function (item, index) {
					if (rowSelection) {
						var checked = void 0;
						if (_this.state.checkArr[curPage]) {
							// console.log(this.state.checkArr[curPage][item], this.getCheckboxProps(itemObj[item]).disabled);
							if (!_this.getCheckboxProps(itemObj[item]).disabled || _this.state.checkArr[curPage][item] !== undefined && _this.getCheckboxProps(itemObj[item]).disabled) {
								checked = _this.state.checkArr[curPage][item];
							} else {
								checked = _this.getCheckboxProps(itemObj[item]).checked;
							}
						} else {
							checked = _this.getCheckboxProps(itemObj[item]).checked;
						}

						return (0, _react.createElement)(renderRow, (0, _extends9.default)({
							key: item, // index -> item
							rowSelection: {
								disabled: _this.getCheckboxProps(itemObj[item]).disabled,
								checked: checked,
								onChange: function onChange(e) {
									_this.handleSelectChange(e, item);
								}
							},
							itemData: itemObj[item],
							actions: actions
						}, rowProps));
					}
					return (0, _react.createElement)(renderRow, (0, _extends9.default)({
						key: item, // index -> item
						itemData: itemObj[item],
						actions: actions
					}, rowProps));
				})
			);
		};

		_this.renderTable = function () {
			var _this$props4 = _this.props,
			    rowSelection = _this$props4.rowSelection,
			    title = _this$props4.title,
			    tHide = _this$props4.tHide,
			    curPage = _this$props4.curPage;

			var columns = [].concat((0, _toConsumableArray3.default)(title));
			if (rowSelection) {

				columns.unshift(_react2.default.createElement(_SelectionCheckboxAll2.default, {
					data: _this.state.checkArr[curPage],
					onChange: _this.handleCheckAll,
					changeableRows: _this.changeableRows
				}));
			}
			if (tHide) return _this.renderTBody();
			return _react2.default.createElement(
				'table',
				{ className: '__table' },
				_react2.default.createElement(
					'thead',
					null,
					_react2.default.createElement(
						'tr',
						null,
						columns.map(function (item, index) {
							return _react2.default.createElement(
								'th',
								{ key: index },
								item
							);
						})
					)
				),
				_this.renderTBody()
			);
		};

		_this.state = {
			checkArr: {}
		};
		_this.wrapper = props.wrapper;
		_this.scrollContainer = null;
		_this.changeableRows = [];
		_this.initCheckedData = Object.create(null);
		return _this;
	}

	(0, _createClass3.default)(Paging, [{
		key: 'componentDidMount',
		value: function componentDidMount() {
			if (this.props.isEnd === 0) {
				// 禁用，加载完成或者加载中无视
				this.loadDataFirst(this.props);
			}
			this.bindScroll();
		}
	}, {
		key: 'componentWillReceiveProps',
		value: function componentWillReceiveProps(nextProps) {
			var _this2 = this;

			if (this.props.resetPrvScrollTop && nextProps.resetPrvScrollTop != this.props.resetPrvScrollTop) {
				this.prvScrollTop = 0;
				this.scrollContainer.scrollTop = 0; // 置顶
			}
			if (nextProps.isEnd === 0) {
				// 禁用，加载完成或者加载中无视
				this.loadDataFirst(nextProps);
			}
			if (this.props.curPage !== nextProps.curPage) {
				var rowSelection = nextProps.rowSelection,
				    curPage = nextProps.curPage,
				    dataSource = nextProps.dataSource;
				var _dataSource$itemArr2 = dataSource.itemArr,
				    itemArr = _dataSource$itemArr2 === undefined ? {} : _dataSource$itemArr2,
				    _dataSource$itemObj4 = dataSource.itemObj,
				    itemObj = _dataSource$itemObj4 === undefined ? {} : _dataSource$itemObj4;

				var curRowData = itemArr[curPage] || [];
				if (rowSelection) {
					// 获取下一页面可勾选的行
					this.changeableRows = curRowData.filter(function (item, i) {
						return !_this2.getCheckboxProps(itemObj[item]).disabled;
					});
					var checkArr = {};
					// 将下一页的初始数据设置到state中
					if (!this.state.checkArr[nextProps.curPage]) {
						for (var i = 0; i < this.changeableRows.length; i++) {
							checkArr[this.changeableRows[i]] = this.getCheckboxProps(itemObj[this.changeableRows[i]]).checked;
						}
						this.setState({
							checkArr: (0, _extends9.default)({}, this.state.checkArr, (0, _defineProperty3.default)({}, curPage, (0, _extends9.default)({}, checkArr)))
						});
					}
				}
			}
		}
	}, {
		key: 'componentDidCatch',
		value: function componentDidCatch(error, info) {
			console.log(error, info);
		}
	}, {
		key: 'render',
		value: function render() {
			var _this3 = this;

			var _props = this.props,
			    isEnd = _props.isEnd,
			    title = _props.title,
			    _props$style = _props.style,
			    style = _props$style === undefined ? {} : _props$style,
			    className = _props.className,
			    curPage = _props.curPage,
			    totalPage = _props.totalPage,
			    children = _props.children,
			    tHide = _props.tHide,
			    renderPagination = _props.renderPagination,
			    pagination = (0, _objectWithoutProperties3.default)(_props, ['isEnd', 'title', 'style', 'className', 'curPage', 'totalPage', 'children', 'tHide', 'renderPagination']);

			return _react2.default.createElement(
				'div',
				{ className: (0, _classnames2.default)("c-paging", className), style: (0, _extends9.default)({}, style) },
				_react2.default.createElement(
					'div',
					{ className: '__conent' },
					this.renderTable()
				),
				isEnd === 1 && _react2.default.createElement(_spin2.default, null),
				isEnd === 3 && _react2.default.createElement(
					'div',
					{ className: '__error' },
					'\u52A0\u8F7D\u5931\u8D25...'
				),
				_react2.default.createElement(
					'div',
					{ className: '__footer' },
					_react2.default.createElement(
						'div',
						{ className: '__left' },
						children
					),
					typeof renderPagination === 'function' ? (0, _react.createElement)(renderPagination, {
						onChange: this.handleChange
					}) : _react2.default.createElement(_pagination2.default, (0, _extends9.default)({}, pagination, {
						showQuickJumper: true,
						defaultPageSize: 1,
						current: curPage,
						total: totalPage,
						onChange: function onChange(page) {
							return _this3.handleChange(page, _this3.props);
						}
					}))
				)
			);
		}
	}]);
	return Paging;
}(_react.Component);

Paging.propTypes = {
	title: _propTypes2.default.array,
	show: _propTypes2.default.bool,
	className: _propTypes2.default.string,
	isEnd: _propTypes2.default.number.isRequired,
	curPage: _propTypes2.default.number.isRequired,
	totalPage: _propTypes2.default.number.isRequired,
	loadDataForPaging: _propTypes2.default.func.isRequired,
	resetPrvScrollTop: _propTypes2.default.number,
	resetPage: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
	tHide: _propTypes2.default.bool,
	rowSelection: _propTypes2.default.object,
	dataSource: _propTypes2.default.object,
	renderRow: _propTypes2.default.func.isRequired,
	renderPagination: _propTypes2.default.func,
	rowProps: _propTypes2.default.object,
	actions: _propTypes2.default.object,
	history: _propTypes2.default.bool
};
Paging.defaultProps = {
	title: [],
	tHide: false,
	className: '__defalut',
	rowSelection: null,
	dataSource: {},
	history: false,
	show: true
};
exports.default = Paging;