'use strict';

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

var _class, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactRedux = require('react-redux');

var _reactBootstrap = require('react-bootstrap');

var _lodash = require('lodash');

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

const { config, i18n } = window;
const __ = i18n.setting.__.bind(i18n.setting);

function _ref(state, props) {
  return {
    type: props.type,
    enable: (0, _lodash.get)(state.config, `poi.mapStartCheck.${props.type}.enable`, false),
    minFreeSlots: (0, _lodash.get)(state.config, `poi.mapStartCheck.${props.type}.minFreeSlots`, '')
  };
}

function _ref2(v) {
  return !isNaN(parseInt(v)) && parseInt(v) >= 0;
}

const SlotCheckConfig = (0, _reactRedux.connect)(() => {
  return _ref;
})((_temp = _class = class SlotCheckConfig extends _react.Component {
  constructor(props) {
    super(props);
    this.CheckValid = _ref2;

    this.handleToggleInput = () => {
      if (this.state.showInput) {
        this.handleDisable();
      } else {
        const num = this.state.value;
        this.setState({
          showInput: true,
          value: this.CheckValid(num) ? parseInt(num) : ''
        });
      }
    };

    this.handleChange = e => {
      this.setState({ value: e.target.value });
    };

    this.handleSubmit = e => {
      e.preventDefault();
      if (this.CheckValid(this.state.value)) {
        const n = parseInt(this.state.value);
        config.set(`poi.mapStartCheck.${this.props.type}`, {
          enable: true,
          minFreeSlots: n
        });
        this.setState({
          showInput: false,
          value: n
        });
      } else {
        this.handleDisable();
      }
    };

    this.handleDisable = () => {
      config.set(`poi.mapStartCheck.${this.props.type}.enable`, false);
      this.setState({ showInput: false });
    };

    this.state = {
      showInput: false,
      value: props.minFreeSlots
    };
  }

  render() {
    let toggleBtnStyle = this.props.enable ? 'success' : 'default';
    if (this.state.showInput) {
      toggleBtnStyle = 'danger';
    }
    let toggleBtnTxt = this.props.enable ? 'ON' : 'OFF';
    if (this.state.showInput) {
      toggleBtnTxt = __('Disable');
    }
    const toggleBtn = _react2.default.createElement(
      _reactBootstrap.Button,
      { onClick: this.handleToggleInput, bsSize: 'xs',
        bsStyle: toggleBtnStyle, style: { verticalAlign: 'text-bottom' } },
      toggleBtnTxt
    );
    const inputValid = this.CheckValid(this.state.value);
    const submitBtn = _react2.default.createElement(
      _reactBootstrap.Button,
      { type: 'submit',
        bsStyle: inputValid ? 'success' : 'danger' },
      inputValid ? __('Save') : __('Disable')
    );
    return _react2.default.createElement(
      'div',
      { style: { margin: '5px 15px' } },
      _react2.default.createElement(
        'form',
        { onSubmit: this.handleSubmit },
        _react2.default.createElement(
          'div',
          null,
          __(`${this.props.type} slots`),
          ' ',
          toggleBtn
        ),
        _react2.default.createElement(
          _reactBootstrap.Collapse,
          { 'in': this.state.showInput },
          _react2.default.createElement(
            'div',
            null,
            _react2.default.createElement(
              _reactBootstrap.Well,
              null,
              _react2.default.createElement(
                _reactBootstrap.FormGroup,
                null,
                _react2.default.createElement(
                  _reactBootstrap.ControlLabel,
                  null,
                  __(`Warn if the number of free ${this.props.type} slots is less than`)
                ),
                _react2.default.createElement(
                  _reactBootstrap.InputGroup,
                  { bsSize: 'small' },
                  _react2.default.createElement(_reactBootstrap.FormControl, { type: 'text',
                    bsStyle: inputValid ? 'success' : 'error',
                    value: this.state.value,
                    onChange: this.handleChange }),
                  _react2.default.createElement(
                    _reactBootstrap.InputGroup.Button,
                    null,
                    submitBtn
                  )
                )
              )
            )
          )
        )
      )
    );
  }
}, _class.propTypes = {
  minFreeSlots: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  type: _propTypes2.default.string,
  enable: _propTypes2.default.bool
}, _temp));

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