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

const router = useRouter()
const route = useRoute()

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 retrieve = async () => {
  const res = await $fetch('/api/settings/document-status/'+route.params.id, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    form.value = {
      id: route.params.id,
      uid: res?.uid || '',
      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 || '',
      seqNo: res?.SeqNo || '',
      whereClause: res?.WhereClause || '',
      table: res?.AD_Table_ID?.identifier || '',
      tableId: res?.AD_Table_ID?.id || '',
      form: res?.AD_Form_ID?.identifier || '',
      formId: res?.AD_Form_ID?.id || '',
      entityType: res?.EntityType?.identifier || '',
      entityTypeId: res?.EntityType?.id || '',
      window: res?.AD_Window_ID?.identifier || '',
      windowId: res?.AD_Window_ID?.id || '',
      namePrintColor: res?.Name_PrintColor_ID?.identifier || '',
      namePrintColorId: res?.Name_PrintColor_ID?.id || '',
      numberPrintColor: res?.Number_PrintColor_ID?.identifier || '',
      numberPrintColorId: res?.Number_PrintColor_ID?.id || '',
      project: res?.C_Project_ID?.identifier || '',
      projectId: res?.C_Project_ID?.id || '',
      namePrintFont: res?.Name_PrintFont_ID?.identifier || '',
      namePrintFontId: res?.Name_PrintFont_ID?.id || '',
      numberPrintFont: res?.Number_PrintFont_ID?.identifier || '',
      numberPrintFontId: res?.Number_PrintFont_ID?.id || '',
      alert: {
        status: '',
        message: '',
        processing: false,
      }
    }
  }
}
onMounted(retrieve)

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/update', {
    method: 'PUT',
    headers: useRequestHeaders(['cookie']),
    body: {
      id: form.value.id,
      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') {
      form.value.alert.processing = false
      toast({
        message: 'Updated!',
        type: 'is-success',
        duration: 2000
      })
      //location.reload()
    } 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 showAttachment = ref(false)

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>
        <template v-if="form.id !== ''">
          <button type="button" @click="submit" class="button" :class="(form.name === '' || form.seqNo === '' || form.tableId === '' || form.alert.processing) ? 'pointerEvents-none opacity-0.6' : ''">Save + Exit</button>
          <button type="button" @click="showAttachment = true" class="button" :class="(form.name === '' || form.seqNo === '' || form.tableId === '' || form.alert.processing) ? 'pointerEvents-none opacity-0.6' : ''">Attachment</button>
        </template>
      </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>
        <AttachmentModal v-model="showAttachment" target="pa_documentstatus" :info="form" />
      </div>
    </div>
  </NuxtLayout>
</template>