<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: '',
  value: '',
  description: '',
  help: '',
  isActive: true,
  lot: 0,
  isDisposed: false,
  isInPosession: false,
  useLifeYears: 0,
  assetDisposalDate: date.now(),
  isOwned: false,
  locationComment: '',
  isDepreciated: false,
  lifeUseUnits: 0,
  processing: false,
  guaranteeDate: date.now(),
  useLifeMonths: 0,
  assetServiceDate: date.now(),
  useUnits: 0,
  serNo: '',
  isFullyDepreciated: false,
  qty: 0,
  lastMaintenanceDate: date.now(),
  nextMaintenenceDate: date.now(),
  lastMaintenanceUnit: 0,
  nextMaintenenceUnit: 0,
  lastMaintenanceNote: '',
  assetCreateDate: date.now(),
  assetRevalDate: date.now(),
  qtyCurrent: 0,
  qtyOriginal: 0,
  processed: false,
  assetActivationDate: date.now(),
  inventoryNo: '',
  assetType: 'MFX',
  manufacturedYear: 1900,
  user: '',
  userId: '',
  product: '',
  productId: '',
  location: '',
  locationId: '',
  assetGroup: '',
  assetGroupId: '',
  partner: '',
  partnerId: '',
  partnerLocation: '',
  partnerLocationId: '',
  attributeSetInstance: '',
  attributeSetInstanceId: '',
  project: '',
  projectId: '',
  parentAsset: '',
  parentAssetId: '',
  assetStatus: '',
  assetStatusId: '',
  assetAction: '',
  assetActionId: '',
  activity: '',
  activityId: '',
  processing: false,
  alert: {
    status: '',
    message: ''
  }
})

