<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: '',
  seqNo: '',
  whereClause: '',
  table: '',
  tableId: '',
  form: '',
  formId: '',
  entityType: '',
  entityTypeId: '',
  window: '',
  windowId: '',
  namePrintColor: '',
  namePrintColorId: '',
  numberPrintColor: '',
  numberPrintColorId: '',
  project: '',
  projectId: '',
  namePrintFont: '',
  namePrintFontId: '',
  numberPrintFont: '',
  numberPrintFontId: '',
  alert: {
    status: '',
    message: '',
    processing: false,
  }
})

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

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

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

  const res = await $fetch('/api/settings/document-status/store', {
    method: 'POST',
    headers: useRequestHeaders(['cookie']),
    body: {
      organizationId: form.value.organizationId,
      isActive: form.value.isActive,
      name: form.value.name,
      description: form.value.description,
      seqNo: form.value.seqNo,
      whereClause: form.value.whereClause,
      tableId: form.value.tableId,
      formId: form.value.formId,
      entityTypeId: form.value.entityTypeId,
      windowId: form.value.windowId,
      namePrintColorId: form.value.namePrintColorId,
      numberPrintColorId: form.value.numberPrintColorId,
      projectId: form.value.projectId,
      namePrintFontId: form.value.namePrintFontId,
      numberPrintFontId: form.value.numberPrintFontId,
    }
  })

  if(Number(res.status) === 200) {
    if(saveType === 'preview') {
      router.push('/settings/document-status/'+res.id+'/view')
    } else if(saveType === 'changes' || saveType === 'onlysave') {
      router.push('/settings/document-status/'+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.alert.scrollTo(0, 0)
  }
}

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

<template>
  <Head>
    <Title>Add Document Status</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.seqNo === '' || form.tableId === '' || 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>
        <SettingsDocumentStatusForm v-model="form" @save="save" />
      </div>
    </div>
  </NuxtLayout>
</template>