<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: '',
  password: '',
  eMail: '',
  isFullBPAccess: false,
  value: '',
  isInPayroll: false,
  isSalesLead: false,
  isLocked: false,
  failedLoginCount: 0,
  isNoPasswordReset: false,
  isExpired: false,
  isAddMailTextAutomatically: false,
  isNoExpire: false,
  isSupportUser: false,
  isShipTo: false,
  isBillTo: false,
  isVendorLead: false,
  eMailUser: '',
  phone2: '',
  birthday: date.now('date'),
  phone: '',
  fax: '',
  lastResult: '',
  lastContact: date.now('date'),
  title: '',
  comments: '',
  eMailVerify: 0,
  eMailVerifyDate: '',
  LDAPUser: '',
  datePasswordChanged: '',
  supervisor: '',
  partner: '',
  notificationType: '',
  greeting: '',
  partnerLocation: '',
  orgTrx: '',
  job: '',
  isMenuAutoExpand: '',
  mailTextId: ''
})

const detailRecordSelected = ref('user_roles')
const detailRecord = ref({
  user_roles: [],
  user_substitute: [],
  user_orgaccess: [],
  user_mail: [],
  user_query: [],
  document_status_access: []
})

const retrieve = async () => {
  const res = await $fetch('/api/admin/users/'+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 || '',
      password: '',
      eMail: res?.EMail || '',
      isFullBPAccess: res?.IsFullBPAccess ?? false,
      value: res?.Value || '',
      isInPayroll: res?.IsInPayroll ?? false,
      isSalesLead: res?.IsSalesLead ?? false,
      isLocked: res?.IsLocked ?? false,
      failedLoginCount: res?.FailedLoginCount || 0,
      isNoPasswordReset: res?.IsNoPasswordReset ?? false,
      isExpired: res?.IsExpired ?? false,
      isEditMailTextAutomatically: res?.IsEditMailTextAutomatically ?? false,
      isNoExpire: res?.IsNoExpire ?? false,
      isSupportUser: res?.IsSupportUser ?? false,
      isShipTo: res?.IsShipTo ?? false,
      isBillTo: res?.IsBillTo ?? false,
      isVendorLead: res?.IsVendorLead ?? false,
      eMailUser: res?.EMailUser || '',
      phone2: res?.Phone2 || '',
      birthday: res?.Birthday || date.now('date'),
      phone: res?.Phone || '',
      fax: res?.Fax || '',
      lastResult: res?.LastResult || '',
      lastContact: res?.LastContact || date.now('date'),
      title: res?.Title || '',
      comments: res?.Comments || '',
      eMailVerify: res?.EMailVerify || 0,
      eMailVerifyDate: res?.EMailVerifyDate || '',
      LDAPUser: res?.LDAPUser || '',
      datePasswordChanged: res?.DatePasswordChanged || '',
      supervisor: res?.Supervisor_ID?.identifier || '',
      partner: res?.C_BPartner_ID?.identifier || '',
      notificationType: res?.NotificationType?.identifier || '',
      greeting: res?.C_Greeting_ID?.identifier || '',
      partnerLocation: res?.C_BPartner_Location_ID?.identifier || '',
      orgTrx: res?.AD_OrgTrx_ID?.identifier || '',
      job: res?.C_Job_ID?.identifier || '',
      isMenuAutoExpand: res?.IsMenuAutoExpand?.identifier || '',
      mailText: res?.R_DefaultMailText_ID?.identifier || ''
    }
  }
}
onMounted(retrieve)

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

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

<template>
  <Head>
    <Title>View Lead 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="columns is-desktop">
            <div class="column is-half 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="column is-half 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>
          <div class="columns is-desktop">
            <div class="column is-half 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="column is-half 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>
          <div class="columns is-desktop">
            <div class="column is-half 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="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Comments</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.comments }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 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 class="column is-half mb-2 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="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Email</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.eMail }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Title</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.title }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Birthday</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.birthday }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Fax</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.fax }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Phone</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.phone }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>2nd Phone</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.phone2 }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Notification Type</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.notificationType }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Position</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.position }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>EMail User ID</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.eMailUser }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Supervisor</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.supervisor }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>LDAP User Name</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.LDAPUser }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Trx Organization</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.orgTrx }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Auto expand menu</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.isMenuAutoExpand }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Greeting</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.greeting }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Default mail template</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.mailText }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>EMail Verify</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.eMailVerifyDate }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Last Contact</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.lastContact }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Verification Info</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.eMailVerify }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Last Result</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.lastResult }}</div>
            </div>
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Date Password Changed</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.datePasswordChanged }}</div>
            </div>
          </div>
          <div class="columns is-desktop">
            <div class="column is-half mb-2 d-flex gap-10px">
              <div class="w-150px d-flex justify-between gap-10px">
                <strong>Failed Login Count</strong>
                <strong>:</strong>
              </div>
              <div>{{ form.failedLoginCount }}</div>
            </div>
            <div class="column is-half 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.isLocked" class="checkInput" id="isLocked" :checked="form.isLocked" disabled>
                <label class="checkLabel" for="isLocked">Locked</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isNoPasswordReset" class="checkInput" id="isNoPasswordReset" :checked="form.isNoPasswordReset" disabled>
                <label class="checkLabel" for="isNoPasswordReset">No Password Reset</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isExpired" class="checkInput" id="isExpired" :checked="form.isExpired" disabled>
                <label class="checkLabel" for="isExpired">Expired</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isNoExpire" class="checkInput" id="isNoExpire" :checked="form.isNoExpire" disabled>
                <label class="checkLabel" for="isNoExpire">No Expire</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isAddMailTextAutomatically" class="checkInput" id="isAddMailTextAutomatically" :checked="form.isAddMailTextAutomatically" disabled>
                <label class="checkLabel" for="isAddMailTextAutomatically">Add Mail Text Automatically</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isSupportUser" class="checkInput" id="isSupportUser" :checked="form.isSupportUser" disabled>
                <label class="checkLabel" for="isSupportUser">Support User</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isShipTo" class="checkInput" id="isShipTo" :checked="form.isShipTo" disabled>
                <label class="checkLabel" for="isShipTo">Ship Contact</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isBillTo" class="checkInput" id="isBillTo" :checked="form.isBillTo" disabled>
                <label class="checkLabel" for="isBillTo">Invoice Contact</label>
              </div>
              <div class="check">
                <input type="checkbox" v-model="form.isFullBPAccess" class="checkInput" id="isFullBPAccess" :checked="form.isFullBPAccess" disabled>
                <label class="checkLabel" for="isFullBPAccess">Full BP Access</label>
              </div>
            </div>

          </div>

          <div>
            <div class="mb-3">
              <AdminLeadDetailRecord action="view" :info="form" />
            </div>
          </div>

        </div>
      </div>

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