<template>
  <button
    class="el-button"
    @click="handleClick"
    :disabled="buttonDisabled || loading"
    :autofocus="autofocus"
    :type="nativeType"
    :class="[
      type ? 'el-button--' + type : '',
      buttonSize ? 'el-button--' + buttonSize : '',
      {
        'is-disabled': buttonDisabled || loading,
        'is-icon': icon && !$slots.default,
        'is-loading': loading,
        'el-button--icon': icon
      }
    ]"
  >
    <!-- <i class="el-icon-loading" v-if="loading"></i> -->
    <!-- <span>{{type}}</span> -->
    <img class="loading-icon" :class="{'text-icon': !$slots.default}" v-if="loading && iconPoi !== 'right'" :src="require(`examples/assets/images/new-loading-${type}.png`)" alt="">
    <i :class="icon" v-if="icon && iconPoi === 'left' && !loading"></i>
    <!-- 如果有$slots.default 那么不是icon按钮 -->
    <span v-if="$slots.default"><slot></slot></span>
    <i :class="icon" v-if="icon && iconPoi === 'right' && !loading"></i>
    <img class="loading-icon loading-right-icon" :class="{'text-icon': !$slots.default}" v-if="loading && iconPoi === 'right'" :src="require(`examples/assets/images/new-loading-${type}.png`)" alt="">
  </button>
</template>
<script>
  export default {
    name: 'ElButton',
    inject: {
      elForm: {
        default: ''
      },
      elFormItem: {
        default: ''
      }
    },
    props: {
      type: {
        type: String,
        default: 'primary'
      },
      size: {
        type: String,
        default: 'large'
      },
      icon: {
        type: String,
        default: ''
      },
      nativeType: {
        type: String,
        default: 'button'
      },
      iconPoi: {
        type: String,
        default: 'left'
      },
      loading: Boolean,
      disabled: Boolean,
      autofocus: Boolean
    },
    computed: {
      _elFormItemSize() {
        return (this.elFormItem || {}).elFormItemSize;
      },
      buttonSize() {
        return this.size || this._elFormItemSize || (this.$SCRMEMENT || {}).size;
      },
      buttonDisabled() {
        return this.$options.propsData.hasOwnProperty('disabled') ? this.disabled : (this.elForm || {}).disabled;
      }
    },
    methods: {
      handleClick(evt) {
        this.$emit('click', evt);
      }
    }
  };
</script>
