import React, { useState, useEffect } from 'react';
import { Table, Button, message } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import type { IntlShape } from 'react-intl';
import { tianyanchaIc } from '../services';
import { useIntl } from 'umi';
interface TianyanchaInfo {
  base: string;               // 省份简称
  name: string;               // 企业名称
  legalPersonName?: string;   // 法人
  legalPersonType?: string;   // 法人类型，1 人，2 公司
  regNumber?: string;         // 注册号
  industry?: string;          // 行业
  companyOrgType?: string;    // 企业类型
  regLocation?: string;       // 注册地址
  estiblishTime?: string;     // 成立时间
  fromTime?: string;          // 经营开始时间
  toTime?: string;            // 经营结束时间
  businessScope?: string;     // 经营范围
  approvedTime?: string;      // 核准时间
  regStatus?: string;         // 企业状态
  regCapital?: string;        // 注册资本
  regInstitute?: string;      // 登记机关
  orgNumber?: string;         // 组织机构代码
  creditCode?: string;        // 统一社会信用代码
  property3?: string;         // 英文名
  updatetime?: string;        // 更新时间
  companyId?: string;         // 表对应id
  taxNumber?: string;         // 纳税人识别号
  email?: string;             // 邮箱
  website?: string;           // 网址
  phoneNumber?: string;       // 电话号
  lastUpdateTime?: string;    // 最后更新时间，建议用 ISO 字符串
}


