'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _InputText = require('./../InputText');

var _InputText2 = _interopRequireDefault(_InputText);

var _PickerDistrict = require('./../PickerDistrict');

var _PickerDistrict2 = _interopRequireDefault(_PickerDistrict);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

// 函数组件因为没有实例, 所以也没有ref, 必须通过forwardRef回调ref
var InputDistrict = (0, _react.forwardRef)(function (_ref, ref) {
  var value = _ref.value,
      onClick = _ref.onClick,
      onChange = _ref.onChange,
      selected = _ref.selected,
      _ref$type = _ref.type,
      type = _ref$type === undefined ? '' : _ref$type,
      _ref$pickerProps = _ref.pickerProps,
      pickerProps = _ref$pickerProps === undefined ? {
    maskAttribute: {},
    submitAttribute: {}
  } : _ref$pickerProps,
      others = _objectWithoutProperties(_ref, ['value', 'onClick', 'onChange', 'selected', 'type', 'pickerProps']);

  var _useState = (0, _react.useState)(false),
      show = _useState[0],
      setShow = _useState[1];

  // 点击文本框


  function onClickInput() {
    if (onClick) onClick.apply(undefined, arguments);
    if (others.readOnly) return;
    setShow(!show);
  }
  // 点击遮罩
  function onClickMask(e) {
    if (pickerProps && pickerProps.maskAttribute && pickerProps.maskAttribute.onClick) {
      pickerProps.maskAttribute.onClick(e);
    }
    setShow(!show);
  }
  // 点击确定按钮
  function onClickSubmit(e, value, options) {
    if (pickerProps && pickerProps.submitAttribute && pickerProps.submitAttribute.onClick) {
      pickerProps.submitAttribute.onClick(e, value, options);
    }
    // 赋值
    if (onChange) {
      onChange(e, value, options);
    }
    // 隐藏框
    setShow(!show);
  }
  // 点击取消按钮
  function onClickCancel(e) {
    if (pickerProps && pickerProps.cancelAttribute && pickerProps.cancelAttribute.onClick) {
      pickerProps.cancelAttribute.onClick(e);
    }
    setShow(!show);
  }
  return _react2.default.createElement(
    _react.Fragment,
    null,
    _react2.default.createElement(_InputText2.default, _extends({
      ref: ref,
      value: value
    }, others, {
      type: 'text',
      readOnly: true,
      onClick: onClickInput
    })),
    _react2.default.createElement(_PickerDistrict2.default, _extends({}, pickerProps, {
      maskAttribute: _extends({}, pickerProps.maskAttribute, {
        onClick: onClickMask
      }),
      submitAttribute: _extends({}, pickerProps.submitAttribute, {
        onClick: onClickSubmit
      }),
      cancelAttribute: _extends({}, pickerProps.cancelAttribute, {
        onClick: onClickCancel
      }),
      selected: selected,
      type: type,
      value: value,
      show: pickerProps.show === undefined ? show : pickerProps.show
    }))
  );
});

exports.default = InputDistrict;
module.exports = exports['default'];