<script setup>
import { ref } from 'vue'
import { toast } from 'bulma-toast'

const props = defineProps({
  params: Object
})

const copied = ref(false)

const copyValue = async () => {
  try {
    await navigator.clipboard.writeText(props.params.data.value)
    copied.value = true
    setTimeout(() => { copied.value = false }, 1500)
    toast({
      message: `Copied: ${props.params.data.value}`,
      type: 'is-success',
      duration: 2000,
      position: 'top-right'
    })
  } catch (e) {
    console.error('Copy failed:', e)
    toast({
      message: 'Failed to copy value',
      type: 'is-danger',
      duration: 2000,
      position: 'top-right'
    })
  }
}
</script>

<template>
  <div class="d-inlineFlex items-center gap-5px">
    <div class="tags">
      <nuxt-link :to="`/materials/products/${props.params.data.id}/edit`"> {{ props.params.data.value }} </nuxt-link>
      <button class="copy-btn" @click.prevent="copyValue" :title="copied ? 'Copied!' : 'Copy value'">
        <UIcon :name="copied ? 'i-heroicons-check' : 'i-heroicons-clipboard-document'" class="copy-icon" />
      </button>
    </div>
  </div>
</template>

<style scoped>
.copy-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 1px 3px;
  border-radius: 3px;
  display: inline-flex;
  align-items: center;
  opacity: 0.5;
  transition: opacity 0.2s;
}

.copy-btn:hover {
  opacity: 1;
}

.copy-icon {
  width: 14px;
  height: 14px;
  color: var(--theme-text-muted, #666);
}

.tag {
  width: 40px;
  height: 20px;
}
.greenbt {
color: white;
  background-color: #00a500;
border: 1px solid #00a500;
}

.greybt {
  color: grey;
  border: 1px solid grey;
}

.icons {
  color: white;
}

span[class^='i-mdi'] {
  font-size: 19px;
  margin-top: 4px;
}
</style>