<script setup>
import { date } from 'alga-js'
const router = useRouter()

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

const form = ref({
  id: '',
  client: '',
  clientId: '',
  organization: '',
  organizationId: '',
  description: '',
  isActive: true,
  documentNo: '',
  isPrinted: false,
  dateDoc: date.now('date'),
  dateAcct: date.now('date'),
  totalDr: 0,
  totalCr: 0,
  isApproved: false,
  currencyRate: 1,
  controlAmt: 0,
  processed: false,
  docType: '',
  docTypeId: '',
  docStatus: 'Draft',
  docStatusId: 'DR',
  postingType: '',
  postingTypeId: '',
  acctSchema: '',
  acctSchemaId: '',
  category: '',
  categoryId: '',
  period: '',
  periodId: '',
  currency: '',
  currencyId: '',
  conversionType: '',
  conversionTypeId: '',
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const back = () => {
  router.push('/accountings/journals')
}

const save = async (record, id = '', saveType = '') => {
  const res = await $fetch('/api/accountings/journals/store', {
    method: 'POST',
    headers: useRequestHeaders(['cookie']),
    body: {
      organizationId: record.organizationId,
      description: record.description,
      isActive: record.isActive,
      documentNo: record.documentNo,
      isPrinted: record.isPrinted,
      dateDoc: record.dateDoc,
      dateAcct: record.dateAcct,
      totalDr: record.totalDr,
      totalCr: record.totalCr,
      isApproved: record.isApproved,
      currencyRate: record.currencyRate,
      controlAmt: record.controlAmt,
      processed: record.processed,
      docTypeId: record.docTypeId,
      docStatusId: record.docStatusId,
      postingTypeId: record.postingTypeId,
      acctSchemaId: record.acctSchemaId,
      categoryId: record.categoryId,
      periodId: record.periodId,
      currencyId: record.currencyId,
      conversionTypeId: record.conversionTypeId,
    }
  })

  if(Number(res.status) === 200) {
    if(saveType === 'preview') {
      router.push('/accountings/journals/'+res.id+'/view')
    } else if(saveType === 'changes') {
      router.push('/accountings/journals/'+res.id+'/edit')
    } else {
      back()
    }
  } else {
    form.value.alert.status = res.status
    form.value.alert.message = res.message
    form.value.processing = false
    window.scrollTo(0, 0)
  }
}

const breadCrumb = computed(() => ([
  { link: '/accountings/journals', text: 'Journal Entries'},
  { link: '#', text: 'Add Journal Entry', active: true}
]))
</script>

<template>
  <Head>
    <Title>Add Journal Entry</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">
            <button type="button" @click="back" class="button">
              <span class="tf-icon ti ti-chevron-left ti-xs me-1"></span> Back
            </button>
          </div>
        </div>
        <div>
          <AccountingsJournalForm v-model="form" @save="save"/>
        </div>
      </div>

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