<template>
  <label
    class="input-wrap"
    :class="[
      border ? 'input-tag' : 'input-radio',
      radioSize ? 'input-wrap-' + radioSize : '',
      { 'is-disabled': isDisabled },
      { 'is-focus': focus },
      { 'is-bordered': border },
      { 'is-checked': model === label }
    ]"
    role="radio"
    :aria-checked="model === label"
    :aria-disabled="isDisabled"
    :tabindex="tabIndex"
    @keydown.space.stop.prevent="model = label"
  >
      <input
        :value="label"
        type="radio"
        v-model="model"
        @focus="focus = true"
        @blur="focus = false"
        @change="handleChange"
        :name="name"
        :disabled="isDisabled"
        tabindex="-1"
      >
    <span class="input-ctnr"
      :class="[isCut ? 'cut' : '']"
      :style="isCut ? 'width: ' + isCut + 'px;' : ''"
    >
      <slot v-if="!!border"></slot>
    </span>
    <slot v-if="!!!border"></slot>
  </label>
</template>
<script>
  import Emitter from 'element-ui-focus/src/mixins/emitter';

  export default {
    name: 'focusRadio',

    mixins: [Emitter],

    inject: {
      elFormItem: {
        default: ''
      }
    },

    componentName: 'FocusRadio',

    props: {
      value: {},
      label: {},
      disabled: Boolean,
      name: String,
      border: Boolean,
      cut: String,
      size: String
    },

    data() {
      return {
        focus: false
      };
    },
    computed: {
      isGroup() {
        let parent = this.$parent;
        while (parent) {
          if (parent.$options.componentName !== 'FocusRadioGroup') {
            parent = parent.$parent;
          } else {
            this._radioGroup = parent;
            return true;
          }
        }
        return false;
      },
      model: {
        get() {
          return this.isGroup ? this._radioGroup.value : this.value;
        },
        set(val) {
          if (this.isGroup) {
            this.dispatch('FocusRadioGroup', 'input', [val]);
          } else {
            this.$emit('input', val);
          }
        }
      },
      _elFormItemSize() {
        return (this.elFormItem || {}).elFormItemSize;
      },
      radioSize() {
        const temRadioSize = this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
        return this.isGroup
          ? this._radioGroup.radioGroupSize || temRadioSize
          : temRadioSize;
      },
      isCut() {
        return this.isGroup
          ? this._radioGroup.cut || this.cut
          : this.cut;
      },
      isDisabled() {
        return this.isGroup
          ? this._radioGroup.disabled || this.disabled
          : this.disabled;
      },
      tabIndex() {
        return !this.isDisabled ? (this.isGroup ? (this.model === this.label ? 0 : -1) : 0) : -1;
      }
    },

    methods: {
      handleChange() {
        this.$nextTick(() => {
          this.$emit('change', this.model);
          this.isGroup && this.dispatch('FocusRadioGroup', 'handleChange', this.model);
        });
      }
    }
  };
</script>
