<script lang="ts" setup>
/**
 * @Name: WeightTable.vue
 * @Description: 特征词与权重表格
 * @Author: Cheney
 */

import { usePagination } from '/@/utils/usePagination';
import * as api from './api';
import emitter from '/@/utils/emitter';
import { useElTableData } from '/@/utils/useElTableData';
import FileUploader from '/@/components/FileUploader/index.vue';
import { Upload } from '@element-plus/icons-vue';


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 = {
    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
  };
  const { success, data } = await useElTableData(api.getTableData, query, tableData, total, loading);
  tableData.value = data || [];
  if (!success) {
    return;
  } else if (data && data.length > 0) {
    tableData.value = tableData.value.flatMap(obj =>
        Object.entries(obj).map(([ keyword, weight ]) => ({ keyword, weight }))
    );
  }
}

function handleUploadSuccess() {
  console.log('success=> ');
}

function handleUploadError() {
  console.log('Error=> ');
}
</script>

<template>
  <el-card shadow="hover" style="border: none; margin-bottom: 10px">
    <el-descriptions title="/ 特征词与权重 /">
      <template #extra>
        <FileUploader
            :api="api.uploadFile"
            :icon="Upload"
            plain
            round
            type="warning"
            :showFileList="false"
            @upload-success="handleUploadSuccess"
            @upload-error="handleUploadError"
        >文件上传
        </FileUploader>
      </template>
    </el-descriptions>
    <el-card shadow="never">
      <el-table v-loading="loading" :data="tableData" height="600" style="width: 100%">
        <el-table-column align="center" type="index" width="60">
          <template #header>
            <span>序号</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="关键词" prop="keyword">
          <template #default="{ row }">
            <span class="font-semibold">{{ row.keyword }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="权重值" prop="weight">
          <template #default="{ row }">
            <span class="font-semibold">{{ row.weight }}</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>
