import React from 'react';
import { ReactWidget } from '@jupyterlab/apputils';
import { Widget } from '@lumino/widgets';
import { DagDataView } from './dataview';
import { tableContent } from './api/datamanage';
import { message } from 'antd';

class DataTableWidget extends ReactWidget {
  constructor(table: any, options?: Widget.IOptions) {
    super(options);
    void this._requestData(table, 1, 100);
    this._table = table;
  }

  private _requestData = async (table: any, page: number, size: number) => {
    const { data } = await tableContent({
      table_name: table.table_name,
      page,
      size
    });
    if (data.code === 200) {
      const { content, header, total } = data.data;
      this._data = { fields: header, dataList: content, total };
    } else {
      void message.error(data.msg);
    }
    this.update();
  };

  render(): JSX.Element {
    return (
      <DagDataView
        data={this._data}
        info={this._table}
        changePage={(page, size) => this._requestData(this._table, page, size)}
      />
    );
  }

  private _data: any | null = null;
  private _table: any;
}

export default DataTableWidget;
