<template>
  <fl-popover
    v-bind="$attrs"
    v-model="visible"
    trigger="click"
  >
  <div class="fl-popconfirm">
    <p class="fl-popconfirm__main">
    <i
      v-if="!hideIcon"
      :class="icon"
      class="fl-popconfirm__icon"
      :style="{color: iconColor}"
    ></i>
      {{title}}
    </p>
    <div class="fl-popconfirm__action">
      <fl-button 
        size="mini" 
        :type="cancelButtonType" 
        @click="cancel"
      >
        {{ displayCancelButtonText }}
      </fl-button>
      <fl-button 
        size="mini" 
        :type="confirmButtonType" 
        @click="confirm"
      >
        {{ displayConfirmButtonText }}
      </fl-button>
    </div>
  </div>
  <slot name="reference" slot="reference"></slot>
</fl-popover>
</template>

<script>
import FlPopover from 'flames-ui/packages/popover';
import FlButton from 'flames-ui/packages/button';
import {t} from 'flames-ui/src/locale';

export default {
  name: 'FlPopconfirm',
  props: {
    title: {
      type: String
    },
    confirmButtonText: {
      type: String
    },
    cancelButtonText: {
      type: String
    },
    confirmButtonType: {
      type: String,
      default: 'primary'
    },
    cancelButtonType: {
      type: String,
      default: 'text'
    },
    icon: {
      type: String,
      default: 'fl-icon-question'
    },
    iconColor: {
      type: String,
      default: '#f90'
    },
    hideIcon: {
      type: Boolean,
      default: false
    }
  },
  components: {
    FlPopover,
    FlButton
  },
  data() {
    return {
      visible: false
    };
  },
  computed: {
    displayConfirmButtonText() {
      return this.confirmButtonText || t('el.popconfirm.confirmButtonText');
    },
    displayCancelButtonText() {
      return this.cancelButtonText || t('el.popconfirm.cancelButtonText');
    }
  },
  methods: {
    confirm() {
      this.visible = false;
      this.$emit('confirm');
    },
    cancel() {
      this.visible = false;
      this.$emit('cancel');
    }
  }
};
</script>
