<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: '',
  organization: '',
  isActive: true,
  name: '',
  description: '',
  isDefault: false,
  isSelfService: false,
  dueDateTolerance: 7,
  isEMailWhenOverdue: false,
  isEMailWhenDue: false,
  isInvoiced: false,
  isAutoChangeRequest: false,
  isConfidentialInfo: false,
  isIndexed: false,
  autoDueDateDays: 0,
  headerColor: '',
  contentColor: '',
  confidentialType: '',
  confidentialTypeId: '',
  statusCategory: '',
  statusCategoryId: '',
})

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

const retrieve = async () => {
  const res = await $fetch('/api/requests/request-types/'+route.params.id, {
    headers: useRequestHeaders(['cookie'])
  })
  if(res) {
    form.value = {
      id: route.params.id,
      organization: res?.AD_Org_ID?.identifier || '',
      isActive: res?.IsActive ?? true,
      name: res?.Name || '',
      description: res?.Description || '',
      isDefault: res?.IsDefault ?? false,
      isSelfService: res?.IsSelfService ?? false,
      dueDateTolerance: res?.DueDateTolerance || 7,
      isEMailWhenOverdue: res?.IsEMailWhenOverdue ?? false,
      isEMailWhenDue: res?.IsEMailWhenDue ?? false,
      isInvoiced: res?.IsInvoiced ?? false,
      isAutoChangeRequest: res?.IsAutoChangeRequest ?? false,
      isConfidentialInfo: res?.IsConfidentialInfo ?? false,
      isIndexed: res?.IsIndexed ?? false,
      autoDueDateDays: res?.AutoDueDateDays || 0,
      headerColor: res?.HeaderColor || '',
      contentColor: res?.ContentColor || '',
      confidentialType: res?.ConfidentialType?.identifier || '',
      confidentialTypeId: res?.ConfidentialType?.id || '',
      statusCategory: res?.R_StatusCategory_ID?.identifier || '',
      statusCategoryId: res?.R_StatusCategory_ID?.id || '',
    }
  }

  for(let record of ['update_notification']) {
    const response = await $fetch('/api/requests/request-types/'+route.params.id+'/'+record.replaceAll('_', '-'), {
      headers: useRequestHeaders(['cookie'])
    })
    if(response) {
      let newData = []
      if(record === 'update_notification') {
        newData = response.records.map(item => {
          return {
            id: item.uid,
            user: item.AD_User_ID.identifier,
            userId: item.AD_User_ID.id,
            isActive: item.IsActive,
            isSelfService: item.IsSelfService,
          }
        })
      }

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

const back = () => {
  router.push('/requests/request-types')
}

const breadCrumb = computed(() => ([
  { link: '#', text: 'Requests'},
  { link: '/requests/request-types', text: 'Request Types'},
  { link: '#', text: 'View Request Type Details', active: true}
]))
</script>

<template>
  <Head>
    <Title>View Request Type 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>
          <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>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>Organization</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.organization }}</div>
            </div>
            <div class="col-md-6 mb-2 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 class="col-md-6 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-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Status Category</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.statusCategory }}</div>
            </div>
            <div class="col-md-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Confidential Type</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.confidentialType }}</div>
            </div>
            <div class="col-md-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Due Date Tolerance</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.dueDateTolerance }}</div>
            </div>
            <div class="col-md-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Auto Due Date Days</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.autoDueDateDays }}</div>
            </div>
            <div class="col-md-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Header Color</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.headerColor }}</div>
            </div>
            <div class="col-md-6 mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Content Color</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.contentColor }}</div>
            </div>
            <div class="col-md-12 mb-3">
              <label class="label d-block mb-2 fw-bold">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.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.isSelfService" class="checkInput" id="isSelfService" :checked="form.isSelfService" disabled>
                <label class="checkLabel" for="isSelfService">Self Service</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isEMailWhenOverdue" class="checkInput" id="isEMailWhenOverdue" :checked="form.isEMailWhenOverdue" disabled>
                <label class="checkLabel" for="isEMailWhenOverdue">Email When Overdue</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isEMailWhenDue" class="checkInput" id="isEMailWhenDue" :checked="form.isEMailWhenDue" disabled>
                <label class="checkLabel" for="isEMailWhenDue">Email When Due</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isInvoiced" class="checkInput" id="isInvoiced" :checked="form.isInvoiced" disabled>
                <label class="checkLabel" for="isInvoiced">Invoiced</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isAutoChangeRequest" class="checkInput" id="isAutoChangeRequest" :checked="form.isAutoChangeRequest" disabled>
                <label class="checkLabel" for="isAutoChangeRequest">Auto Change Request</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isConfidentialInfo" class="checkInput" id="isConfidentialInfo" :checked="form.isConfidentialInfo" disabled>
                <label class="checkLabel" for="isConfidentialInfo">Confidential Info</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isIndexed" class="checkInput" id="isIndexed" :checked="form.isIndexed" disabled>
                <label class="checkLabel" for="isIndexed">Indexed</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">
              <RequestsRequestTypeDetailRecord v-model="detailRecordSelected" :entries="detailRecord" action="view" />
            </div>
          </div>

        </div>
      </div>

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