// packageI/parkCouponFree/components/car-number-input/car-number-input.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    numbers: [{
      value: '',
      focus: true
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false,
      eco: true
    }]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    click(e) {
      const _index = e.target.dataset.index;
      const _nums = this.data.numbers;
      _nums.forEach((item, index) => {
        item.focus = index == _index;
      });
      this.setData({
        numbers: _nums
      });
      this.triggerEvent('focus', _index);
    },

    /**
     * {
     *    index: Number,
     *    value: String,
     *    remove: Boolean
     * }
     */
    change(data) {
      const {
        index,
        value,
        remove
      } = data;
      const _nums = this.data.numbers;
      _nums[index].value = remove ? '' : value;

      const len = _nums.length;
      // let nextIndex = remove ? index - 1 : index + 1;
      let nextIndex = remove ? index : index + 1;
      if (remove && nextIndex < 0) {
        nextIndex = len - 1;
      }
      if (!remove && nextIndex >= len)
        nextIndex = 0;

      _nums.forEach((item, idx) => {
        item.focus = idx == nextIndex;
      });

      this.setData({
        numbers: _nums
      });
      this.triggerEvent('focus', nextIndex);
    }
  }
})