import React, { useEffect, useState } from 'react';
import { message, Space, Table, Tag } from 'antd';
import type { TablePaginationConfig, TableProps } from 'antd';
import { FilterValue, SorterResult, TableCurrentDataSource } from 'antd/es/table/interface';
import { SoftwareControl } from '@/services/services/software';
import { useModel } from '@umijs/max';
import moment from 'moment';


const columns: TableProps<SoftwareModel.SoftwareControlBase>['columns'] = [
  {
    title: '软件代码',
    dataIndex: 'software',
    width: 100,
    key: 'softwareCode',
    render: (software) => <span> {software.softwareCode}</span >
  },
  {
    title: '软件名称',
    dataIndex: 'software',
    key: 'softwareName',
    render: (software) => <span>{software.softwareName}</span>,
  },
  {
    title: '到期时间',
    dataIndex: 'expirationTime',
    key: 'expirationTime',
    width: 200,
    render: (expirationTime) => expirationTime ? moment(expirationTime).format('YYYY-MM-DD HH:mm:ss') : 'null',
  },
  {
    title: '是否永久',
    dataIndex: 'isForever',
    key: 'isForever',
    width: 100,
    render: (isForever) => isForever ? <Tag color="green">是</Tag> : <Tag color="red">否</Tag>,
  }
];

interface UserSoftwareInfoProps {
  userId?: number;
}

const UserSoftwareInfo: React.FC<UserSoftwareInfoProps> = ({ userId }) => {
  const [data, setData] = React.useState<SoftwareModel.SoftwareControlBase[]>([]);
  const [loading, setLoading] = React.useState<boolean>(false);
  const [messageApi, messageHolder] = message.useMessage();
  const [tableParams, setTableParams] = useState<TableModel.TableParams>({
    pagination: {
      current: 1,
      pageSize: 10,
      showQuickJumper: true,
      totalBoundaryShowSizeChanger: true,
    },
  });

  async function QueryUserSoftwareControlCollection() {
    try {
      if (userId == null) {
        messageApi.error("用户ID不能为空");
      }
      setLoading(true);
      let res = await SoftwareControl.GetUserSoftwareControlCollection(tableParams, {
        userId: userId
      });
      setData(res.collection);
    } catch (error: any) {
      messageApi.error(error.message);
    } finally {
      setLoading(false);
    }
  }

  useEffect(() => {
    QueryUserSoftwareControlCollection().then();
  }, []);

  async function TableChangeHandle(pagination: TablePaginationConfig, filters: Record<string, FilterValue | null>, sorter: SorterResult<SoftwareModel.SoftwareControlBase> | SorterResult<SoftwareModel.SoftwareControlBase>[], extra: TableCurrentDataSource<SoftwareModel.SoftwareControlBase>): Promise<void> {
    await QueryUserSoftwareControlCollection();
    setTableParams({
      pagination: {
        ...tableParams.pagination,
        current: pagination.current,
        pageSize: pagination.pageSize,
      }
    });
  }

  return (
    <div>
      <Table<SoftwareModel.SoftwareControlBase> columns={columns} dataSource={data} rowKey={(record) => record.id} pagination={tableParams.pagination} onChange={TableChangeHandle} loading={loading} />
      {messageHolder}
    </div>
  );
};

export default UserSoftwareInfo;