import lifecycle from '../mixins/lifecycle';

Component({
  mixins: [lifecycle],
  data: {
    confirmStyle: ''
  },

  props: {
    className: '',
    item: '',
    id: '',
    value: '',
    selected: false,
    onChange: function onChange() {}
  },

  didMount: function didMount() {
    var _data = this.data,
        results = _data.results,
        items = _data.items;
    var _props = this.props,
        selected = _props.selected,
        id = _props.id,
        value = _props.value;

    if (selected) {
      results.push({ id: id, value: value });
      items.push(this);
      this.setData({
        confirmStyle: true
      });
    }
  },


  methods: {
    handleClick: function handleClick() {
      var _props2 = this.props,
          id = _props2.id,
          value = _props2.value,
          onChange = _props2.onChange;
      var confirmStyle = this.data.confirmStyle;
      var _data2 = this.data,
          results = _data2.results,
          items = _data2.items,
          commonProps = _data2.commonProps;

      if (commonProps.max === 1) {
        if (confirmStyle === '') {
          items.forEach(function (element) {
            element.setData({
              confirmStyle: ''
            });
          });
          results.splice(0, results.length);
          confirmStyle = true;
          results.push({ id: id, value: value });
          items.push(this);
          onChange(results);
        }
        this.setData({
          confirmStyle: confirmStyle
        });
        return;
      }
      if (confirmStyle === '' && results.length < commonProps.max) {
        confirmStyle = true;
        results.push({ id: id, value: value });
        items.push(this);
      } else {
        confirmStyle = '';
        results.some(function (key, index) {
          if (JSON.stringify(key) === JSON.stringify({ id: id, value: value })) {
            results.splice(index, 1);
            return true;
          } else {
            return false;
          }
        });
      }
      this.setData({
        confirmStyle: confirmStyle
      });
    }
  }
});