'use strict';

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

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);

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

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

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

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

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

var _extends4 = _interopRequireDefault(_extends3);

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _class, _temp; /**
                    * 借助全局变量RcInstance.APIS增加对路由切换的管理
                    */

// import { hackForiOS11 } from '../utils/utils';


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _index = require('../rc-instance/index');

var _index2 = _interopRequireDefault(_index);

require('./MModals.scss');

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

var Dom = document.body;
var ModalsStatics = {};
var count = 0;
var modalObj = {};
var vaildActions = function vaildActions(actions) {
	if (!(actions instanceof Array)) {
		console.error("actions is not a Array");
		return false;
	}
	for (var i = 0; i < actions.length; i++) {
		if ((0, _typeof3.default)(actions[i]) !== "object") {
			console.error("actions's child is not a Object");
			return false;
		}
	}
	return true;
};
ModalsStatics = {
	/**
  * 根据antd-mobile的设计改写
  * 多增加已个options选项
  * @param  {Object} options { showClose, className, style, type, onMaskCb }
  */
	alert: function alert(title, message) {
		var actions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
		var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
		var _options = options,
		    _options$showClose = _options.showClose,
		    showClose = _options$showClose === undefined ? false : _options$showClose;

		if (process.env.NODE_ENV === 'development' && !vaildActions(actions)) return;
		var div = document.createElement('div');
		var key = 'modals' + count;
		Dom.appendChild(div);
		options = (0, _extends4.default)({}, options, {
			count: count,
			title: title,
			message: message,
			actions: actions,
			onCloseSoon: function onCloseSoon() {
				_reactDom2.default.unmountComponentAtNode(div);
				Dom.removeChild(div);
				delete _index2.default.APIS[key];
				delete modalObj[key];
			},
			onClose: function onClose() {
				options.onCloseSoon();
			}
		});
		modalObj = (0, _extends4.default)({}, modalObj, (0, _defineProperty3.default)({}, key, div));
		_index2.default.APIS[key] = div;
		count++;
		return _reactDom2.default.render(_react2.default.createElement(Modals, options), div);
	},
	operation: function operation() {
		var actions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

		if (process.env.NODE_ENV === 'development' && !vaildActions(actions)) return;
		ModalsStatics.alert("", "", actions, { type: "operation" });
	},

	/**
  * 根据antd-mobile的规则定义
  * @param  {String} type default, secure-text, login-password
  */
	prompt: function prompt(title, message) {
		var actions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
		var inputType = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
		var defaultValue = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
		var placeholders = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
		var options = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : { inputStyle: [], maxLength: [] };

		if (process.env.NODE_ENV === 'development' && !vaildActions(actions)) return;
		switch (inputType) {
			case "secure-text":
				inputType = ["password"];
				break;
			case "login-password":
				inputType = ["text", "password"];
				break;
			case "default":
				inputType = ["text"];
				break;
			default:
				inputType = typeof inputType !== 'string' && inputType.length > 0 ? inputType : ["text"];
				break;
		}
		ModalsStatics.alert(title, message, actions, (0, _extends4.default)({}, options, {
			inputType: [].concat((0, _toConsumableArray3.default)(inputType)),
			defaultValue: defaultValue instanceof Array ? [].concat((0, _toConsumableArray3.default)(defaultValue)) : [defaultValue],
			placeholders: placeholders instanceof Array ? [].concat((0, _toConsumableArray3.default)(placeholders)) : [placeholders]
		}));
	},
	select: function select(title, message) {
		var actions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
		var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
		var _options$selectData = options.selectData,
		    selectData = _options$selectData === undefined ? [] : _options$selectData,
		    _options$selectValue = options.selectValue,
		    selectValue = _options$selectValue === undefined ? 'value' : _options$selectValue,
		    _options$selectLabel = options.selectLabel,
		    selectLabel = _options$selectLabel === undefined ? 'label' : _options$selectLabel,
		    _options$selectCur = options.selectCur,
		    selectCur = _options$selectCur === undefined ? '' : _options$selectCur,
		    _options$selectParams = options.selectParams,
		    selectParams = _options$selectParams === undefined ? {} : _options$selectParams;

		var __arr = [],
		    __obj = {};
		for (var i = 0; i < selectData.length; i++) {
			__obj = selectData[i] || {};
			__arr = [].concat((0, _toConsumableArray3.default)(__arr), [{ value: __obj[selectValue], label: __obj[selectLabel], params: __obj[selectParams] }]);
		}
		ModalsStatics.alert(title, message, actions, (0, _extends4.default)({}, options, {
			selectData: __arr,
			selectValue: selectValue,
			selectLabel: selectLabel,
			type: 'select'
		}));
	}
};
var Modals = (_temp = _class = function (_Component) {
	(0, _inherits3.default)(Modals, _Component);

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

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

		_this.handleClose = _this.handleClose.bind(_this);
		_this.handlePress = _this.handlePress.bind(_this);

		_this.state = {
			selectCur: '',
			selectParams: {}
		};
		return _this;
	}
	/**
  * 手动清理全部弹窗
  */


	(0, _createClass3.default)(Modals, [{
		key: 'componentWillMount',
		value: function componentWillMount() {
			var _props = this.props,
			    _props$selectCur = _props.selectCur,
			    selectCur = _props$selectCur === undefined ? '' : _props$selectCur,
			    selectParams = _props.selectParams;

			this.setState({
				selectCur: selectCur,
				selectParams: selectParams
			});

			// hack
			// hackForiOS11.set();
		}
	}, {
		key: 'componentDidMount',
		value: function componentDidMount() {}
	}, {
		key: 'componentWillUnmount',
		value: function componentWillUnmount() {
			this.timer && clearTimeout(this.timer);

			// hack
			// hackForiOS11.remove();
		}
		/**
   * 给外包暴露方法，只删除自己
   */

	}, {
		key: 'close',
		value: function close(isMaskClick) {
			isMaskClick && this && this.props.onMaskCb && this.props.onMaskCb();
			this && this.props.onClose && this.props.onClose();
		}
	}, {
		key: 'handleClose',
		value: function handleClose(event, isMaskClick) {
			var _this2 = this;

			event && event.preventDefault();
			event && event.stopPropagation();
			this.refs.fixed.classList.add("__leave");
			this.refs.bg.classList.add("__bg-leave");
			this.timer = setTimeout(function () {
				// 主线程
				_this2.close(isMaskClick);
			}, 201);
		}
	}, {
		key: 'handlePress',
		value: function handlePress(event, atIndex) {
			var _this3 = this;

			var _props2 = this.props,
			    actions = _props2.actions,
			    _props2$inputType = _props2.inputType,
			    inputType = _props2$inputType === undefined ? [] : _props2$inputType,
			    type = _props2.type;

			var values = inputType.map(function (item, index) {
				return _this3.refs['input' + index].value;
			});
			if (type === 'select') {
				values = [this.state.selectCur, this.state.selectParams];
			}
			actions[atIndex].onPress && actions[atIndex].onPress.apply(null, values);
			// 关闭
			this.handleClose();
		}
	}, {
		key: 'render',
		value: function render() {
			var _this4 = this;

			var _props3 = this.props,
			    type = _props3.type,
			    title = _props3.title,
			    message = _props3.message,
			    showClose = _props3.showClose,
			    actions = _props3.actions,
			    _props3$className = _props3.className,
			    className = _props3$className === undefined ? "" : _props3$className,
			    _props3$style = _props3.style,
			    style = _props3$style === undefined ? {} : _props3$style,
			    _props3$inputType = _props3.inputType,
			    inputType = _props3$inputType === undefined ? [] : _props3$inputType,
			    _props3$inputStyle = _props3.inputStyle,
			    inputStyle = _props3$inputStyle === undefined ? [] : _props3$inputStyle,
			    _props3$defaultValue = _props3.defaultValue,
			    defaultValue = _props3$defaultValue === undefined ? [] : _props3$defaultValue,
			    _props3$placeholders = _props3.placeholders,
			    placeholders = _props3$placeholders === undefined ? [] : _props3$placeholders,
			    _props3$selectData = _props3.selectData,
			    selectData = _props3$selectData === undefined ? [] : _props3$selectData,
			    _props3$maxLength = _props3.maxLength,
			    maxLength = _props3$maxLength === undefined ? [] : _props3$maxLength;
			var _state = this.state,
			    selectCur = _state.selectCur,
			    selectParams = _state.selectParams;

			return _react2.default.createElement(
				'div',
				{ className: 'rcm-modal ' + className, style: (0, _extends4.default)({}, style) },
				_react2.default.createElement('div', { className: '__bg __bg_enter', onClick: function onClick(e) {
						return _this4.handleClose(e, 1);
					}, onTouchMove: function onTouchMove(e) {
						return e.preventDefault();
					}, ref: 'bg' }),
				_react2.default.createElement(
					'div',
					{ className: '__fixed __enter', ref: 'fixed' },
					_react2.default.createElement(
						'div',
						{ className: '__close ' + (title && '__bb') },
						(typeof title === 'undefined' ? 'undefined' : (0, _typeof3.default)(title)) === "object" ? title : title && _react2.default.createElement(
							'div',
							{ className: '__title' },
							title
						),
						showClose && _react2.default.createElement(
							'span',
							{ onClick: this.handleClose },
							'\u2715'
						)
					),
					(typeof message === 'undefined' ? 'undefined' : (0, _typeof3.default)(message)) === "object" ? message : message && _react2.default.createElement(
						'div',
						{ className: '__message' },
						message
					),
					_react2.default.createElement(
						'div',
						{ className: '__flex __jc-c __jd-c ' + (inputType.length > 0 && '__input') + ' ' + (!message && '__mt') },
						inputType.map(function (item, index) {
							return _react2.default.createElement('input', {
								key: index,
								defaultValue: defaultValue[index] || "",
								style: inputStyle[index] || {},
								type: inputType[index],
								placeholder: placeholders[index] || "",
								ref: 'input' + index,
								maxLength: maxLength[index]
							});
						})
					),
					type == "select" && selectData.length > 0 && _react2.default.createElement(
						'div',
						{ className: '__select' },
						selectData.map(function () {
							var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
							var index = arguments[1];

							var is_select = item.value == selectCur;
							return _react2.default.createElement(
								'div',
								{
									key: item.value,
									onClick: function onClick(e) {
										return _this4.setState({ selectCur: item.value, selectParams: item.params });
									},
									className: '__flex __select_pd __jc-sb __bb ' + (is_select && '__select_active')
								},
								_react2.default.createElement(
									'div',
									null,
									item.label
								),
								is_select && _react2.default.createElement('i', { className: 'iconfont icon-completed', style: { fontSize: '36px' } })
							);
						})
					),
					// 1个按钮
					actions.length > 0 && actions.length == 1 && type !== "operation" && _react2.default.createElement(
						'div',
						{ className: '__flex __pd __jc-c' },
						_react2.default.createElement(
							'div',
							{ className: '__1of2 __flex __jc-c' },
							_react2.default.createElement(
								'div',
								{
									className: '__btn __orange',
									onClick: function onClick(e) {
										_this4.handlePress(e, 0);
									},
									style: (0, _extends4.default)({}, actions[0].style || {})
								},
								actions[0].text || "确定"
							)
						)
					),
					// 2个按钮
					actions.length > 0 && actions.length == 2 && type !== "operation" && _react2.default.createElement(
						'div',
						{ className: '__flex __pd __jc-c' },
						_react2.default.createElement(
							'div',
							{ className: '__col __flex __jc-c' },
							_react2.default.createElement(
								'div',
								{
									className: '__btn',
									onClick: function onClick(e) {
										_this4.handlePress(e, 0);
									},
									style: (0, _extends4.default)({}, actions[0].style || {})
								},
								actions[0].text || "取消"
							)
						),
						_react2.default.createElement(
							'div',
							{ className: '__col __flex __jc-c' },
							_react2.default.createElement(
								'div',
								{
									className: '__btn __orange',
									onClick: function onClick(e) {
										_this4.handlePress(e, 1);
									},
									style: (0, _extends4.default)({}, actions[1].style || {})
								},
								actions[1].text || "确定"
							)
						)
					),
					// 3个按钮+
					(actions.length > 0 && actions.length > 2 || type === "operation") && _react2.default.createElement(
						'div',
						{ className: '__flex __jd-c' },
						actions.map(function (iten, index) {
							return _react2.default.createElement(
								'div',
								{
									className: '__btn1 ' + (index != actions.length - 1 && '__bb'),
									key: index,
									style: (0, _extends4.default)({}, actions[index].style || {}),
									onClick: function onClick(e) {
										_this4.handlePress(e, index);
									}
								},
								actions[index].text || "-"
							);
						})
					)
				)
			);
		}
	}]);
	return Modals;
}(_react.Component), _class.alert = ModalsStatics.alert, _class.operation = ModalsStatics.operation, _class.prompt = ModalsStatics.prompt, _class.select = ModalsStatics.select, _class.closeAll = function () {
	for (var i in modalObj) {
		if (modalObj[i] && modalObj.hasOwnProperty(i)) {
			_reactDom2.default.unmountComponentAtNode(modalObj[i]);
			Dom.removeChild(modalObj[i]);
			delete modalObj[i];
			delete _index2.default.APIS[i];
		}
	}
	// 初始化
	count = 0;
	modalObj = {};
}, _temp);

Modals.propTypes = {
	// type,
	// title,
	// message,
	// showClose,
	// actions,
	// className = "",
	// style = {},
	// inputType = [],
	// placeholders,
	// defaultValue
};
Modals.defaultProps = {};
exports.default = Modals;