<script setup>
import Modal from "@/components/Modal.vue";

const props = defineProps({
  variant: Object,
});

const { $deleteapi } = useNuxtApp();
const showConfirmModal = ref(null);

function displayModal() {
  showConfirmModal.value = {
    component: "dialog/Confirm",
    title: "Xoá phiên bản",
    width: "500px",
    height: "auto",
    vbind: {
      content: `Bạn xác nhận xoá phiên bản <code>${props.variant.code}</code>?`,
      onModalevent: deleteVariant,
    },
  };
}

const refreshData = inject("refreshData");
async function deleteVariant() {
  const res = await $deleteapi("Product_Variant", props.variant.id);
  if (res !== "error") {
    if (refreshData) refreshData();
  }
}
</script>

<template>
  <a
    class="has-text-danger"
    @click="displayModal"
  >
    <span class="icon">
      <Icon
        name="material-symbols:delete-outline-rounded"
        :size="18"
      />
    </span>
    <Modal
      v-bind="showConfirmModal"
      @close="showConfirmModal = null"
    />
  </a>
</template>
