<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: '',
  currentNext: 1000000,
  startNo: 1000000,
  incrementNo: 1,
  currentNextSys: 100,
  decimalPattern: '',
  vFormat: '',
  prefix: '',
  suffix: '',
  isAudited: false,
  startNewYear: false,
  isTableID: false,
  isAutoSequence: false,
  startNewMonth: false,
  isOrgLevelSequence: false,
  orgColumn: '',
  dateColumn: '',
  alert: {
    status: '',
    message: '',
    processing: false,
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/settings/document-sequences/'+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 || '',
      currentNext: res?.CurrentNext || 1000000,
      startNo: res?.StartNo || 1000000,
      incrementNo: res?.IncrementNo || 1,
      currentNextSys: res?.CurrentNextSys || 100,
      decimalPattern: res?.DecimalPattern || '',
      vFormat: res?.VFormat || '',
      prefix: res?.Prefix || '',
      suffix: res?.Suffix || '',
      isAudited: res?.IsAudited ?? false,
      startNewYear: res?.StartNewYear ?? false,
      isTableID: res?.IsTableID ?? false,
      isAutoSequence: res?.IsAutoSequence ?? false,
      startNewMonth: res?.StartNewMonth ?? false,
      isOrgLevelSequence: res?.IsOrgLevelSequence ?? false,
      orgColumn: res?.OrgColumn || '',
      dateColumn: res?.DateColumn || '',
      alert: {
        status: '',
        message: '',
        processing: false,
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/settings/document-sequences')
}
const edit = () => {
  router.push(`/settings/document-sequences/${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-sequences/'+route.params.id, {
      method: 'DELETE',
      headers: useRequestHeaders(['cookie'])
    })
    router.push(`/settings/document-sequences`)
  }
}

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-sequences', text: 'Document Sequences'},
  { link: '#', text: 'View Document Sequence Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Document Sequence 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>Current Next</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.currentNext }}</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>Current Next Sys</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.currentNextSys }}</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>Start No</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.startNo }}</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>Increment No</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.incrementNo }}</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>Decimal Pattern</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.decimalPattern }}</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>Value Format</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.vFormat }}</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>Prefix</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.prefix }}</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>Suffix</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.suffix }}</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>Org Column</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.orgColumn }}</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>Date Column</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.dateColumn }}</div>
              </div>
            </div>
          </div>
          <div class="columns">
            <div class="column is-full">
              <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.isAudited" class="checkInput" id="isAudited" :checked="form.isAudited" disabled>
                    <label class="checkLabel" for="isAudited">Audited</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isTableID" class="checkInput" id="isTableID" :checked="form.isTableID" disabled>
                    <label class="checkLabel" for="isTableID">Table ID</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isAutoSequence" class="checkInput" id="isAutoSequence" :checked="form.isAutoSequence" disabled>
                    <label class="checkLabel" for="isAutoSequence">Auto Sequence</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.startNewYear" class="checkInput" id="startNewYear" :checked="form.startNewYear" disabled>
                    <label class="checkLabel" for="startNewYear">Start New Year</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.startNewMonth" class="checkInput" id="startNewMonth" :checked="form.startNewMonth" disabled>
                    <label class="checkLabel" for="startNewMonth">Start New Month</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isOrgLevelSequence" class="checkInput" id="isOrgLevelSequence" :checked="form.isOrgLevelSequence" disabled>
                    <label class="checkLabel" for="isOrgLevelSequence">Org Level Sequence</label>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
        </div>
      </div>

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