<template>
  <a-table
    :columns="columns"
    :data-source="data"
    bordered
    style="width: 100%; height: 90%"
    :pagination="false"
    :scroll="{ x: '2000' }" />
  <a-pagination
    :total="total"
    :show-total="(total, range) => ` 共 ${total} 条`"
    show-size-changer
    show-quick-jumper
    @change="onChange"
    style="display: flex; justify-content: right; margin-top: 10px; margin-right: 30px" />
</template>

<script lang="ts">
  import { defineComponent, watch, inject, ref, onMounted } from 'vue';
  import type { TableColumnType } from 'ant-design-vue';
  import { Pagination } from 'ant-design-vue';

  export default defineComponent({
    // name: 'EnvironmentTable',
    components: {
      'a-pagination': Pagination,
    },
    setup() {
      const total = ref<number>();
      // 分页后的展示数据
      let data = ref<any[]>([]);
      // 原始数据
      let dataList = ref<any[]>([]);
      let columns = ref<TableColumnType[]>([]);

      interface PageData {
        // 图表 表格数据
        graphTableList: any[];
        // 图表 表格表头
        graphTableColumns: any[];
        // 图表 图表数据
        graphGraphList: any[];
        // 分析 表格数据
        analysisTableList: any[];
        // 分析 图表数据
        analysisGraphList: any[];
      }
      const pageData = inject<PageData>('pageData');

      if (!pageData) {
        throw new Error('pageData is not provided');
      }
      // 监听 pageData 的变化
      watch(
        () => pageData as PageData,
        (_newValue, _oldValue) => {
          // data.value = pageData.graphTableList;

          // let columnA: any[] = [...column];
          // columnA.push(...pageData.graphTableColumns);
          // columns.value = columnA;
          // // pageData.graphList;
          // // 执行你的逻辑代码
          init();
        },
        { deep: true },
      );

      const getRowSpan = (dataIndex: string, record: any, data: any, dependents: string[] = []) => {
        let rowSpan = 1;
        for (let i = data.indexOf(record) + 1; i < data.length; i++) {
          let shouldMerge = true;
          for (const dependent of dependents) {
            if (data[i][dependent] !== record[dependent]) {
              shouldMerge = false;
              break;
            }
          }
          if (shouldMerge && data[i][dataIndex] === record[dataIndex]) {
            rowSpan++;
          } else {
            break;
          }
        }
        return rowSpan;
      };

      const column: TableColumnType[] = [
        {
          title: '设备/组名',
          dataIndex: 'name',
          customCell: (record, rowIndex) => {
            if (rowIndex == undefined) {
              return {
                rowSpan: 0,
                colSpan: 0,
              };
            }
            const rowSpan = getRowSpan('name', record, data.value);
            if (rowIndex != 0 && data.value[rowIndex - 1].name == record.name) {
              return {
                rowSpan: 0,
                colSpan: 0,
              };
            }
            return {
              rowSpan: rowSpan,
            };
          },
        },
        {
          title: '参数名称',
          dataIndex: 'selectedValueName',
          customCell: (record, rowIndex) => {
            if (rowIndex == undefined) {
              return {
                rowSpan: 0,
                colSpan: 0,
              };
            }
            const rowSpan = getRowSpan('selectedValueName', record, data.value, ['name']);
            if (rowIndex != 0 && data.value[rowIndex - 1].name == record.name) {
              return {
                rowSpan: 0,
                colSpan: 0,
              };
            }
            return {
              rowSpan: rowSpan,
            };
          },
        },
      ];

      // 分页器
      const onChange = (pageNumber: number, size: number) => {
        const start = (pageNumber - 1) * size;
        const end = start + size;
        data.value = dataList.value.slice(start, end);
      };
      const init = () => {
        dataList.value = pageData.graphTableList;

        let columnA: any[] = [...column];
        let columnB: any[] = [];
        pageData.graphTableColumns.forEach((item) => {
          columnB.push({ title: item, dataIndex: item });
        });
        columnA.push(...columnB);
        columns.value = columnA;
        total.value = dataList.value.length;
        onChange(1, 10);
      };
      onMounted(() => {
        init();
        // data.value = pageData.graphTableList;
        // let columnA: any[] = [...column];
        // columnA.push(...pageData.graphTableColumns);
        // columns.value = columnA;
        // index.value = 0;
      });
      return {
        data,
        column,
        columns,
        total,
        onChange,
      };
    },
  });
</script>

<style lang="less" scoped></style>
