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

definePageMeta({
  middleware: ['auth', 'org']
})

const clients = useCookie('logship_clients')
const clientId = useCookie('logship_client_id')

const clientName = computed(() => {
  return clients.value?.filter(i => i.id == clientId.value)?.[0]?.name || 0
})

const form = ref({
  id: '',
  isActive: true,
  name: '',
  description: '',
  value: '',
  isSummary: false,
  replicationStrategy: '',
  replicationStrategyId: '',
})

const detailRecordSelected = ref('organization_info')
const detailRecord = ref({
  organization_info: []
})

const retrieve = async () => {
  const res = await $fetch('/api/admin/organizations/'+route.params.id, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    form.value = {
      id: route.params.id,
      isActive: res?.IsActive ?? true,
      name: res?.Name || '',
      description: res?.Description || '',
      value: res?.Value || '',
      isSummary: res?.IsSummary ?? false,
      replicationStrategy: res?.AD_ReplicationStrategy_ID?.identifier || '',
      replicationStrategyId: res?.AD_ReplicationStrategy_ID?.id || '',
    }
    
    for(let record of ['organization_info']) {
      const response = await $fetch('/api/admin/organizations/'+route.params.id+'/'+record.replaceAll('_', '-'), {
        headers: useRequestHeaders(['cookie'])
      })
      if(response) {
        let newData = []
        if(record === 'organization_info') {
          newData = response.records.map(item => {
            return {
              id: item.id,
              isActive: item.IsActive,
              DUNS: item.DUNS,
              taxID: item.TaxID,
              tenant: item?.AD_Client_ID?.identifier || '',
              tenantId: item?.AD_Client_ID?.id || '',
              location: item?.C_Location_ID?.identifier || '',
              locationId: item?.C_Location_ID?.id || '',
              warehouse: item?.M_Warehouse_ID?.identifier || '',
              warehouseId: item?.M_Warehouse_ID?.id || '',
              orgType: item?.AD_OrgType_ID?.identifier || '',
              orgTypeId: item?.AD_OrgType_ID?.id || '',
              supervisor: item?.Supervisor_ID?.identifier || '',
              supervisorId: item?.Supervisor_ID?.id || '',
              parentOrg: item?.Parent_Org_ID?.identifier || '',
              parentOrgId: item?.Parent_Org_ID?.id || '',
              show: true
            }
          })
        }

        detailRecord.value[record] = newData
      }
    }
  }
}
onMounted(retrieve)

const back = () => {
  router.push('/users/organizations')
}

const breadCrumb = computed(() => ([
  { link: '/users/users', text: 'Users'},
  { link: '/users/organizations', text: 'Organizations'},
  { link: '#', text: 'View Organization Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Organization 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">
            <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>
        <div class="details">
          <div class="row mb-3 mt-0">
            <div class="col-md-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Organization Name</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.name }}</div>
            </div>
            <div class="col-md-6 mb-2 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 class="col-md-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Tenant</strong>
                <strong>:</strong>
              </div>
              <div>{{ clientName }}</div>
            </div>
            <div class="col-md-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Replication Strategy</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.replicationStrategy }}</div>
            </div>
            <div class="col-md-12 mb-2 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 class="col-md-12 mb-3">
              <label class="label d-block mb-2 fw-bold">Organization Status :</label>
              <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.isSummary" class="checkInput" id="isSummary" :checked="form.isSummary" disabled>
                <label class="checkLabel" for="isSummary">Summary</label>
              </div>
            </div>

          </div>
          
          <div class="row mb-3">
            <h4 class="card-action-title bb-1px_solid_hexeee">Detail Record</h4>
            <div class="col-md-12 mb-3">
              <AdminOrganizationDetailRecord v-model="detailRecordSelected" :entries="detailRecord" action="view" />
            </div>
          </div>

        </div>
      </div>

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