<script setup>
const props = defineProps({
  params: Object
})

const attachmentQty = ref(0)

const isTargetShipper = computed(() => {
  const shipperId = props?.params?.data?.shipperId
  if (!shipperId) return false
  return ['1000002', '1000005', '1000006'].includes(String(shipperId))
})

const getAttachment = async () => {
  attachmentQty.value = 0
  const getAttachmentRecord = await $fetch(`/api/attachments/C_Order/${props.params.data.id}`)
  if(getAttachmentRecord?.Strapi_Attachment_ID && getAttachmentRecord?.Strapi_Attachment_documentId) {
    const res = await $fetch(`/api/attachments/C_Order/${getAttachmentRecord.Strapi_Attachment_documentId}/files`, {
      headers: useRequestHeaders(['cookie'])
    })
    if(res) {
      attachmentQty.value = res?.length ?? 0
    }
  }
}

onMounted(getAttachment)
watch(() => props.params?.data?.id, getAttachment)
</script>

<template>
  <div class="d-inlineFlex items-center gap-5px">
    <div class="tags are-small">
      <span v-if="!isTargetShipper && attachmentQty > 0" class="tag is-rounded" style="margin-top: 3px; height: 19px; font-weight: bold; background-color: #d3d3d3 !important; color: #696969 !important;">
        <i style="font-size: 14px; color: #696969; margin-right: 5px;" class="ti ti-pdf"></i>{{ attachmentQty }} Files
      </span>
      <span v-if="isTargetShipper && attachmentQty > 0" class="tag is-rounded" style="margin-top: 3px; height: 19px; font-weight: bold; background-color: #d4f8d4 !important; color: #2e7d32 !important;">
        <i style="font-size: 14px; color: #2e7d32; margin-right: 5px;" class="ti ti-check"></i>Label
      </span>
      <span v-if="isTargetShipper && attachmentQty === 0" class="tag is-rounded" style="margin-top: 3px; height: 19px; font-weight: bold; background-color: #ffb6c1 !important; color: #db7093 !important;">
        <i style="font-size: 14px; color: #db7093; margin-right: 5px;" class="ti ti-ban"></i>Label
      </span>
    </div>
  </div>
</template>

<style scoped>
.tag {
  width: 40px;
  height: 20px;
}

.tag-base {
  margin-top: 3px;
  height: 19px;
  font-weight: bold;
}

.tag-file {
  background-color: lightgrey;
  color: dimgrey;
}

.tag-label-success {
  background-color: #d4f8d4 !important;
  color: #2e7d32 !important;
}

.tag-label-error {
  background-color: #ffcdd2 !important;
  color: #c62828 !important;
}

.tag-icon {
  font-size: 14px;
  margin-right: 5px;
}

.tag-file .tag-icon {
  color: dimgrey;
}

.tag-label-success .tag-icon {
  color: #2e7d32;
}

.tag-label-error .tag-icon {
  color: palevioletred;
}
</style>