<template>
  <div class="menu-content">
    <div class="gradient-text common-dialog-title2">空间分析</div>
    <div class="analyze-data-container">
      <div class="item" @click="handleShowDetail">
        <div class="item-label">行政镇(个)</div>
        <div class="item-value">{{ validateNum(analysisData.townCount) }}</div>
      </div>
      <div class="item" @click="handleShowDetail">
        <div class="item-label">行政村(个)</div>
        <div class="item-value">{{ validateNum(analysisData.villageCount) }}</div>
      </div>
      <div class="item" @click="handleShowDetail">
        <div class="item-label">面积(km²)</div>
        <div class="item-value">{{ validateNum(analysisData.areaSize) }}</div>
      </div>
      <div class="item" @click="handleShowDetail">
        <div class="item-label">常住人口(万)</div>
        <div class="item-value">{{ validateNum(analysisData.populationSize) }}</div>
      </div>
      <div class="item" @click="handleShowDetail">
        <div class="item-label">GDP(万元)</div>
        <div class="item-value">{{ validateNum(analysisData.GDP) }}</div>
      </div>
      <div class="flex" style="margin: 15px 0 10px; width: 100%">
        <el-input v-model="keyword" class="custom-input2" placeholder="请输入" size="large" style="flex: 1" />
      </div>
      <div class="flex" style="flex-wrap: wrap">
        <div v-for="(item, index) in filteredList" :key="index" class="item2">{{ item.name + '(' + item.list.length + ')' }}</div>
      </div>
    </div>
    <Dialog v-if="showDetail" v-model="showDetail" title="空间分析" height="760px" hide-footer>
      <div class="common-btn-primary export-btn" @click="exportData">导出</div>
      <div class="common-table">
        <div class="table-header">
          <div class="td">序号</div>
          <div class="td">行政镇</div>
          <div class="td">行政村</div>
          <div class="td">常住人口(万)</div>
          <div class="td">面积(km2)</div>
          <div class="td">GDP(亿元)</div>
        </div>
        <div v-for="(item, index) in analysisData.townData" :key="index" class="tr">
          <div class="td">{{ index + 1 }}</div>
          <div class="td">{{ item.townName }}</div>
          <div class="td">{{ item.villageName ? item.villageName : '-' }}</div>
          <div class="td">{{ item.populationSize }}</div>
          <div class="td">{{ item.areaSize }}</div>
          <div class="td">{{ item.GDP }}</div>
        </div>
      </div>
    </Dialog>
  </div>
</template>

<script lang="ts" setup name="AnalyzeDataDialog">
import { validateNum } from '@/utils/ruoyi';
import { getSpatialAnalysis } from '@/api/globalMap';
import { deepClone } from '@/utils';
import * as XLSX from 'xlsx';

const props = defineProps({
  updateLocation: []
});
const emits = defineEmits(['handleMenu']);
const keyword = ref('');
let analysisData = ref({
  townCount: '',
  townData: [],
  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(
  () => props.updateLocation,
  () => {
    location.value = props.updateLocation;
  },
  {
    immediate: true,
    deep: true
  }
);
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;
      }
      if (res.data && res.data.townData) {
        const data = [];
        res.data.townData.forEach((item) => {
          data.push(item);
          item.children.forEach((item2) => {
            const obj = deepClone(item2);
            obj.townName = item.townName;
            data.push(obj);
          });
          delete item.children;
        });
        res.data.townData = data;
      }
      analysisData.value = res.data;
    });
  },
  {
    immediate: true,
    deep: true
  }
);
let showDetail = ref(false);
const handleShowDetail = () => {
  showDetail.value = true;
};
// 导出
const exportData = () => {
  let jsonData = [];
  const headerMap = {
    townName: '行政镇',
    villageName: '行政村',
    populationSize: '常住人口(万)',
    areaSize: '面积(km2)',
    GDP: 'GDP(亿元)'
  };
  const orderedKeys = Object.keys(headerMap);
  analysisData.value.townData.forEach((item) => {
    const newItem = {};
    Object.keys(item).forEach((key) => {
      if (headerMap[key]) {
        newItem[headerMap[key]] = item[key];
      }
    });
    jsonData.push(newItem);
  });
  const worksheet = XLSX.utils.json_to_sheet(jsonData, {
    header: orderedKeys.map((key) => headerMap[key]) // 按 headerMap 顺序传递中文表头
  });
  // 创建工作簿并添加工作表
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
  // 导出文件
  XLSX.writeFile(workbook, '空间分析数据.xlsx');
};
</script>

<style lang="scss" scoped>
.menu-content {
  width: 574px;
  height: 581px;
  background: url('@/assets/images/map/rightMenu/content.png') no-repeat;
  background-size: 100% 100%;
  padding: 60px 10px 10px 15px;
  font-size: 14px;
  position: relative;
  color: #ffffff;
}
.analyze-data-container {
  width: 100%;
  font-size: 16px;
  padding: 10px 0;
  display: flex;
  flex-wrap: wrap;
  .item {
    width: 130px;
    height: 60px;
    background: url('@/assets/images/map/rightMenu/ironTower/boxBg.png') no-repeat;
    background-size: 100% 100%;
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    margin-right: 9px;
    cursor: pointer;
    &:nth-child(4) {
      margin-right: 0;
    }
    &:nth-child(5) {
      margin-top: 10px;
    }
    .item-label {
      margin-bottom: 5px;
    }
  }
}
.item2 {
  background-color: #0b184b;
  font-size: 14px;
  padding: 5px 10px;
  margin-right: 10px;
  color: #ffffff;
  border: 2px solid #247dff;
  border-radius: 4px;
  margin-top: 5px;
}
.export-btn {
  position: absolute;
  right: 20px;
  top: 55px;
}
</style>
