<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,
  isDefault: false,
  isDocumentLevel: false,
  isSummary: false,
  isTaxExempt: false,
  isSalesTax: false,
  validFrom: date.now('date'),
  rate: 6,
  requiresTaxCertificate: false,
  taxIndicator: '6%CT',
  countryGroup: '',
  countryGroupId: '',
  country: '',
  countryId: '',
  region: '',
  regionId: '',
  toCountryGroup: '',
  toCountryGroupId: '',
  toCountry: '',
  toCountryId: '',
  toRegion: '',
  toRegionId: '',
  taxCategory: '',
  taxCategoryId: '',
  SOPOType: '',
  SOPOTypeId: '',
  taxPostingIndicator: '',
  taxPostingIndicatorId: '',
  taxProvider: '',
  taxProviderId: '',
  parentTax: '',
  parentTaxId: '',
  processing: false,
  alert: {
    status: '',
    message: '',
    processing: false
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/settings/tax-rates/'+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,
      isDefault: res?.IsDefault ?? false,
      isDocumentLevel: res?.IsDocumentLevel ?? false,
      isSummary: res?.IsSummary ?? false,
      isTaxExempt: res?.IsTaxExempt ?? false,
      isSalesTax: res?.IsSalesTax ?? false,
      validFrom: res?.ValidFrom || date.now('date'),
      rate: res?.Rate || 6,
      requiresTaxCertificate: res?.RequiresTaxCertificate ?? false,
      taxIndicator: res?.TaxIndicator || '6%CT',
      countryGroup: res?.C_CountryGroup_ID?.identifier || '',
      countryGroupId: res?.C_CountryGroup_ID?.id || '',
      country: res?.C_Country_ID?.identifier || '',
      countryId: res?.C_Country_ID?.id || '',
      region: res?.C_Region_ID?.identifier || '',
      regionId: res?.C_Region_ID?.id || '',
      toCountryGroup: res?.To_CountryGroup_ID?.identifier || '',
      toCountryGroupId: res?.To_CountryGroup_ID?.id || '',
      toCountry: res?.To_Country_ID?.identifier || '',
      toCountryId: res?.To_Country_ID?.id || '',
      toRegion: res?.To_Region_ID?.identifier || '',
      toRegionId: res?.To_Region_ID?.id || '',
      taxCategory: res?.C_TaxCategory_ID?.identifier || '',
      taxCategoryId: res?.C_TaxCategory_ID?.id || '',
      SOPOType: res?.SOPOType?.identifier || '',
      SOPOTypeId: res?.SOPOType?.id || '',
      taxPostingIndicator: res?.TaxPostingIndicator?.identifier || '',
      taxPostingIndicatorId: res?.TaxPostingIndicator?.id || '',
      taxProvider: res?.C_TaxProvider_ID?.identifier || '',
      taxProviderId: res?.C_TaxProvider_ID?.id || '',
      parentTax: res?.Parent_Tax_ID?.identifier || '',
      parentTaxId: res?.Parent_Tax_ID?.id || '',
      processing: false,
      alert: {
        status: '',
        message: '',
        processing: false
      }
    }
  }
}
onMounted(retrieve)

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

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: '/settings/tax-rates', text: 'Tax Rates'},
  { link: '#', text: 'View Tax Rate Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Tax Rate 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>Name</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.name }}</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>Rate</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.rate }}</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>Tax Indicator</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.taxIndicator }}</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>Valid From</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.validFrom }}</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>Parent Tax</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.parentTax }}</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>Tax Category</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.taxCategory }}</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>SO/PO Type</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.SOPOType }}</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>Tax Posting Indicator</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.taxPostingIndicator }}</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>Tax Provider</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.taxProvider }}</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>Country Group</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.countryGroup }}</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>To</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.toCountryGroup }}</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>Country</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.country }}</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>To</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.toCountry }}</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>Region</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.region }}</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>To</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.toRegion }}</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>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.isDefault" class="checkInput" id="isDefault" :checked="form.isDefault" disabled>
                    <label class="checkLabel" for="isDefault">Default</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isDocumentLevel" class="checkInput" id="isDocumentLevel" :checked="form.isDocumentLevel" disabled>
                    <label class="checkLabel" for="isDocumentLevel">Document Level</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isSummary" class="checkInput" id="isSummary" :checked="form.isSummary" disabled>
                    <label class="checkLabel" for="isSummary">Summary</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isTaxExempt" class="checkInput" id="isTaxExempt" :checked="form.isTaxExempt" disabled>
                    <label class="checkLabel" for="isTaxExempt">Tax Exempt</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isSalesTax" class="checkInput" id="isSalesTax" :checked="form.isSalesTax" disabled>
                    <label class="checkLabel" for="isSalesTax">Sales Tax</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.requiresTaxCertificate" class="checkInput" id="requiresTaxCertificate" :checked="form.requiresTaxCertificate" disabled>
                    <label class="checkLabel" for="requiresTaxCertificate">Requires Tax Certificate</label>
                  </div>
                </div>
              </div>
            </div>
            <div class="column is-half"></div>
          </div>
          
        </div>
      </div>

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