import { createFromIconfontCN } from '@ant-design/icons';

/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2025-02-14 17:50:16
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2025-02-27 14:42:02
 * @FilePath: /MediResourceManaSys/src/pages/dataFilling/fillingMana/generateTableData.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { Drawer, Input, message, Popconfirm, Popover, Switch, Tooltip } from 'antd';
import AuthHisContent from './authHisttory';
import { ProColumns } from '@ant-design/pro-table';

type HistoryItem = {
  value: number;
  createTime: string;
};

type ItemDic = {
  code: string;
  name: string;
  type: number;
  unit: string;
  status: number;
  periodType: number;
  description: string;
  dimensionType: number;
};

type ItemData = {
  code: string;
  value: any;
  history?: HistoryItem[];
  itemDic?: ItemDic;
  [key: string]: any;
};

type ItemTitle = {
  type: number;
  code: string;
  name: string;
};

const IconFont = createFromIconfontCN({
  scriptUrl: '',
});

export default function generateTableData(itemTitles: ItemTitle[], itemDatas: ItemData[][], hideIndexs?: number[]) {
  const columns: any[] = itemTitles.map((title, index) => ({
    title: title.name,
    dataIndex: title.code,
    key: title.code,
    width: index > 0 ? 120 : 320,
    valueType: title.type == 2 ? 'digit' : 'text',
    editable: title.type == 2,
    ellipsis: true,
    hideInTable: hideIndexs?.includes(index),
    columnType: 1,
    render: (text: any, record: any) => {
      let history = record[`${title.code}_history`] ? record[`${title.code}_history`] : [];
      let content = <AuthHisContent hisList={[...history]} />;

      return (
        <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center' }}>
          <span>{text}</span>
          {history.length > 0 && (
            <Popover className="qualificationAuth-popover" overlayInnerStyle={{ borderRadius: 4 }} content={content} title={false}>
              <IconFont type={'icon-qingliangtishi'} style={{ marginLeft: 10 }} />
            </Popover>
          )}
        </div>
      ); // 返回 text 对应的数据
    },
  }));

  const dataSource = itemDatas.map((row, rowIndex) => {
    // 用 Record<string, any> 类型来解决动态属性问题
    const rowData: Record<string, any> = { id: `row_${rowIndex}` };

    row.forEach((item) => {
      // 处理每个对象的 `value`
      rowData[item.code] = item.value;

      // 遍历当前 item 的所有属性
      Object.keys(item).forEach((key) => {
        if (key !== 'code' && key !== 'value') {
          // 排除 code 和 value
          rowData[`${item.code}_${key}`] = item[key]; // 将其他属性添加到 rowData 中
        }
      });
    });

    return rowData;
  });

  return { columns, dataSource };
}
