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

const entries = ref([])
const retrieve = async () => {
  if(props.info?.id) {
    const res = await $fetch('/api/materials/products/'+props.info.id+'/related', {
      headers: useRequestHeaders(['cookie'])
    })
    if(res) {
      entries.value = res.records.map(item => {
        return {
          id: item?.id || item?.uid || '',
          client: item?.AD_Client_ID?.identifier || '',
          clientId: item?.AD_Client_ID?.id || '',
          organization: item?.AD_Org_ID?.identifier || '',
          organizationId: item?.AD_Org_ID?.id || '',
          isActive: item?.IsActive ?? true,
          name: item?.Name || '',
          description: item?.Description || '',
          product: item?.M_Product_ID?.identifier || item?.M_Product_ID?.Name || '',
          productId: item?.M_Product_ID?.id || '',
          relatedProduct: item?.RelatedProduct_ID?.identifier || '',
          relatedProductId: item?.RelatedProduct_ID?.id || '',
          relatedProductType: item?.RelatedProductType?.identifier || '',
          relatedProductTypeId: item?.RelatedProductType?.id || '',
          uom: item?.C_UOM_ID?.identifier || '',
          uomId: item?.C_UOM_ID?.id || '',
          quantity: item?.Quantity || 1,
          alert: {
            status: '',
            message: '',
            processing: false,
          }
        }
      })
    }
  }
}
onMounted(retrieve)
watch(() => props.info, () => {
  retrieve()
})
</script>

<template>
  <div class="detailRecordResponsive">
    <div class="tableResponsive">
      <table class="table tableList tableFlush">
        <thead>
          <tr> 
            <th>Name</th>
            <th>Related Product</th>
            <th>Related Product Type</th>
            <th>Description</th>
          </tr>
        </thead>
        <tbody>
          <tr v-if="Number(entries.length) === 0">
            <td colspan="4" class="align-center">No record found.</td>
          </tr>
          <tr v-for="(entry, index) in entries" :key="index">
            <td>{{ entry.name }}</td>
            <td>{{ entry.relatedProduct }}</td>
            <td>{{ entry.relatedProductType }}</td>
            <td>{{ entry.description }}</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>