<script setup>
import { date } from 'alga-js'
const router = useRouter()
const route = useRoute()

definePageMeta({
  middleware: ['auth', 'org']
})

const clients = useCookie('logship_clients')
const client = useCookie('logship_client')
const clientId = useCookie('logship_client_id')

const clientName = computed(() => {
  return client.value?.Name || clients.value?.filter(i => i.id == clientId.value)?.[0]?.name || ''
})

const form = ref({
  id: '',
  uid: '',
  client: '',
  clientId: '',
  organization: '',
  organizationId: '',
  name: '',
  description: '',
  shippingServiceCode: '',
  trackingURL: '',
  isActive: true,
  isInternational: false,
  isOnline: false,
  isResidential: false,
  isSaturdayDelivery: false,
  shippingProcessorCfg: '',
  shippingProcessorCfgId: '',
  processing: false,
  alert: {
    status: '',
    message: '',
    processing: false
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/materials/promotion-groups/'+route.params.id, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    form.value = {
      id: route.params.id,
      uid: res?.uid || '',
      client: res?.AD_Client_ID?.identifier || '',
      clientId: res?.AD_Client_ID?.id || '',
      organization: res?.AD_Org_ID?.identifier || '',
      organizationId: res?.AD_Org_ID?.id || '',
      name: res?.Name || '',
      description: res?.Description || '',
      shippingServiceCode: res?.ShippingServiceCode || '',
      trackingURL: res?.TrackingURL || '',
      isActive: res?.IsActive ?? true,
      isInternational: res?.IsInternational ?? false,
      isOnline: res?.IsOnline ?? false,
      isResidential: res?.IsResidential ?? false,
      isSaturdayDelivery: res?.IsSaturdayDelivery ?? false,
      shippingProcessorCfg: res?.M_ShippingProcessorCfg_ID?.identifier || '',
      shippingProcessorCfgId: res?.M_ShippingProcessorCfg_ID?.id || '',
      processing: false,
      alert: {
        status: '',
        message: '',
        processing: false
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/materials/promotion-groups')
}

const breadCrumb = computed(() => ([
  { link: '#', text: 'Materials'},
  { link: '/materials/promotion-groups', text: 'Promotion Groups'},
  { link: '#', text: 'View Promotion Group Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Promotion Group Details</Title>
  </Head>

  <NuxtLayout name="new-dash">
    <NewBreadCrumb :items="breadCrumb" />
    <div>
      <div class="banner">
        <div class="d-flex justify-between mb-3">
          <h4></h4>
          <div class="d-flex gap-0.75rem">
            <button type="button" @click="back" class="button">
              <span class="tf-icon ti ti-chevron-left ti-xs me-1 color-inherit"></span> Back
            </button>
          </div>
        </div>
        <div>
          <div class="columns is-gapless is-multiline mb-3 mt-0">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Tenant</strong>
                <strong>:</strong>
              </div>
              <div>{{ clientName }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Organization</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.organization }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Name</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.name }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Search Key</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.value }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Description</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.description }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Service Code</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.shippingServiceCode }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Tracking URL</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.trackingURL }}</div>
            </div>
            <div class="column is-full mb-3">
              <label class="label d-block mb-2 fw-bold">Status :</label>
              <div class="check">
                <input type="checkbox" v-model="form.isActive" class="checkInput" id="isActive" :checked="form.isActive" disabled>
                <label class="checkLabel" for="isActive">Active</label>
              </div>
            </div>

          </div>
        </div>
      </div>

    </div>
  </NuxtLayout>
</template>