<template>
  <ps-popper
    v-model:visible="visible"
    trigger="click"
    effect="light"
    popper-class="ps-popover"
    append-to-body
  >
    <div class="ps-popconfirm">
      <p class="ps-popconfirm__main">
        <i
          v-if="!hideIcon"
          :class="icon"
          class="ps-popconfirm__icon"
          :style="{color: iconColor}"
        ></i>
        {{ title }}
      </p>
      <div class="ps-popconfirm__action">
        <ps-button
          size="mini"
          :type="cancelButtonType"
          @click="cancel"
        >
          {{ cancelButtonText_ }}
        </ps-button>
        <ps-button
          size="mini"
          :type="confirmButtonType"
          @click="confirm"
        >
          {{ confirmButtonText_ }}
        </ps-button>
      </div>
    </div>
    <template #trigger>
      <slot name="reference"></slot>
    </template>
  </ps-popper>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue'
import PsButton from '@present-ui/button'
import PsPopper from '@present-ui/popper'
import { t } from '@present-ui/locale'

export default defineComponent({
  name: 'PsPopconfirm',

  components: {
    PsButton,
    PsPopper,
  },

  props: {
    title: {
      type: String,
    },
    confirmButtonText: {
      type: String,
    },
    cancelButtonText: {
      type: String,
    },
    confirmButtonType: {
      type: String,
      default: 'primary',
    },
    cancelButtonType: {
      type: String,
      default: 'text',
    },
    icon: {
      type: String,
      default: 'ps-icon-question',
    },
    iconColor: {
      type: String,
      default: '#f90',
    },
    hideIcon: {
      type: Boolean,
      default: false,
    },
  },
  emits:['confirm','cancel'],
  setup(props,{ emit }){
    const visible = ref(false)
    const confirm = () => {
      visible.value = false
      emit('confirm')
    }
    const cancel = () => {
      visible.value = false
      emit('cancel')
    }
    const confirmButtonText_ = computed(() => {
      return props.confirmButtonText || t('ps.popconfirm.confirmButtonText')
    })
    const cancelButtonText_ = computed(() => {
      return props.cancelButtonText || t('ps.popconfirm.cancelButtonText')
    })
    return {
      visible,
      confirm,
      cancel,
      confirmButtonText_,
      cancelButtonText_,
    }
  },
})
</script>
