hideLoading()

async function fetchEntry(){  // This can be condensed
  showLoading()
  var name = document.getElementById('name')
  var crs = document.getElementById('3alpha')
  var nlc = document.getElementById('nlc')
  var tiploc = document.getElementById('tiploc')
  var stanox = document.getElementById('stanox')

  var values = {
    name: name.value,
    crs: crs.value,
    nlc: nlc.value,
    tiploc: tiploc.value,
    stanox: stanox.value
  }
  parseData(values)
}

async function parseData(values){
  vibe()
  let data
  if (values.crs != ''){
    setLoadingDesc(`Searching\n${values.crs.toUpperCase()}`)
    data = await getData('crs', values.crs)
  } else if (values.nlc != ''){
    setLoadingDesc(`Searching\n${values.nlc.toUpperCase()}`)
    data = await getData('nlc', values.nlc)
  } else if (values.tiploc != ''){
    setLoadingDesc(`Searching\n${values.tiploc.toUpperCase()}`)
    data = await getData('tiploc', values.tiploc)
  } else if (values.stanox != ''){
    setLoadingDesc(`Searching\n${values.stanox.toUpperCase()}`)
    data = await getData('stanox', values.stanox)
  } else if (values.name != ''){
    setLoadingDesc(`Searching\n${values.name}`)
    data = await getData('name', values.name)
  } else {
    log('find-code.parseData: No data entered', 'WARN')
    await clearForm()
    document.getElementById('name').value = 'No data entered'
    vibe('err')
    hideLoading()
    return
  }
  displayData(data)
}

async function getData(type, value){
  log(`find-code.getData: Looking for: ${type} '${value}'`, 'INFO')
  try {
    var url = `${window.location.origin}/api/v1/find/${type}/${value}`
    var resp = await fetch(url)
    return await resp.json()
  } catch (err) {
    log(`find-code.getData: Error getting data: ${err}`, 'WARN')
    vibe('err')
    return ''
  }
}

async function displayData(data){
  hideLoading()
  if (data.status === 'failed' || data == ''){
    log('find-code.displayData: Unable to find data', 'WARN')
    clearForm()
    document.getElementById('name').value = 'Not Found'
  } else {
    log('find-code.displayData: Inserting data', 'INFO')
    vibe('ok')
    try {
      document.getElementById('name').value = data['0']['NLCDESC']
    } catch (err) {}
    try {
      document.getElementById('3alpha').value = data['0']['3ALPHA']
    } catch (err) {}
    try {
      document.getElementById('nlc').value = data['0']['NLC']
    } catch (err) {}
    try {
      document.getElementById('tiploc').value = data['0']['TIPLOC']
    } catch (err) {}
    try {
      document.getElementById('stanox').value = data['0']['STANOX']
    } catch (err) {}
  }
}

async function clearForm(){
  document.getElementById('name').value = ''
  document.getElementById('3alpha').value = ''
  document.getElementById('nlc').value = ''
  document.getElementById('tiploc').value = ''
  document.getElementById('stanox').value = ''
  vibe('ok')
  hideLoading()
}