const TianyanchaInfo = ({ id }:{id:any}) => {
   const intl = useIntl();
  
// 渲染列表头，注意用 intl.formatMessage 做标题国际化
const getColumns = (intl: IntlShape): ColumnsType<TianyanchaInfo> => [
  { title: intl.formatMessage({ id: 'component.tianyancha.base' }), dataIndex: 'base', key: 'base' },
  { title: intl.formatMessage({ id: 'component.tianyancha.name' }), dataIndex: 'name', key: 'name' },
  { title: intl.formatMessage({ id: 'component.tianyancha.legalPersonName' }), dataIndex: 'legalPersonName', key: 'legalPersonName' },
  { title: intl.formatMessage({ id: 'component.tianyancha.legalPersonType' }), dataIndex: 'legalPersonType', key: 'legalPersonType' },
  { title: intl.formatMessage({ id: 'component.tianyancha.regNumber' }), dataIndex: 'regNumber', key: 'regNumber' },
  { title: intl.formatMessage({ id: 'component.tianyancha.industry' }), dataIndex: 'industry', key: 'industry' },
  { title: intl.formatMessage({ id: 'component.tianyancha.companyOrgType' }), dataIndex: 'companyOrgType', key: 'companyOrgType' },
  { title: intl.formatMessage({ id: 'component.tianyancha.regLocation' }), dataIndex: 'regLocation', key: 'regLocation' },
  { title: intl.formatMessage({ id: 'component.tianyancha.estiblishTime' }), dataIndex: 'estiblishTime', key: 'estiblishTime' },
  { title: intl.formatMessage({ id: 'component.tianyancha.fromTime' }), dataIndex: 'fromTime', key: 'fromTime' },
  { title: intl.formatMessage({ id: 'component.tianyancha.toTime' }), dataIndex: 'toTime', key: 'toTime' },
  { title: intl.formatMessage({ id: 'component.tianyancha.businessScope' }), dataIndex: 'businessScope', key: 'businessScope' },
  { title: intl.formatMessage({ id: 'component.tianyancha.approvedTime' }), dataIndex: 'approvedTime', key: 'approvedTime' },
  { title: intl.formatMessage({ id: 'component.tianyancha.regStatus' }), dataIndex: 'regStatus', key: 'regStatus' },
  { title: intl.formatMessage({ id: 'component.tianyancha.regCapital' }), dataIndex: 'regCapital', key: 'regCapital' },
  { title: intl.formatMessage({ id: 'component.tianyancha.regInstitute' }), dataIndex: 'regInstitute', key: 'regInstitute' },
  { title: intl.formatMessage({ id: 'component.tianyancha.orgNumber' }), dataIndex: 'orgNumber', key: 'orgNumber' },
  { title: intl.formatMessage({ id: 'component.tianyancha.creditCode' }), dataIndex: 'creditCode', key: 'creditCode' },
  { title: intl.formatMessage({ id: 'component.tianyancha.property3' }), dataIndex: 'property3', key: 'property3' },
  { title: intl.formatMessage({ id: 'component.tianyancha.updatetime' }), dataIndex: 'updatetime', key: 'updatetime' },
  { title: intl.formatMessage({ id: 'component.tianyancha.companyId' }), dataIndex: 'companyId', key: 'companyId' },
  { title: intl.formatMessage({ id: 'component.tianyancha.taxNumber' }), dataIndex: 'taxNumber', key: 'taxNumber' },
  { title: intl.formatMessage({ id: 'component.tianyancha.email' }), dataIndex: 'email', key: 'email' },
  { title: intl.formatMessage({ id: 'component.tianyancha.website' }), dataIndex: 'website', key: 'website' },
  { title: intl.formatMessage({ id: 'component.tianyancha.phoneNumber' }), dataIndex: 'phoneNumber', key: 'phoneNumber' },
  { title: intl.formatMessage({ id: 'component.tianyancha.lastUpdateTime' }), dataIndex: 'lastUpdateTime', key: 'lastUpdateTime' },
];

  const [pagination, setPagination] = useState({ current: 1, pageSize: 10, total: 0 });
  const [loading, setLoading] = useState(false);
  const [dataList, setDataList] = useState<TianyanchaInfo[]>([]);
  const [updateTime, setUpdateTime] = useState<string>(new Date().toLocaleDateString());

  const getList = async (page: number, pageSize: number) => {
    setLoading(true);
    try {
      const { code, data, total } = await tianyanchaIc({ pageNum: page, pageSize });
      if (code === 200) {
        setDataList(data);
        setPagination({ current: page, pageSize, total });
      } else {
        message.error(intl.formatMessage({ id: 'component.tianyancha.fetchFailed' }));
      }
    } catch (err) {
      message.error(intl.formatMessage({ id: 'component.tianyancha.fetchFailed' }));
    } finally {
      setLoading(false);
    }
  };

  const onUpdateTime = () => {
    setUpdateTime(new Date().toLocaleDateString());
    getList(pagination.current, pagination.pageSize);
    message.success(intl.formatMessage({ id: 'component.tianyancha.updateSuccess' }));
  };

  const handleTableChange = (pageInfo: any) => {
    getList(pageInfo.current, pageInfo.pageSize);
  };

  useEffect(() => {
    setUpdateTime(new Date().toLocaleDateString());
    getList(pagination.current, pagination.pageSize);
  }, [id]);

  return (
    <>
      <Table
        title={() => (
          <div>
            <span style={{ fontWeight: 'bold', fontSize: 16, textIndent: '-16px' }}>
              {intl.formatMessage({ id: 'component.tianyancha.title' })}
            </span>
            （{intl.formatMessage({ id: 'component.tianyancha.updateTime' })}：{updateTime}）{' '}
            <Button size="small" onClick={onUpdateTime}>
              {intl.formatMessage({ id: 'component.tianyancha.updateBtn' })}
            </Button>
          </div>
        )}
        rowKey="companyId"
        dataSource={dataList}
        columns={getColumns(intl)}
        pagination={{
          current: pagination.current,
          pageSize: pagination.pageSize,
          total: pagination.total,
          showSizeChanger: true,
          pageSizeOptions: ['5', '10', '20'],
        }}
        loading={loading}
        onChange={handleTableChange}
      />
    </>
  );
};

export default TianyanchaInfo;
