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

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: '',
  docStatusId: '',
  postingType: '',
  postingTypeId: '',
  acctSchema: '',
  acctSchemaId: '',
  category: '',
  categoryId: '',
  period: '',
  periodId: '',
  currency: '',
  currencyId: '',
  conversionType: '',
  conversionTypeId: '',
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/accountings/journals/'+route.params.id, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    form.value = {
      id: route.params.id,
      client: res?.AD_Client_ID?.identifier || '',
      clientId: res?.AD_Client_ID?.id || '',
      organization: res?.AD_Org_ID?.identifier || '',
      organizationId: res?.AD_Org_ID?.id || '',
      description: res?.Description || '',
      isActive: res?.IsActive ?? true,
      documentNo: res?.DocumentNo || '',
      isPrinted: res?.IsPrinted ?? false,
      dateDoc: res?.DateDoc || date.now('date'),
      dateAcct: res?.DateAcct || date.now('date'),
      totalDr: res?.TotalDr || 0,
      totalCr: res?.TotalCr || 0,
      isApproved: res?.IsApproved ?? false,
      currencyRate: res?.CurrencyRate || 1,
      controlAmt: res?.ControlAmt || 0,
      processed: res?.Processed ?? false,
      docType: res?.C_DocType_ID?.identifier || '',
      docTypeId: res?.C_DocType_ID?.id || '',
      docStatus: res?.DocStatus?.identifier || '',
      docStatusId: res?.DocStatus?.id || '',
      postingType: res?.PostingType?.identifier || '',
      postingTypeId: res?.PostingType?.id || '',
      acctSchema: res?.C_AcctSchema_ID?.identifier || '',
      acctSchemaId: res?.C_AcctSchema_ID?.id || '',
      category: res?.GL_Category_ID?.identifier || '',
      categoryId: res?.GL_Category_ID?.id || '',
      period: res?.C_Period_ID?.identifier || '',
      periodId: res?.C_Period_ID?.id || '',
      currency: res?.C_Currency_ID?.identifier || '',
      currencyId: res?.C_Currency_ID?.id || '',
      conversionType: res?.C_ConversionType_ID?.identifier || '',
      conversionTypeId: res?.C_ConversionType_ID?.id || '',
      processing: false,
      alert: {
        status: '',
        message: ''
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/accountings/journals')
}
const edit = () => {
  router.push(`/accountings/journals/${route.params.id}/edit`)
}
const destroy = async () => {
  const confirmation = confirm('Are you sure want to delete this record?')
  if(confirmation) {
    await $fetch('/api/accountings/journals/'+route.params.id, {
      method: 'DELETE',
      headers: useRequestHeaders(['cookie'])
    })
    router.push(`/accountings/journals`)
  }
}

const dropDownHandler = (e) => {
  if(e.target.parentElement.classList.contains('show')) {
    e.target.parentElement.classList.remove('show')
  } else {
    e.target.parentElement.classList.add('show')
  }
}

const dropItemHandler = (e) => {
  if(e.target.parentElement.parentElement.classList.contains('show')) {
    e.target.parentElement.parentElement.classList.remove('show')
  } else {
    e.target.parentElement.parentElement.classList.add('show')
  }
}

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

<template>
  <Head>
    <Title>View Journal Entry Details</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">
            <div>
              <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 class="dropDown">
              <button type="button" class="button dropDownToggler" @click="dropDownHandler">
                <span class="tf-icon ti ti-settings ti-xs me-1 color-inherit"></span> Action
              </button>
              <div class="dropDownBackdrop" @click="dropDownHandler"></div>
              <div class="dropDownMenu dropDownRight mnw-80px">
                <span class="dropDownItem" @click="dropItemHandler($event); edit();">Edit</span>
                <span class="dropDownItem" @click="dropItemHandler($event); destroy();">Delete</span>
              </div>
            </div>
          </div>
        </div>
        <div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Tenant</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.client }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Orgaanization</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.organization }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Accounting Schema</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.acctSchema }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Document No</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.documentNo }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Journal Batch</strong>
                  <strong>:</strong>
                </div>
                <div></div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Description</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.description }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Posting Type</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.postingType }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Document Type</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.docType }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>GL Category</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.category }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Currency</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.currency }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Document Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.dateDoc }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Account Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.dateAcct }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Period</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.docType }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Document Status</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.docStatus }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Total Debit</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.totalDr }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Total Credit</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.totalCr }}</div>
              </div>
            </div>
            
          </div>
          <div class="columns">
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Control Amount</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.controlAmt }}</div>
              </div>
            </div>
            <div class="column is-half">
              <div class="d-flex gap-10px">
                <div class="w-150px d-flex justify-between gap-10px">
                  <strong>Status</strong>
                  <strong>:</strong>
                </div>
                <div class="d-flex flexDirection-column">
                  <div class="check">
                    <input type="checkbox" v-model="form.isActive" class="checkInput" id="isActive" :checked="form.isActive" disabled>
                    <label class="checkLabel" for="isActive">Active</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isApproved" class="checkInput" id="isApproved" :checked="form.isApproved" disabled>
                    <label class="checkLabel" for="isApproved">Approved</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isPrinted" class="checkInput" id="isPrinted" :checked="form.isPrinted" disabled>
                    <label class="checkLabel" for="isPrinted">Printed</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.processed" class="checkInput" id="processed" :checked="form.processed" disabled>
                    <label class="checkLabel" for="processed">Processed</label>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
        </div>
      </div>

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