const retrieve = async () => {
  const res = await $fetch('/api/assets/assets/'+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 || '',
      value: res?.Value || '',
      description: res?.Description || '',
      help: res?.Help || '',
      isActive: res?.IsActive ?? true,
      lot: res?.Lot,
      isDisposed: res?.IsDisposed,
      isInPosession: res?.IsInPosession,
      useLifeYears: res?.UseLifeYears,
      assetDisposalDate: res?.AssetDisposalDate,
      isOwned: res?.IsOwned,
      locationComment: res?.LocationComment || '',
      isDepreciated: res?.IsDepreciated,
      lifeUseUnits: res?.LifeUseUnits,
      processing: res?.Processing,
      guaranteeDate: res?.GuaranteeDate,
      useLifeMonths: res?.UseLifeMonths,
      assetServiceDate: res?.AssetServiceDate,
      useUnits: res?.UseUnits,
      serNo: res?.SerNo || '',
      isFullyDepreciated: res?.IsFullyDepreciated,
      qty: res?.Qty,
      lastMaintenanceDate: res?.LastMaintenanceDate,
      nextMaintenenceDate: res?.NextMaintenenceDate,
      lastMaintenanceUnit: res?.LastMaintenanceUnit,
      nextMaintenenceUnit: res?.NextMaintenenceUnit,
      lastMaintenanceNote: res?.LastMaintenanceNote,
      assetCreateDate: res?.A_Asset_CreateDate,
      assetRevalDate: res?.A_Asset_RevalDate,
      qtyCurrent: res?.A_QTY_Current,
      qtyOriginal: res?.A_QTY_Original,
      processed: res?.Processed,
      assetActivationDate: res?.AssetActivationDate,
      inventoryNo: res?.InventoryNo,
      assetType: res?.A_AssetType,
      manufacturedYear: res?.ManufacturedYear || 1900,
      user: res?.AD_User_ID?.identifier || '',
      userId: res?.AD_User_ID?.id || '',
      product: res?.M_Product_ID?.identifier || '',
      productId: res?.M_Product_ID?.id || '',
      location: res?.C_Location_ID?.identifier || '',
      locationId: res?.C_Location_ID?.id || '',
      assetGroup: res?.A_Asset_Group_ID?.identifier || '',
      assetGroupId: res?.A_Asset_Group_ID?.id || '',
      partner: res?.C_BPartner_ID?.identifier || '',
      partnerId: res?.C_BPartner_ID?.id || '',
      partnerLocation: res?.C_BPartner_Location_ID?.identifier || '',
      partnerLocationId: res?.C_BPartner_Location_ID?.id || '',
      attributeSetInstance: res?.M_AttributeSetInstance_ID?.identifier || '',
      attributeSetInstanceId: res?.M_AttributeSetInstance_ID?.id || '',
      project: res?.C_Project_ID?.identifier || '',
      projectId: res?.C_Project_ID?.id || '',
      parentAsset: res?.A_Parent_Asset_ID?.identifier || '',
      parentAssetId: res?.A_Parent_Asset_ID?.id || '',
      assetStatus: res?.A_Asset_Status?.identifier || '',
      assetStatusId: res?.A_Asset_Status?.id || '',
      assetAction: res?.A_Asset_Action?.identifier || '',
      assetActionId: res?.A_Asset_Action?.id || '',
      activity: res?.C_Activity_ID?.identifier || '',
      activityId: res?.C_Activity_ID?.id || '',
      processing: false,
      alert: {
        status: '',
        message: ''
      }
    }
  }
}
onMounted(retrieve)

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

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: '/assets/assets', text: 'Assets'},
  { link: '#', text: 'View Asset Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Asset 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>Search Key</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.value }}</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>Inventory No</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.inventoryNo }}</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>Asset Group</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.assetGroup }}</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>Description</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.description }}</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>Comment/Help</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.help }}</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>Product</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.product }}</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>Attribute Set Instance</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.attributeSetInstance }}</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>Serial No</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.serNo }}</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>Manufactured Year</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.manufacturedYear }}</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>In Service Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.assetServiceDate }}</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>Guarantee Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.guaranteeDate }}</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>Create Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.assetCreateDate }}</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>Revaluation Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.assetRevalDate }}</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>Project</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.project }}</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>Activity</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.activity }}</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>Business Partner</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.partner }}</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>User/Contact</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.user }}</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>Partner Location</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.partnerLocation }}</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>Location comment</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.locationComment }}</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>Last Maintenance</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.lastMaintenanceDate }}</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>Last Unit</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.lastMaintenanceUnit }}</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>Next Maintenance</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.nextMaintenanceDate }}</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>Next Unit</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.nextMaintenanceUnit }}</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>Activation Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.assetActivationDate }}</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>Asset Disposal Date</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.assetDisposalDate }}</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>Asset Status</strong>
                  <strong>:</strong>
                </div>
                <div>{{ form.assetStatus }}</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.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.isOwned" class="checkInput" id="isOwned" :checked="form.isOwned" disabled>
                    <label class="checkLabel" for="isOwned">Owned</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isDepreciated" class="checkInput" id="isDepreciated" :checked="form.isDepreciated" disabled>
                    <label class="checkLabel" for="isDepreciated">Depreciated</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isFixedAsset" class="checkInput" id="isFixedAsset" :checked="form.isFixedAsset" disabled>
                    <label class="checkLabel" for="isFixedAsset">Fixed Asset</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isOneAssetPerUOM" class="checkInput" id="isOneAssetPerUOM" :checked="form.isOneAssetPerUOM" disabled>
                    <label class="checkLabel" for="isOneAssetPerUOM">One Asset per UOM</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isCreateAsActive" class="checkInput" id="isCreateAsActive" :checked="form.isCreateAsActive" disabled>
                    <label class="checkLabel" for="isCreateAsActive">Create as Active</label>
                  </div>
                  <div class="check">
                    <input type="checkbox" v-model="form.isTrackIssues" class="checkInput" id="isTrackIssues" :checked="form.isTrackIssues" disabled>
                    <label class="checkLabel" for="isTrackIssues">Track Issues</label>
                  </div>
                </div>
              </div>
            </div>
            
          </div>
          
        </div>
      </div>

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