<template>
  <div>空间分析</div>
  <div class="analyze-data-container">
    <div class="item">
      <div class="item-label">行政镇(个)</div>
      <div class="item-value">{{ validateNum(analysisData.townCount) }}</div>
    </div>
    <div class="item">
      <div class="item-label">行政村(个)</div>
      <div class="item-value">{{ validateNum(analysisData.villageCount) }}</div>
    </div>
    <div class="item">
      <div class="item-label">面积(km²)</div>
      <div class="item-value">{{ validateNum(analysisData.areaSize) }}</div>
    </div>
    <div class="item">
      <div class="item-label">常住人口(万)</div>
      <div class="item-value">{{ validateNum(analysisData.populationSize) }}</div>
    </div>
    <div class="item">
      <div class="item-label">GDP(万元)</div>
      <div class="item-value">{{ validateNum(analysisData.GDP) }}</div>
    </div>
    <div class="flex" style="margin: 20px 0; width: 100%">
      <el-input v-model="keyword" size="large" style="flex: 1" />
    </div>
    <div class="flex">
      <div v-for="(item, index) in filteredList" :key="index" class="item2">{{ item.name + '(' + item.list.length + ')' }}</div>
    </div>
  </div>
  <DrawTools @handleAnalysisData="handleAnalysisData" />
</template>

<script lang="ts" setup name="AnalyzeDataDialog">
import DrawTools from './DrawTools.vue';
import { validateNum } from '@/utils/ruoyi';
import { getSpatialAnalysis } from '@/api/globalMap';
import { deepClone } from '@/utils';

const emits = defineEmits(['handleMenu']);
const keyword = ref('');
let analysisData = ref({
  townCount: '',
  villageCount: '',
  areaSize: '',
  populationSize: '',
  GDP: '',
  list: []
});
const filteredList = computed(() => {
  const data = deepClone(analysisData.value.list);
  return data.filter((item) => {
    if (!!keyword.value) {
      // 模糊搜索逻辑，这里简单使用 includes 进行包含关系判断
      let flag = item.name.toLowerCase().includes(keyword.value.toLowerCase());
      if (flag) {
        return true;
      } else {
        let res = filteredSubItems(item);
        if (res && res.length > 0) {
          item.list = res;
          return true;
        } else {
          return false;
        }
      }
    } else {
      return true;
    }
  });
});
const filteredSubItems = (parentItem) => {
  return parentItem.list.filter((subItem) => {
    return subItem.name.toLowerCase().includes(keyword.value.toLowerCase());
  });
};
const location = ref([]);
watch(
  () => location,
  () => {
    getSpatialAnalysis(location.value).then((res) => {
      if (res.data && res.data.list) {
        const list = [];
        res.data.list.forEach((item) => {
          if (item.num > 0) {
            list.push(item);
          }
        });
        res.data.list = list;
      }
      analysisData.value = res.data;
    });
  },
  {
    immediate: true,
    deep: true
  }
);

const handleAnalysisData = (data) => {
  location.value = data;
  emits('handleMenu', '空间分析');
};
</script>

<style lang="scss" scoped>
.analyze-data-container {
  width: 100%;
  font-size: 16px;
  padding: 15px;
  display: flex;
  flex-wrap: wrap;
  .item {
    width: calc(33.333333%);
    height: 100px;
    background-color: #fff;
    border-left: 1px solid #000;
    border-top: 1px solid #000;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 36px;
    &:nth-child(3),
    &:nth-child(4),
    &:nth-child(5) {
      border-bottom: 1px solid #000;
    }
    &:nth-child(5) {
      border-right: 1px solid #000;
    }
    .item-label {
      margin-bottom: 18px;
    }
  }
}
.item2 {
  background-color: #fff;
  font-size: 36px;
  padding: 10px 20px;
  margin-right: 20px;
}
</style>
