<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: '',
  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: '',
  alert: {
    status: '',
    message: '',
    processing: false,
  }
})

const back = () => {
  router.push('/settings/document-types')
}

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

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

  const res = await $fetch('/api/settings/document-types/store', {
    method: 'POST',
    headers: useRequestHeaders(['cookie']),
    body: {
      organizationId: form.value.organizationId,
      isActive: form.value.isActive,
      name: form.value.name,
      description: form.value.description,
      isDocNoControlled: form.value.isDocNoControlled,
      printName: form.value.printName,
      hasCharges: form.value.hasCharges,
      hasProforma: form.value.hasProforma,
      isDefault: form.value.isDefault,
      documentCopies: form.value.documentCopies,
      isSOTrx: form.value.isSOTrx,
      isDefaultCounterDoc: form.value.isDefaultCounterDoc,
      isPickQAConfirm: form.value.isPickQAConfirm,
      isShipConfirm: form.value.isShipConfirm,
      isInTransit: form.value.isInTransit,
      isSplitWhenDifference: form.value.isSplitWhenDifference,
      isCreateCounter: form.value.isCreateCounter,
      isIndexed: form.value.isIndexed,
      isOverwriteSeqOnComplete: form.value.isOverwriteSeqOnComplete,
      isOverwriteDateOnComplete: form.value.isOverwriteDateOnComplete,
      isPrepareSplitDocument: form.value.isPrepareSplitDocument,
      isChargeOrProductMandatory: form.value.isChargeOrProductMandatory,
      isNoPriceListCheck: form.value.isNoPriceListCheck,
      documentNote: form.value.documentNote,
      printFormatId: form.value.printFormatId,
      definiteSequenceId: form.value.definiteSequenceId,
      docNoSequenceId: form.value.docNoSequenceId,
      categoryId: form.value.categoryId,
      docBaseTypeId: form.value.docBaseTypeId,
    }
  })

  if(Number(res.status) === 200) {
    if(saveType === 'preview') {
      router.push('/settings/document-types/'+res.id+'/view')
    } else if(saveType === 'changes' || saveType === 'onlysave') {
      router.push('/settings/document-types/'+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-types', text: 'Document Types'},
  { link: '#', text: 'Add Document Type', active: true}
]))
</script>

<template>
  <Head>
    <Title>Add Document Type</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.printName === '' || form.categoryId === '' || form.docBaseTypeId === '' || form.documentCopies === '' || form.printFormatId === '' || 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>
        <SettingsDocumentTypeForm v-model="form" @save="save"/>
      </div>
    </div>
  </NuxtLayout>
</template>