<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: '',
  name: '',
  description: '',
  isActive: true,
  chargeAmt: 0,
  isSameTax: false,
  isSameCurrency: false,
  isTaxIncluded: false,
  taxCategory: '',
  taxCategoryId: '',
  partner: '',
  partnerId: '',
  chargeType: '',
  chargeTypeId: '',
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/settings/charges/'+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 || '',
      name: res?.Name || '',
      description: res?.Description || '',
      isActive: res?.IsActive ?? true,
      chargeAmt: res?.ChargeAmt || 0,
      isSameTax: res?.IsSameTax ?? false,
      isSameCurrency: res?.IsSameCurrency ?? false,
      isTaxIncluded: res?.IsTaxIncluded ?? false,
      taxCategory: res?.C_TaxCategory_ID?.identifier || '',
      taxCategoryId: res?.C_TaxCategory_ID?.id || '',
      partner: res?.C_BPartner_ID?.identifier || '',
      partnerId: res?.C_BPartner_ID?.id || '',
      chargeType: res?.C_ChargeType_ID?.identifier || '',
      chargeTypeId: res?.C_ChargeType_ID?.id || '',
      processing: false,
      alert: {
        status: '',
        message: ''
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/settings/charges')
}

const save = async (record, id = '', saveType = '') => {
  const res = await $fetch('/api/settings/charges/update', {
    method: 'PUT',
    headers: useRequestHeaders(['cookie']),
    body: {
      id: record.id,
      organizationId: record.organizationId,
      name: record.name,
      description: record.description,
      isActive: record.isActive,
      chargeAmt: record.chargeAmt,
      isSameTax: record.isSameTax,
      isSameCurrency: record.isSameCurrency,
      isTaxIncluded: record.isTaxIncluded,
      taxCategoryId: record.taxCategoryId,
      partnerId: record.partnerId,
      chargeTypeId: record.chargeTypeId,
    }
  })

  if(Number(res.status) === 200) {
    if(saveType === 'preview') {
      router.push('/settings/charges/'+res.id+'/view')
    } else if(saveType === 'changes') {
      form.value.processing = false
      location.reload()
    } 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: '/settings/charges', text: 'Charges'},
  { link: '#', text: 'Edit Charge', active: true}
]))
</script>

<template>
  <Head>
    <Title>Edit Charge</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>
          <SettingsChargeForm v-model="form" @save="save"/>
        </div>
      </div>

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