<template>
  <button
    :type="nativeType"
    class="co-button"
    :class="['co-button--' + type, 'co-button--' + size, {
      'is-disabled': disabled,
      'is-plain': plain
    }]"
    @click="handleClick"
    :disabled="disabled">
    <span class="co-button-icon" v-if="icon || $slots.icon">
      <slot name="icon">
        <i v-if="icon" class="coicon" :class="'coicon-' + icon"></i>
      </slot>
    </span>
    <label class="co-button-text"><slot></slot></label>
  </button>
</template>

<script>
if (process.env.NODE_ENV === 'component') {
  require('CoUI/packages/font/style.css');
}

/**
 * co-header
 * @module components/button
 * @desc 按钮
 * @param {string} [type=default] - 显示类型，接受 default, primary, danger
 * @param {boolean} [disabled=false] - 禁用
 * @param {boolean} [plain=false] - 幽灵按钮
 * @param {string} [size=normal] - 尺寸，接受 normal, small, large
 * @param {string} [native-type] - 原生 type 属性
 * @param {string} [icon] - 图标，提供 more, back，或者自定义的图标（传入不带前缀的图标类名，最后拼接成 .coicon-xxx）
 * @param {slot} - 显示文本
 * @param {slot} [icon] 显示图标
 *
 * @example
 * <co-button size="large" icon="back" type="primary">按钮</co-button>
 */
export default {
  name: 'co-button',

  methods: {
    handleClick(evt) {
      this.$emit('click', evt);
    }
  },

  props: {
    icon: String,
    disabled: Boolean,
    nativeType: String,
    plain: Boolean,
    type: {
      type: String,
      default: 'default',
      validator(value) {
        return [
          'default',
          'danger',
          'primary',
          'success',
          'warning'
        ].indexOf(value) > -1;
      }
    },
    size: {
      type: String,
      default: 'normal',
      validator(value) {
        return [
          'small',
          'normal',
          'large'
        ].indexOf(value) > -1;
      }
    }
  }
};
</script>

<style lang="css">
  @import "../../../src/style/var-ios9.css";

  @component-namespace co {
    @component button {
      appearance: none;
      border-radius: 0.2rem;
      border: 0;
      box-sizing: border-box;
      color: inherit;
      display: block;
      font-size: 0.7rem;
      height: 1.35rem;
      outline: 0;
      overflow: hidden;
      position: relative;
      text-align: center;

      &::after {
        background-color: #000;
        content: " ";
        opacity: 0;
        position: absolute 0 0 0 0;
      }

      &:not(.is-disabled):active::after {
        opacity: .4;
      }

      @descendent icon {
        vertical-align: middle;
        display: inline-block;
      }

      @modifier default {
        color: $button-default-color;
        background-color: $button-default-background-color;
        box-shadow: $button-default-box-shadow;

        @when plain {
          border: 0.05rem solid $button-default-plain-color;
          background-color: transparent;
          box-shadow: none;
          color: $button-default-plain-color;
        }
      }

      @modifier primary {
        color: $button-primary-color;
        background-color: $button-primary-background-color;

        @when plain {
          border: 0.05rem solid $button-primary-background-color;
          background-color: transparent;
          color: $button-primary-background-color;
        }
      }

      @modifier danger {
        color: $button-danger-color;
        background-color: $button-danger-background-color;

        @when plain {
          border: 0.05rem solid $button-danger-background-color;
          background-color: transparent;
          color: $button-danger-background-color;
        }
      }

      @modifier warning {
        color: $button-warning-color;
        background-color: $button-warning-background-color;

        @when plain {
          border: 0.05rem solid $button-warning-background-color;
          background-color: transparent;
          color: $button-warning-background-color;
        }
      }

      @modifier success {
        color: $button-success-color;
        background-color: $button-success-background-color;

        @when plain {
          border: 0.05rem solid $button-success-background-color;
          background-color: transparent;
          color: $button-success-background-color;
        }
      }

      @modifier large {
        display: block;
        width: 100%;
      }

      @modifier normal {
        display: inline-block;
        padding: 0 0.6rem;
      }

      @modifier small {
        display: inline-block;
        font-size: 0.7rem;
        padding: 0 0.6rem;
        height: 1.65rem;
      }

      @when disabled {
        opacity: .6;
      }
    }
  }
</style>
