<template>
  <span class="ipub-label flex-center"
        @click="handleClick"
        @mouseover="handleMouseover"
        :class="[
          type ? 'ipub-label--' + type : '',
          {
            'is-round': round
          },
          cover ? 'ipub-label--cover' : '',
          hover ? 'ipub-label--hover' : '',
          active ? 'ipub-label--active' : '',
          circle ? 'ipub-label--circle' : '',
          size ? 'ipub-label--' + size : ''
        ]">
    <IpubIcon v-if="icon"
              class="ipub-label__icon"
              :type="icon" />
    <slot></slot>
    <IpubIcon v-if="closable"
              class="ipub-label__action"
              type="iconClose"
              @click.stop="handleClose" />
    <IpubIcon v-if="addable"
              class="ipub-label__action"
              type="iconAdd"
              @click.stop="handleAdd" />
  </span>
</template>

<script>
import IpubIcon from '../../icon'
export default {
  name: 'IpubLabel',
  components: {
    IpubIcon
  },
  props: {
    type: {
      type: String,
      default: 'default',
      validator (val) {
        return ['theme', 'link', 'success', 'warning', 'danger', 'default'].indexOf(val) !== -1
      }
    },
    size: {
      type: String,
      default: 'common',
      validator (val) {
        return ['small', 'common', 'large'].indexOf(val) !== -1
      }
    },
    icon: {
      type: String,
      default: ''
    },
    round: Boolean,
    closable: Boolean,
    addable: Boolean,
    hover: Boolean,
    cover: Boolean,
    active: Boolean,
    circle: Boolean
  },
  methods: {
    handleClose (event) {
      this.$emit('close', event);
    },
    handleAdd (event) {
      this.$emit('add', event);
    },
    handleClick (event) {
      this.$emit('click', event);
    },
    handleMouseover (event) {
      this.$emit('mouseover', event);
    }
  }
}
</script>

<style lang="scss" scoped>
@mixin label-type($color) {
  color: $color;
  &.ipub-label--cover {
    background: rgba($color, 0.2);
    color: $main_color;
    border-color: transparent;
  }
  &.ipub-label--hover:hover {
    background: $color;
    color: #fff;
    border-color: $color;
  }

  &.ipub-label--active {
    cursor: pointer;
    color: #fff;
    border-color: $color;
    background: $color;
  }
}
.ipub-label {
  display: inline-flex !important;
  vertical-align: middle;
  color: $main_color;
  padding: 0 10px;
  border: 1px solid;
  border-radius: 4px;
  font-size: 12px;
  box-sizing: border-box;
  white-space: nowrap;
  transition: all 0.3s;
  // & + & {
  //   margin-left: 10px;
  // }
  &--default {
    @include label-type($logo_color);
  }
  &--warning {
    @include label-type($warning_color);
  }
  &--success {
    @include label-type($success_color);
  }
  &--link {
    @include label-type($link_color);
  }
  &--danger {
    @include label-type($key_color);
  }
  &--theme {
    @include label-type($theme_color);
  }
  &.is-round {
    border-radius: 12px;
  }
  &__icon {
    margin-right: 5px;
    vertical-align: middle;
  }
  &__action {
    vertical-align: middle;
    margin-left: 5px;
    cursor: pointer;
  }
  &--hover:hover {
    cursor: pointer;
    color: $theme_color;
    border-color: $theme_color;
  }
  &--small {
    height: 20px;
    line-height: 20px;
    &.ipub-label--circle {
      border-radius: 10px;
      padding: 0 16px;
    }
  }
  &--common {
    height: 30px;
    line-height: 30px;
    &.ipub-label--circle {
      border-radius: 15px;
      padding: 0 14px;
      font-size: 14px;
    }
  }
  &--large {
    height: 40px;
    line-height: 40px;
    &.ipub-label--circle {
      border-radius: 20px;
      padding: 0 20px;
      font-size: 14px;
    }
  }
}
</style>
