<script lang="ts" setup>
/**
 * @Name: FeatureWordTable.vue
 * @Description: 反查关键词表格
 * @Author: Cheney
 */

import { usePagination } from '/@/utils/usePagination';
import { useElTableData } from '/@/utils/useElTableData';
import * as api from '/@/views/featureWord/queryPage/api';
import emitter from '/@/utils/emitter';


const { tableData, total, currentPage, pageSize, handlePageChange } = usePagination(fetchTableData);
const filter = inject<Ref>('filter');
const loading = ref(false);

emitter.on('QueryPage-query', () => {
  fetchTableData();
});

onBeforeMount(() => {
  pageSize.value = 20;
});

onBeforeUnmount(() => {
  emitter.all.clear();
});

async function fetchTableData() {
  const query = {
    search_term: filter.value.searchTerm,
    marketplace_Ids: filter.value.marketIds,
    report_type: filter.value.reportType,
    date_start: filter.value.reportDate[0],
    date_end: filter.value.reportDate[1],
    page: currentPage.value,
    limit: pageSize.value
  };
  await useElTableData(api.getFeatureWordData, query, tableData, total, loading);
}

function arraySpanMethod({ row, column, rowIndex, columnIndex }) {
  if (columnIndex === 0) { // 只对 "特征词" 列进行处理
    const currentWord = tableData.value[rowIndex].feature_word;
    let rowspan = 1;

    // 计算有多少连续行具有相同的 feature_word, 包括最后一行
    while (rowIndex + rowspan < tableData.value.length &&
    tableData.value[rowIndex + rowspan].feature_word === currentWord) {
      rowspan++;
    }

    if (rowIndex === 0 || tableData.value[rowIndex - 1].feature_word !== currentWord) {
      return [ rowspan, 1 ];
    } else {
      return [ 0, 0 ];
    }
  }
  return [ 1, 1 ]; // 对其他列不做特殊处理
}
</script>

<template>
  <el-card shadow="hover" style="border: none; margin-bottom: 10px">
    <el-descriptions title="/ 反查关键词 /"></el-descriptions>
    <el-card shadow="never">
      <el-table v-loading="loading" :data="tableData" :span-method="arraySpanMethod" height="800" stripe style="width: 100%">
        <el-table-column align="center" label="特征词" prop="feature_word">
          <template #default="{ row }">
            <span class="font-semibold">{{ row.feature_word }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="关键词" prop="searchTerm">
          <template #default="{ row }">
            <el-tooltip :content="row.searchTerm" :show-after="500" effect="dark" placement="top-start">
              <span class="font-semibold line-clamp-1 text-ellipsis">{{ row.searchTerm }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column align="center" label="权重值" prop="weight_value">
          <template #default="{ row }">
            <span class="font-semibold">{{ row.weight_value }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="搜索频次" prop="searchFrequencyRank">
          <template #default="{ row }">
            <span class="font-semibold">{{ row.searchFrequencyRank }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="点击分享" prop="clickShare">
          <template #default="{ row }">
            <span class="font-semibold">{{ row.clickShare }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="转化分享" prop="conversionShare">
          <template #default="{ row }">
            <span class="font-semibold">{{ row.conversionShare }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt-3.5 flex justify-end">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 50, 100, 200]"
            :total="total"
            layout="sizes, prev, pager, next, total"
            @change="handlePageChange"/>
      </div>
    </el-card>
  </el-card>
</template>

<style scoped>

</style>
