<template>
  <div :class="align">
    <el-button
      v-for="item of option"
      :id="item.id || null"
      :loading.sync="item.loading"
      :class="item.align"
      :key="item.name"
      :icon="item.icon"
      :size="item.size"
      :type="item.type"
      :disabled.sync="item.disabled"
      :round="item.round"
      @click.stop="doAction($event, item.action, item)"
      >{{ generateButtonText(item) }}</el-button
    >
  </div>
</template>

<script>
import { ft } from '../../../lib/utils';

const LABELS = {
  new: ft('i18n_common_new', '新增')
};

export default {
  name: 'ButtonGroup',
  inheritAttrs: false,
  props: {
    align: {
      type: String,
      default: 'btns-right'
    },
    option: {
      type: Array,
      default: () => {
        return [
          {
            action: 'item-add',
            align: 'btn-right',
            disabled: false,
            icon: 'el-icon-plus',
            label: LABELS.new,
            loading: false,
            name: 'btn-add',
            round: false,
            size: 'small',
            type: 'primary'
          }
        ];
      }
    }
  },
  methods: {
    doAction($event, action, data) {
      this.$emit(action, data);
      this.$emit('do-action', action);
      this.$emit('do-event-action', $event, action);
    },

    generateButtonText(item) {
      return item.i18nKey
        ? this.$ft(item.i18nKey, item.label)
        : item.label.includes('i18n_')
        ? this.$t(item.label)
        : item.label;
    }
  }
};
</script>

<style scoped>
.btns-right .btn-right,
.btns-left .btn-right {
  float: right;
  margin-left: 10px;
}
.btns-right .btn-left,
.btns-left .btn-left {
  float: left;
  margin-right: 10px;
}
.btns-left {
  text-align: left;
}
.btns-right {
  text-align: right;
}
.btns-center {
  text-align: center;
}
</style>
