import React, { useEffect, useState } from 'react';
import { Table, message } from 'antd';
import { getRiskList } from '../services'
import { useIntl } from 'umi';

const RiskList = ({id}:{id:any}) => {
  const intl = useIntl();
  const columns = [
    { title: intl.formatMessage({id: 'component.globalModal.Risktype'}), dataIndex: 'type', key: 'type' },
    { title: intl.formatMessage({id: 'component.globalModal.level'}), dataIndex: 'level', key: 'level' },
    { title: intl.formatMessage({id: 'component.globalModal.desc'}), dataIndex: 'desc', key: 'desc' },
  ]
  //合规风险
  const [dataList, setDataList] = useState<any[]>([])
  //加载
  const [loading, setLoading] = useState(false);
  //分页
  const [pagination, setPagination] = useState({ current: 1, pageSize: 10, total: 0, });

  //数据渲染
  const getList = async (page = 1, pageSize = 10) => {
    setLoading(true);
    try {
      const { code, data, total } = await getRiskList({ pageNum: page, pageSize });
      if (code === 200) {
        setDataList(data);
        setPagination({ current: page, pageSize, total });
      } else {
        message.error(intl.formatMessage({id: 'component.globalModal.fetchRiskFail'}));
      }
    } catch (error) {
      message.error(intl.formatMessage({id: 'component.globalModal.apiError'}));
    } finally {
      setLoading(false);
    }
  };
  //分页
  const handleTableChange = (paginationInfo: any) => {
    getList(paginationInfo.current, paginationInfo.pageSize);
  };
  //初始化
  useEffect(() => {
    getList();
  }, [id]);

  return (
    <>
      <Table
        title={() => <div style={{ fontWeight: 'bold', fontSize: 16, textIndent: '-16px' }}>{intl.formatMessage({id: 'component.globalModal.riskList'})}</div>}
        dataSource={dataList}
        loading={loading}
        columns={columns}
        pagination={{
          current: pagination.current,
          pageSize: pagination.pageSize,
          total: pagination.total,
          showSizeChanger: true,
          onChange: (page, pageSize = 10) => getList(page, pageSize),
        }}
        onChange={handleTableChange}
      />
    </>
  );
};

export default RiskList;
