<script setup>
import { toast } from 'bulma-toast'

const router = useRouter()

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

const form = ref({
  id: '',
  uid: '',
  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 back = () => {
  router.push('/settings/document-sequences')
}

const save = async (record) => {
  form.value = record
}

const submit = async (saveType = '') => {
  form.value.alert.processing = true

  const res = await $fetch('/api/settings/document-sequences/store', {
    method: 'POST',
    headers: useRequestHeaders(['cookie']),
    body: {
      organizationId: form.value.organizationId,
      isActive: form.value.isActive,
      name: form.value.name,
      description: form.value.description,
      currentNext: form.value.currentNext,
      startNo: form.value.startNo,
      incrementNo: form.value.incrementNo,
      currentNextSys: form.value.currentNextSys,
      decimalPattern: form.value.decimalPattern,
      vFormat: form.value.vFormat,
      prefix: form.value.prefix,
      suffix: form.value.suffix,
      isAudited: form.value.isAudited,
      startNewYear: form.value.startNewYear,
      isTableID: form.value.isTableID,
      isAutoSequence: form.value.isAutoSequence,
      startNewMonth: form.value.startNewMonth,
      isOrgLevelSequence: form.value.isOrgLevelSequence,
      orgColumn: form.value.orgColumn,
      dateColumn: form.value.dateColumn,
    }
  })

  if(Number(res.status) === 200) {
    if(saveType === 'preview') {
      router.push('/settings/document-sequences/'+res.id+'/view')
    } else if(saveType === 'changes' || saveType === 'onlysave') {
      router.push('/settings/document-sequences/'+res.id+'/edit')
      toast({
        message: 'Saved!',
        type: 'is-success',
        duration: 2000
      })
    } else {
      back()
    }
  } else {
    form.value.alert.status = res.status
    form.value.alert.message = res.message
    form.value.alert.processing = false
    toast({
      message: res.message || 'Save error',
      type: 'is-danger',
      duration: 2000
    })
    //window.scrollTo(0, 0)
  }
}

const breadCrumb = computed(() => ([
  { link: '#', text: 'Settings'},
  { link: '/settings/document-sequences', text: 'Document Sequences'},
  { link: '#', text: 'Add Document Sequence', active: true}
]))
</script>

<template>
  <Head>
    <Title>Add Document Sequence</Title>
  </Head>

  <NuxtLayout name="new-dash">
    <NewBreadCrumb :items="breadCrumb" />
    
    <div class="bannercontrol">
      <div class="d-flex gap-10px">
        <button type="button" @click="submit('onlysave')" class="button" :class="(form.name === '' || (form.isAutoSequence === true && (form.currentNext === '' || form.startNo === '' || form.incrementNo === '')) || (form.isAutoSequence === true && form.isTableID === true && (form.currentNext === '' || form.incrementNo === '')) || (form.isTableID === false && form.startNewYear === true && form.dateColumn === '') || form.alert.processing) ? 'pointerEvents-none opacity-0.6' : ''">Save</button>
      </div>
      <div>
        <div class="d-flex gap-10px">
          <button v-if="activeSave" type="button" @click="activeSave = false; back();" class="button">
            <span class="tf-icon ti ti-square-x ti-xs me-1 color-inherit"></span> Abort
          </button>
          <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="banner">
      <div>
        <SettingsDocumentSequenceForm v-model="form" @save="save" />
      </div>
    </div>
  </NuxtLayout>
</template>