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

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

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

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

const form = ref({
  id: '',
  uid: '',
  client: '',
  clientId: '',
  organization: '',
  organizationId: '',
  name: '',
  description: '',
  isActive: true,
  validFrom: date.now('date'),
  isQuantityBased: false,
  isBPartnerFlatDiscount: false,
  flatDiscount: 0,
  cumulativeLevel: 'Line',
  cumulativeLevelId: 'L',
  discountType: 'Breaks',
  discountTypeId: 'B',
  processing: false,
  alert: {
    status: '',
    message: '',
    processing: false
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/materials/discount-schemas/'+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 || '',
      isActive: res?.IsActive ?? true,
      validFrom: res?.ValidFrom || date.now('date'),
      isQuantityBased: res?.IsQuantityBased ?? false,
      isBPartnerFlatDiscount: res?.IsBPartnerFlatDiscount ?? false,
      flatDiscount: res?.FlatDiscount || 0,
      cumulativeLevel: res?.CumulativeLevel?.identifier || 'Line',
      cumulativeLevelId: res?.CumulativeLevel?.id || 'L',
      discountType: res?.DiscountType?.identifier || 'Breaks',
      discountTypeId: res?.DiscountType?.id || 'B',
      processing: false,
      alert: {
        status: '',
        message: '',
        processing: false
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/materials/discount-schemas')
}

const breadCrumb = computed(() => ([
  { link: '#', text: 'Materials'},
  { link: '/materials/discount-schemas', text: 'Discount Schemas'},
  { link: '#', text: 'View Discount Schema Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Discount Schema 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>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>Valid From</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.validFrom }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Flat Discount</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.flatDiscount }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Cumulative Level</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.cumulativeLevel }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Discount Type</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.discountType }}</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 class="check">
                <input type="checkbox" v-model="form.isQuantityBased" class="checkInput" id="isQuantityBased" :checked="form.isQuantityBased" disabled>
                <label class="checkLabel" for="isQuantityBased">Quantity Based</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isBPartnerFlatDiscount" class="checkInput" id="isBPartnerFlatDiscount" :checked="form.isBPartnerFlatDiscount" disabled>
                <label class="checkLabel" for="isBPartnerFlatDiscount">Business Partner Flat Discount</label>
              </div>
            </div>

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

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