<template>
  <div class="web-home">
    <div class="title">
      <img src="@/assets/images/web/web-home_title.png"/>
    </div>
    <div class="area">
      <div class="selected">
        <div class="label">搜索范围：</div>
        <div class="value">
          <template v-if="searchAreaCpt.text">
            {{searchAreaCpt.text}}
          </template>
          <template v-else>
            <template v-for="item in searchAreaCpt.arr">
              <div>{{item.indexName}}</div>
            </template>
          </template>
        </div>
      </div>
      <div class="search-input">
        <div class="left-select __hover" @click="state.showArea = true">
          搜索范围<SvgIcon name="arrow_1" rotate="90" size="14" color="var(--cus-text-color-3)"/>
        </div>
        <el-input v-model="state.searchText" placeholder="请输入关键字进行查询" @keyup.enter="toList(state.searchText)"/>
        <div class="right-icon __hover" @click="toList(state.searchText)">
          <SvgIcon name="search_1" color="var(--cus-main-color)" size="40"/>
        </div>
      </div>
    </div>
    <div class="history" v-if="state.historyList?.length > 0">
      <div class="label">搜索记录</div>
      <div class="result">
        <template v-for="(item, index) in state.historyList">
          <span class="__hover" @click="toList(item, true)">{{index + 1}}.{{item}}</span>
        </template>
      </div>
    </div>
    <CusDialog
      :show="state.showArea"
      @onClose="$emit('update:show', false)"
      width="1000px"
      height="auto"
      submit-text="关闭"
      :show-close="false"
      @onSubmit="state.showArea = false"
    >
      <CusTab :tabs="state.areaList" type="type1" v-model:param="state.areaTab" label-key="treeName" value-key="treeId"/>
      <div class="index-list">
        <div class="all">
          <div class="__check" :class="{active: indexTabAllCpt}" @click="onIndexTabAll">全选</div>
        </div>
        <div class="list">
          <template v-for="(item, index) in indexListCpt">
            <div class="list-item">
              <div class="__check" :class="{active: item.__select}" @click="item.__select = !item.__select">{{ item.indexName }}</div>
            </div>
          </template>
        </div>
      </div>
    </CusDialog>
  </div>
</template>

<script setup lang="ts">
import {computed, getCurrentInstance, onMounted, reactive} from "vue";
import router from "@/router";
import {ElMessage} from "element-plus";
import {useWebStore} from "@/stores";

const {proxy} = getCurrentInstance()
const WebStore = useWebStore()
const state: any = reactive({
  searchText: '',
  historyList: [],
  areaList: [],
  showArea: false,
  areaTab: ''
})
const indexListCpt = computed(() => {
  return state.areaList.filter(v => v.treeId === state.areaTab)?.[0]?.children || []
})
const indexTabAllCpt = computed(() => {
  return indexListCpt.value.every(v => v.__select)
})
const searchAreaCpt = computed(() => {
  const obj = {
    text: '',
    arr: []
  }
  let i = 0
  state.areaList.forEach(v => {
    v.children.forEach(c => {
      i++
      if (c.__select) {
        obj.arr.push(c)
      }
    })
  })
  if (i === obj.arr.length) {
    obj.arr = []
  }
  if (obj.arr.length === 0) {
    obj.text = '全部'
  }
  return obj
})
const initHistory = () => {
  proxy.$api.mockGetSearchHistory().then(res => {
    state.historyList = res.data
  })
}
const initArea = () => {
  WebStore.getSearchAreaTree().then(res => {
    state.areaList = JSON.parse(JSON.stringify(res))
    state.areaTab = state.areaList[0].treeId
  })
}
const onIndexTabAll = () => {
  const flag = JSON.parse(JSON.stringify(indexTabAllCpt.value))
  indexListCpt.value.forEach(v => {
    v.__select = !flag
  })
}
const toList = (text, isAll = false) => {
  if (text) {
    const routerUrl = router.resolve({
      name: '4f6dd2ea-7c0a-4923-9a57-932ef42235f6',
      query: {
        text,
        index: isAll ? '' : searchAreaCpt.value.arr.map(v => v.indexKey).join(',')
      }
    });
    window.open(routerUrl.href, "_blank");
  } else {
    ElMessage({
      message: '请输入关键字进行查询！',
      grouping: true,
      type: 'warning',
    })
  }
}
onMounted(() => {
  initHistory()
  initArea()
})
</script>

<style lang="scss" scoped>
.web-home {
  width: 100%;
  height: 100%;
  background-image: url("@/assets/images/web/web-home_bg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 40px;
  .area {
    width: 1000px;
    .selected {
      display: flex;
      line-height: 30px;
      .label {
        font-weight: 500;
        font-size: 16px;
        color: var(--cus-main-color);
        padding-left: 16px;
      }
      .value {
        flex: 1;
        color: var(--cus-text-color-2);
        display: flex;
        flex-wrap: wrap;
        column-gap: 20px;
      }
    }
    .search-input {
      margin-top: 10px;
      display: flex;
      align-items: center;
      width: 100%;
      height: 60px;
      background: rgba(255,255,255,0.9);
      box-shadow: 0px 0px 2px 0px rgba(167,220,255,0.5);
      border-radius: 60px;
      border: 1px solid var(--cus-main-color);
      .left-select {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 184px;
        gap: 17px;
        color: var(--cus-text-color-4);
        font-size: 18px;
        position: relative;
        &:after {
          content: '';
          position: absolute;
          right: 0;
          height: 40px;
          width: 1px;
          background-color: var(--cus-main-color);
        }
      }
      :deep(.el-input) {
        font-size: 18px;
        color: var(--cus-text-color-2);
        .el-input__wrapper {
          box-shadow: none !important;
          background-color: transparent;
          .el-input__inner {
            &::placeholder {
              color: var(--cus-text-color-4);
            }
          }
        }
      }
      .right-icon {
        margin-right: 20px;
      }
    }
  }
  .history {
    width: 1000px;
    .label {
      font-weight: 500;
      font-size: 16px;
      color: var(--cus-main-color);
      padding-left: 16px;
    }
    .result {
      margin-top: 10px;
      width: 100%;
      padding: 16px;
      background: rgba(255,255,255,0.8);
      box-shadow: 0px 4px 4px 0px rgba(255,255,255,0.15);
      border-radius: 8px;
      font-weight: 400;
      font-size: 16px;
      color: var(--cus-text-color-2);
      display: flex;
      flex-wrap: wrap;
      gap: 10px 32px;
    }
  }
}
.cus-tab {
  padding: 30px 0 22px 28px;
  :deep(.cus-tab-item::after) {
    bottom: -23px !important;
  }
}
.index-list {
  padding: 20px 28px 0;
  .list {
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 30px;
  }
}
</style>
