<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,
  isAccrual: false,
  autoPeriodControl: false,
  periodOpenHistory: 0,
  periodOpenFuture: 0,
  separator: '-',
  hasAlias: true,
  hasCombination: false,
  isTradeDiscountPosted: false,
  isDiscountCorrectsTax: false,
  isAdjustCOGS: false,
  isPostServices: false,
  isExplicitCostAdjustment: false,
  isAllowNegativePosting: true,
  isPostIfClearingEqual: true,
  gaap: '',
  gaapId: '',
  costingMethod: '',
  costingMethodId: '',
  currency: '',
  currencyId: '',
  period: '',
  periodId: '',
  costType: '',
  costTypeId: '',
  costingLevel: '',
  costingLevelId: '',
  commitmentType: '',
  commitmentTypeId: '',
  taxCorrectionType: '',
  taxCorrectionTypeId: '',
  orgOnly: '',
  orgOnlyId: '',
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/accountings/schemas/'+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,
      isAccrual: res?.IsAccrual ?? false,
      autoPeriodControl: res?.AutoPeriodControl ?? false,
      periodOpenHistory: res?.Period_OpenHistory || 0,
      periodOpenFuture: res?.Period_OpenFuture || 0,
      separator: res?.Separator || '-',
      hasAlias: res?.HasAlias ?? true,
      hasCombination: res?.HasCombination ?? false,
      isTradeDiscountPosted: res?.IsTradeDiscountPosted ?? false,
      isDiscountCorrectsTax: res?.IsDiscountCorrectsTax ?? false,
      isAdjustCOGS: res?.IsAdjustCOGS ?? false,
      isPostServices: res?.IsPostServices ?? false,
      isExplicitCostAdjustment: res?.IsExplicitCostAdjustment ?? false,
      isAllowNegativePosting: res?.IsAllowNegativePosting ?? true,
      isPostIfClearingEqual: res?.IsPostIfClearingEqual ?? true,
      gaap: res?.GAAP?.identifier || '',
      gaapId: res?.GAAP?.id || '',
      costingMethod: res?.CostingMethod?.identifier || '',
      costingMethodId: res?.CostingMethod?.id || '',
      currency: res?.C_Currency_ID?.identifier || '',
      currencyId: res?.C_Currency_ID?.id || '',
      period: res?.C_Period_ID?.identifier || '',
      periodId: res?.C_Period_ID?.id || '',
      costType: res?.M_CostType_ID?.identifier || '',
      costTypeId: res?.M_CostType_ID?.id || '',
      costingLevel: res?.CostingLevel?.identifier || '',
      costingLevelId: res?.CostingLevel?.id || '',
      commitmentType: res?.CommitmentType?.identifier || '',
      commitmentTypeId: res?.CommitmentType?.id || '',
      taxCorrectionType: res?.TaxCorrectionType?.identifier || '',
      taxCorrectionTypeId: res?.TaxCorrectionType?.id || '',
      orgOnly: res?.AD_OrgOnly_ID?.identifier || '',
      orgOnlyId: res?.AD_OrgOnly_ID?.id || '',
      processing: false,
      alert: {
        status: '',
        message: ''
      }
    }
  }
}
onMounted(retrieve)

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

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/schemas', text: 'Accounting Schemas'},
  { link: '#', text: 'View Accounting Schema Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Accounting Schema 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>GAAP</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.gaap }}</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>Commitment Type</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.commitmentType }}</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>Costing Method</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.costingMethod }}</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>Costing Level</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.costingLevel }}</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>Cost Type</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.costType }}</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>History Days</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.periodOpenHistory }}</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>Future Days</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.periodOpenFuture }}</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>Element Separator</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.separator }}</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>Period</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.period }}</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 Correction</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.taxCorrectionType }}</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>Only Organization</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.orgOnly }}</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.isAccrual" class="checkInput" id="isAccrual" :checked="form.isAccrual" disabled>
                    <label class="checkLabel" for="isAccrual">Accrual</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.autoPeriodControl" class="checkInput" id="autoPeriodControl" :checked="form.autoPeriodControl" disabled>
                    <label class="checkLabel" for="autoPeriodControl">Automatic Period Control</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isExplicitCostAdjustment" class="checkInput" id="isExplicitCostAdjustment" :checked="form.isExplicitCostAdjustment" disabled>
                    <label class="checkLabel" for="isExplicitCostAdjustment">Explicit Cost Adjustment</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isTradeDiscountPosted" class="checkInput" id="isTradeDiscountPosted" :checked="form.isTradeDiscountPosted" disabled>
                    <label class="checkLabel" for="isTradeDiscountPosted">Post Trade Discount</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.hasAlias" class="checkInput" id="hasAlias" :checked="form.hasAlias" disabled>
                    <label class="checkLabel" for="hasAlias">Use Account Alias</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isPostIfClearingEqual" class="checkInput" id="isPostIfClearingEqual" :checked="form.isPostIfClearingEqual" disabled>
                    <label class="checkLabel" for="isPostIfClearingEqual">Post if Clearing Equal</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isAllowNegativePosting" class="checkInput" id="isAllowNegativePosting" :checked="form.isAllowNegativePosting" disabled>
                    <label class="checkLabel" for="isAllowNegativePosting">Allow Negative Posting</label>
                  </div>
                </div>
              </div>
            </div>
            <div class="column is-half">
            </div>
          </div>
          
        </div>
      </div>

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