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

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

const form = ref({
  id: '',
  client: '',
  clientId: '',
  organization: '',
  organizationId: '',
  isActive: true,
  name: '',
  description: '',
  isDocNoControlled: false,
  printName: '',
  hasCharges: false,
  hasProforma: false,
  isDefault: false,
  documentCopies: 1,
  isSOTrx: false,
  isDefaultCounterDoc: false,
  isPickQAConfirm: false,
  isShipConfirm: false,
  isInTransit: false,
  isSplitWhenDifference: false,
  isCreateCounter: false,
  isIndexed: false,
  isOverwriteSeqOnComplete: false,
  isOverwriteDateOnComplete: false,
  isPrepareSplitDocument: false,
  isChargeOrProductMandatory: false,
  isNoPriceListCheck: false,
  documentNote: '',
  printFormat: '',
  printFormatId: '',
  definiteSequence: '',
  definiteSequenceId: '',
  docNoSequence: '',
  docNoSequenceId: '',
  category: '',
  categoryId: '',
  docBaseType: '',
  docBaseTypeId: '',
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/settings/document-types/'+route.params.id, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    form.value = {
      id: route.params.id,
      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 || '',
      description: res?.Description || '',
      isDocNoControlled: res?.IsDocNoControlled ?? false,
      printName: res?.PrintName || '',
      hasCharges: res?.HasCharges ?? false,
      hasProforma: res?.HasProforma ?? false,
      isDefault: res?.IsDefault ?? false,
      documentCopies: res?.DocumentCopies || 1,
      isSOTrx: res?.IsSOTrx ?? false,
      isDefaultCounterDoc: res?.IsDefaultCounterDoc ?? false,
      isPickQAConfirm: res?.IsPickQAConfirm ?? false,
      isShipConfirm: res?.IsShipConfirm ?? false,
      isInTransit: res?.IsInTransit ?? false,
      isSplitWhenDifference: res?.IsSplitWhenDifference ?? false,
      isCreateCounter: res?.IsCreateCounter ?? false,
      isIndexed: res?.IsIndexed ?? false,
      isOverwriteSeqOnComplete: res?.IsOverwriteSeqOnComplete ?? false,
      isOverwriteDateOnComplete: res?.IsOverwriteDateOnComplete ?? false,
      isPrepareSplitDocument: res?.IsPrepareSplitDocument ?? false,
      isChargeOrProductMandatory: res?.IsChargeOrProductMandatory ?? false,
      isNoPriceListCheck: res?.IsNoPriceListCheck ?? false,
      documentNote: res?.DocumentNote || '',
      printFormat: res?.AD_PrintFormat_ID?.identifier || '',
      printFormatId: res?.AD_PrintFormat_ID?.id || '',
      definiteSequence: res?.DefiniteSequence_ID?.identifier || '',
      definiteSequenceId: res?.DefiniteSequence_ID?.id || '',
      docNoSequence: res?.DocNoSequence_ID?.identifier || '',
      docNoSequenceId: res?.DocNoSequence_ID?.id || '',
      category: res?.GL_Category_ID?.identifier || '',
      categoryId: res?.GL_Category_ID?.id || '',
      docBaseType: res?.DocBaseType?.identifier || '',
      docBaseTypeId: res?.DocBaseType?.id || '',
      processing: false,
      alert: {
        status: '',
        message: ''
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/settings/document-types')
}
const edit = () => {
  router.push(`/settings/document-types/${route.params.id}/edit`)
}
const destroy = async () => {
  const confirmation = confirm('Are you sure want to delete this record?')
  if(confirmation) {
    await $fetch('/api/settings/document-types/'+route.params.id, {
      method: 'DELETE',
      headers: useRequestHeaders(['cookie'])
    })
    router.push(`/settings/document-types`)
  }
}

const dropDownHandler = (e) => {
  if(e.target.parentElement.classList.contains('show')) {
    e.target.parentElement.classList.remove('show')
  } else {
    e.target.parentElement.classList.add('show')
  }
}

const dropItemHandler = (e) => {
  if(e.target.parentElement.parentElement.classList.contains('show')) {
    e.target.parentElement.parentElement.classList.remove('show')
  } else {
    e.target.parentElement.parentElement.classList.add('show')
  }
}

const breadCrumb = computed(() => ([
  { link: '/settings/document-types', text: 'Document Types'},
  { link: '#', text: 'View Document Type Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Document Type 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">
            <div>
              <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 class="dropDown">
              <button type="button" class="button dropDownToggler" @click="dropDownHandler">
                <span class="tf-icon ti ti-settings ti-xs me-1 color-inherit"></span> Action
              </button>
              <div class="dropDownBackdrop" @click="dropDownHandler"></div>
              <div class="dropDownMenu dropDownRight mnw-80px">
                <span class="dropDownItem" @click="dropItemHandler($event); edit();">Edit</span>
                <span class="dropDownItem" @click="dropItemHandler($event); destroy();">Delete</span>
              </div>
            </div>
          </div>
        </div>
        <div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Tenant</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.client }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Orgaanization</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.organization }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Name</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.name }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Description</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.description }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Print Name</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.printName }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Document Copies</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.documentCopies }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Doc No Sequence</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.docNoSequence }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Category</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.category }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Definite Sequence</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.definiteSequence }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Print Format</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.printFormat }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Doc Base Type</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.docBaseType }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Document Note</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.documentNote }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Status</strong>
                  <strong>:</strong>
                </div>
                <div class="d-flex flexDirection-column">
                  <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.isDocNoControlled" class="checkInput" id="isDocNoControlled" :checked="form.isDocNoControlled" disabled>
                    <label class="checkLabel" for="isDocNoControlled">Doc No Controlled</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.hasCharges" class="checkInput" id="hasCharges" :checked="form.hasCharges" disabled>
                    <label class="checkLabel" for="hasCharges">Has Charges</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.hasProforma" class="checkInput" id="hasProforma" :checked="form.hasProforma" disabled>
                    <label class="checkLabel" for="hasProforma">Has Proforma</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.isSOTrx" class="checkInput" id="isSOTrx" :checked="form.isSOTrx" disabled>
                    <label class="checkLabel" for="isSOTrx">Sales Transaction</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isDefaultCounterDoc" class="checkInput" id="isDefaultCounterDoc" :checked="form.isDefaultCounterDoc" disabled>
                    <label class="checkLabel" for="isDefaultCounterDoc">Default Counter Doc</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isPickQAConfirm" class="checkInput" id="isPickQAConfirm" :checked="form.isPickQAConfirm" disabled>
                    <label class="checkLabel" for="isPickQAConfirm">Pick QA Confirm</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isShipConfirm" class="checkInput" id="isShipConfirm" :checked="form.isShipConfirm" disabled>
                    <label class="checkLabel" for="isShipConfirm">Ship Confirm</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isInTransit" class="checkInput" id="isInTransit" :checked="form.isInTransit" disabled>
                    <label class="checkLabel" for="isInTransit">In Transit</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isSplitWhenDifference" class="checkInput" id="isSplitWhenDifference" :checked="form.isSplitWhenDifference" disabled>
                    <label class="checkLabel" for="isSplitWhenDifference">Split When Difference</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isCreateCounter" class="checkInput" id="isCreateCounter" :checked="form.isCreateCounter" disabled>
                    <label class="checkLabel" for="isCreateCounter">Create Counter</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isIndexed" class="checkInput" id="isIndexed" :checked="form.isIndexed" disabled>
                    <label class="checkLabel" for="isIndexed">Indexed</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isOverwriteSeqOnComplete" class="checkInput" id="isOverwriteSeqOnComplete" :checked="form.isOverwriteSeqOnComplete" disabled>
                    <label class="checkLabel" for="isOverwriteSeqOnComplete">Overwrite Seq On Complete</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isOverwriteDateOnComplete" class="checkInput" id="isOverwriteDateOnComplete" :checked="form.isOverwriteDateOnComplete" disabled>
                    <label class="checkLabel" for="isOverwriteDateOnComplete">Overwrite Date On Complete</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isPrepareSplitDocument" class="checkInput" id="isPrepareSplitDocument" :checked="form.isPrepareSplitDocument" disabled>
                    <label class="checkLabel" for="isPrepareSplitDocument">Prepare Split Document</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isChargeOrProductMandatory" class="checkInput" id="isChargeOrProductMandatory" :checked="form.isChargeOrProductMandatory" disabled>
                    <label class="checkLabel" for="isChargeOrProductMandatory">Charge Or Product Mandatory</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isNoPriceListCheck" class="checkInput" id="isNoPriceListCheck" :checked="form.isNoPriceListCheck" disabled>
                    <label class="checkLabel" for="isNoPriceListCheck">No Price List Check</label>
                  </div>
                </div>
              </div>
            </div>
            <div class="column is-half"></div>
          </div>
          
        </div>
      </div>

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