import "antd-mobile/es/modal/style";
import _Modal from "antd-mobile/es/modal";
import "antd-mobile/es/list/style";
import _List from "antd-mobile/es/list";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { PureComponent, Component } from 'react';
import PropTypes from 'prop-types';
import closest from '../_util/closest';
var Item = _List.Item;
var prefixCls = 'eh-select-organization';

var SelectOrganization =
/*#__PURE__*/
function (_ref) {
  _inherits(SelectOrganization, _ref);

  function SelectOrganization() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, SelectOrganization);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(SelectOrganization)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "state", {});

    _defineProperty(_assertThisInitialized(_this), "onWrapTouchStart", function (e) {
      // fix touch to scroll background page on iOS
      if (!/iPhone|iPod|iPad/i.test(navigator.userAgent)) {
        return;
      }

      var pNode = closest(e.target, '.am-modal-content');

      if (!pNode) {
        e.preventDefault();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onClose", function () {
      var onClose = _this.props.onClose;

      if (onClose) {
        onClose();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onSelect", function (org) {
      var _this$props = _this.props,
          onClose = _this$props.onClose,
          onSelect = _this$props.onSelect;

      if (onSelect) {
        onSelect(org);
      }

      if (onClose) {
        onClose();
      }
    });

    return _this;
  }

  _createClass(SelectOrganization, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          visible = _this$props2.visible,
          dataList = _this$props2.dataList,
          nameKey = _this$props2.nameKey,
          valueKey = _this$props2.valueKey,
          title = _this$props2.title;

      if (!visible) {
        return null;
      }

      return React.createElement("div", {
        className: prefixCls
      }, React.createElement(_Modal, {
        visible: visible,
        transparent: true,
        maskClosable: false,
        onClose: this.onClose,
        title: title,
        footer: [{
          text: '取消',
          onPress: function onPress() {
            return _this2.onClose();
          }
        }],
        wrapProps: {
          onTouchStart: this.onWrapTouchStart
        },
        className: prefixCls
      }, React.createElement(_List, null, dataList.map(function (org) {
        return React.createElement(Item, {
          key: org[valueKey],
          onClick: function onClick() {
            return _this2.onSelect(org);
          }
        }, org[nameKey] || '');
      }))));
    }
  }]);

  return SelectOrganization;
}(PureComponent || Component);

SelectOrganization.propTypes = {
  visible: PropTypes.bool,
  dataList: PropTypes.arrayOf(PropTypes.object),
  title: PropTypes.string,
  onClose: PropTypes.func,
  onSelect: PropTypes.func,
  // name 的 key 值，决定从数据中获取哪个字段作为 name，默认为 name
  nameKey: PropTypes.string,
  // value 的 key 值，决定从数据中获取哪个字段作为 value，默认为 id
  valueKey: PropTypes.string
};
SelectOrganization.defaultProps = {
  visible: false,
  dataList: [],
  title: '请选择企业',
  nameKey: 'name',
  valueKey: 'id'
};
export default SelectOrganization;