<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,
  isSOPriceList: false,
  enforcePriceLimit: false,
  isTaxIncluded: false,
  isDefault: false,
  pricePrecision: 2,
  isPresentForProduct: false,
  isMandatory: false,
  currency: '',
  currencyId: '',
  basePriceList: '',
  basePriceListId: '',
  processing: false,
  alert: {
    status: '',
    message: '',
    processing: false
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/materials/price-lists/'+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,
      isSOPriceList: res?.IsSOPriceList ?? false,
      enforcePriceLimit: res?.EnforcePriceLimit ?? false,
      isTaxIncluded: res?.IsTaxIncluded ?? false,
      isDefault: res?.IsDefault ?? false,
      pricePrecision: res?.PricePrecision || 2,
      isPresentForProduct: res?.isPresentForProduct ?? false,
      isMandatory: res?.IsMandatory ?? false,
      currency: res?.C_Currency_ID?.identifier || '',
      currencyId: res?.C_Currency_ID?.id || '',
      basePriceList: res?.BasePriceList_ID?.identifier || '',
      basePriceListId: res?.BasePriceList_ID?.id || '',
      processing: false,
      alert: {
        status: '',
        message: '',
        processing: false
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/materials/price-lists')
}

const breadCrumb = computed(() => ([
  { link: '#', text: 'Materials'},
  { link: '/materials/price-lists', text: 'PriceLists'},
  { link: '#', text: 'View PriceList Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View PriceList 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>Price Precision</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.pricePrecision }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Currency</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.currency }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Base Price List</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.basePriceList }}</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.isDefault" class="checkInput" id="isDefault" :checked="form.isDefault" disabled>
                <label class="checkLabel" for="isDefault">Default</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isSOPriceList" class="checkInput" id="isSOPriceList" :checked="form.isSOPriceList" disabled>
                <label class="checkLabel" for="isSOPriceList">Sales Price list</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.enforcePriceLimit" class="checkInput" id="enforcePriceLimit" :checked="form.enforcePriceLimit" disabled>
                <label class="checkLabel" for="enforcePriceLimit">Enforce Price Limit</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isTaxIncluded" class="checkInput" id="isTaxIncluded" :checked="form.isTaxIncluded" disabled>
                <label class="checkLabel" for="isTaxIncluded">Price includes Tax</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isPresentForProduct" class="checkInput" id="isPresentForProduct" :checked="form.isPresentForProduct" disabled>
                <label class="checkLabel" for="isPresentForProduct">Present for Product</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isMandatory" class="checkInput" id="isMandatory" :checked="form.isMandatory" disabled>
                <label class="checkLabel" for="isMandatory">Mandatory</label>
              </div>
            </div>

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

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