<template>
  <div class="lofty-home-valuation-step1">
    <div class="address-body">
      <div class="address-input-container">
        <input class="address-input"
               v-model="location"
               type="text"
               :placeholder="step1.placeholder"
               @mousedown.stop
               @dblclick.stop
               @focus.stop="handleMutual"
               @touchstart.stop="handleMutual"
               @input.stop="searchSuggest"
        />
        <div v-show="showError" class="error-tip">A valid address is required to continue</div>
        <i class="lofty_iconfont lofty-icon-close" v-if="location" @click="clearLocation"></i>
        <ul v-show="suggestList.length>0 && showSuggest" class="autocomplete">
          <li v-for="address in suggestList" :key="address" class="item" @click="changeLocation(address)">
            <i class="lofty_iconfont lofty-icon-location_02"></i>
            <span v-html="address"></span>
          </li>
        </ul>
      </div>
      <div v-if="step1.showUnit" class="shu-line"></div>
      <input v-if="step1.showUnit" v-model="unit" class="unit-input" placeholder="Unit"/>
      <div class="lofty-btn lofty-next-btn" @click="submit">{{step1.buttonText}}</div>
    </div>
  </div>
</template>

<script setup>
import util from "@/util/index.js";
import http from "@/api/index.js";

const { step1 } = defineProps({ step1: Object })
const emits = defineEmits(['next'])
const location = ref('')
const unit = ref('')
const showSuggest = ref(false)
const suggestList = ref([])

const searchSuggest = util.debounce(async function (e){
  if(!e.target.value) {
    return
  }
  const data = {
    query: e.target.value,
    country: 'usa',
    beginHighlight: encodeURIComponent('<mark>'),
    endHighlight: encodeURIComponent('</mark>')
  }
  const res = await http.get('/search/home-j/here-api/autoSuggest', { data })
  const list = res.data
  const result = []
  for (let i = 0, k = 0, len = list.length; k < 5 && i < len ; i++) {
    let address = formatAddress(list[i])
    if(address && result.indexOf(address) === -1) {
      result.push(address)
      k++
    }
  }
  suggestList.value = result
  showSuggest.value = true
})

const showError = ref(false)
function submit() {
  if(!location.value) {
    showError.value = true
    setTimeout(()=>{showError.value = false}, 3000)
    return
  }
  emits('next', location.value, unit.value)
}

function changeLocation(address) {
  location.value = address
  showSuggest.value = false
}

function formatAddress(item) {
  let address = item.address,
      detailAddr = getAddressItem(address, ['houseNumber', 'street']) + getAddressItem(address, ['city']);
  return detailAddr
      ? decodeURIComponent(
          detailAddr +
          getAddressItem(address, ['state', 'postalCode'], false)
      )
      : null;
}

function getAddressItem(address, names, comma) {
  let addr = [];
  let lastItem = '';
  for (let i = 0, len = names.length; i < len; i++) {
    let item = address[names[i]];
    if (item && lastItem !== item) {
      addr.push((lastItem = item));
    }
  }
  return addr.length ? addr.join(' ') + (comma === false ? '' : ', ') : '';
}
function handleMutual() {
  showSuggest.value = false;
}

function clearLocation() {
  location.value = '';
  showSuggest.value = false
}
</script>
<style lang="scss">
.lofty-home-valuation-step1 {
  position: relative;
  z-index: 12;
  color: #fff;
  margin-top: 50px;
  .address-body {
    margin-top: 30px;
    padding: 0 50px;
    display: flex;
    height: 60px;
    position: relative;
    @include media-600 {
      flex-wrap: wrap;
      padding: 0 10px;
      height: 50px;
    }
    .address-input-container {
      width: 100px;
      height: 100%;
      flex: 1;
      position: relative;
    }
    .address-input {
      width: 100%;
      height: 100%;
      outline: 0;
      border-radius: 0;
      padding-left: 20px;
      font-size: 16px;
      color: #111;
      border: none;
      @include media-600 {
        padding-right: 35px;
        padding-left: 10px;
      }
      &::placeholder {
        font-size: 16px;
        font-family: var(--lofty-font-regular);
        color: rgb(168, 168, 168);
        @include media-600 {
          font-size: 14px;
        }
      }
    }
    .lofty_iconfont.lofty-icon-close {
      position: absolute;
      font-size: 14px;
      color: #a0a3af;
      right: 16px;
      top: 20px;
      cursor: pointer;
      @include media-600 {
        top: 15px;
      }
    }
    .autocomplete {
      position: absolute;
      margin-top: -10px;
      background: #fff;
      padding: 0;
      list-style: none;
      z-index: 3000;
      width: 100%;
      .item {
        padding: 0 10px;
        list-style: none;
        font-size: 14px;
        font-weight: 400;
        line-height: 44px;
        color: #111;
        cursor: pointer;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        .lofty-icon-location_02 {
          font-size: 12px;
          margin-right: 4px;
          color: #a8a8a8;
        }
        mark {
          font-size: 13px;
          font-weight: bold;
          color: #000;
          background: none;
        }
        &.selected {
          background-color: #fafafa;
        }
        &:hover {
          background-color: #fafafa;
        }
      }
    }
    .shu-line {
      width: 1px;
      background: #fff;
      @include media-600 {
        height: 50px;
      }
      &::before {
        display: block;
        content: '';
        width: 1px;
        height: 20px;
        background: #dadada;
        position: relative;
        top: 20px;
        @include media-600 {
          top: 15px;
        }
      }
    }
    .unit-input {
      height: 100%;
      outline: 0;
      width: 80px;
      border-radius: 0;
      border: none;
      padding-left: 10px;
      @include media-600 {
        width: 60px;
      }
    }
    .lofty-next-btn {
      line-height: 60px;
      min-width: 200px;
      @include media-600 {
        margin-top: 10px;
        width: 100%;
        height: 50px;
        line-height: 50px;
      }
    }
  }
  .error-tip {
    color: rgba(240, 69, 76, 1);
    @include media-600 {
      font-size: 14px;
    }
  }
}
</style>