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

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

const clientName = ref('')
onMounted(() => {
  const getClient = localStorage.getItem('logship_clients')
  if(getClient) {
    const clientObj = JSON.parse(getClient)
    clientName.value = clientObj[0].name
  }
})

const form = ref({
  id: '',
  uid: '',
  client: '',
  clientId: '',
  organization: '',
  organizationId: '',
  name: '',
  value: '',
  description: '',
  documentNote: '',
  afterDelivery: false,
  netDays: 30,
  netDay: '',
  graceDays: 0,
  isActive: true,
  isDefault: false,
  isValid: false,
  isDueFixed: false,
  fixMonthCutoff: 0,
  fixMonthDay: 0,
  fixMonthOffset: 0,
  isNextBusinessDay: false,
  discount: 2,
  discount2: 0,
  discountDays: 10,
  discountDays2: 0,
  paymentTermUsage: 'Both',
  paymentTermUsageId: 'B',
  processing: false,
  alert: {
    status: '',
    message: '',
    processing: false
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/partners/payment-terms/'+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 || '',
      isActive: res?.IsActive ?? true,
      name: res?.Name || '',
      value: res?.Value || '',
      description: res?.Description || '',
      documentNote: res?.DocumentNote || '',
      afterDelivery: res?.AfterDelivery ?? false,
      netDays: res?.NetDays || 30,
      netDay: res?.NetDay || '',
      graceDays: res?.GraceDays || 0,
      isDefault: res?.IsDefault ?? false,
      isValid: res?.IsValid ?? false,
      isDueFixed: res?.IsDueFixed ?? false,
      fixMonthCutoff: res?.FixMonthCutoff || 0,
      fixMonthDay: res?.FixMonthDay || 0,
      fixMonthOffset: res?.FixMonthOffset || 0,
      isNextBusinessDay: res?.IsNextBusinessDay ?? false,
      discount: res?.Discount || 2,
      discount2: res?.Discount2 || 0,
      discountDays: res?.DiscountDays || 10,
      discountDays2: res?.DiscountDays2 || 0,
      paymentTermUsage: res?.PaymentTermUsage?.identifier || '',
      paymentTermUsageId: res?.PaymentTermUsage?.id || '',
      processing: false,
      alert: {
        status: '',
        message: '',
        processing: false
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/partners/payment-terms')
}

const breadCrumb = computed(() => ([
  { link: '/partners/partners', text: 'Partners'},
  { link: '/partners/payment-terms', text: 'Payment Terms'},
  { link: '#', text: 'View Payment Term Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Payment Term 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>Document Note</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.documentNote }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Grace Days</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.graceDays }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Net Days</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.netDays }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Net Day</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.netDay }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Fix Month Cutoff</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.fixMonthCutoff }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Fix Month Day</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.fixMonthDay }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Fix Month Offset</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.fixMonthOffset }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-180px d-flex justify-between gap-10px">
                <strong>Payment Term Usage</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.paymentTermUsage }}</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</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.discount }}</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 2</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.discount2 }}</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 Days</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.discountDays }}</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 Days 2</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.discountDays2 }}</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.afterDelivery" class="checkInput" id="afterDelivery" :checked="form.afterDelivery" disabled>
                <label class="checkLabel" for="afterDelivery">After Delivery</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isDueFixed" class="checkInput" id="isDueFixed" :checked="form.isDueFixed" disabled>
                <label class="checkLabel" for="isDueFixed">Due Fixed</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isNextBusinessDay" class="checkInput" id="isNextBusinessDay" :checked="form.isNextBusinessDay" disabled>
                <label class="checkLabel" for="isNextBusinessDay">Next Business Day</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isValid" class="checkInput" id="isValid" :checked="form.isValid" disabled>
                <label class="checkLabel" for="isValid">Valid</label>
              </div>
            </div>

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

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