<template>
  <div
    class="vux-checker-item"
    :class="classNames"
    @click="select"
  >
    <slot />
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: [String, Number, Boolean],
      required: true
    },
    disabled: Boolean
  },
  computed: {
    classNames () {
      const names = {
        'vux-tap-active': !this.disabled
      }
      if (this.$parent.defaultItemClass) {
        names[this.$parent.defaultItemClass] = true
      }
      if (this.$parent.selectedItemClass) {
        names[this.$parent.selectedItemClass] = this.$parent.type === 'radio' ? (this.$parent.currentValue === this.value) : (this.$parent.currentValue.indexOf(this.value) > -1)
      }
      if (this.$parent.disabledItemClass) {
        names[this.$parent.disabledItemClass] = this.disabled
      }
      return names
    }
  },
  methods: {
    select () {
      if (this.$parent.type === 'radio') {
        this.selectRadio()
      } else {
        this.selectCheckbox()
      }
    },
    selectRadio () {
      if (!this.disabled) {
        this.$set(this.$parent, 'currentValue', this.value)
        this.$emit('on-item-click', this.value, this.disabled)
      }
    },
    selectCheckbox () {
      if (!this.disabled) {
        const index = this.$parent.currentValue.indexOf(this.value)
        if (index > -1) {
          this.$parent.value.splice(index, 1)
        } else {
          if (!this.$parent.max || (this.$parent.max && this.$parent.currentValue.length < this.$parent.max)) {
            this.$parent.currentValue.push(this.value)
          }
        }
        this.$emit('on-item-click', this.value, this.disabled)
      }
    }
  }
}
</script>
