import React from 'react';
import { Card, Table, Button, message } from 'antd';
import { UnorderedListOutlined } from '@ant-design/icons';
import styles from '../../supplierTaskManageDetail.less';
import type { TaskDetailData, IndicatorList, User } from '@/servers/types/supplierEvaluateTask';

interface EvaluatorInfoProps {
  taskData: TaskDetailData;
  onViewIndicators: (record: IndicatorList) => void;
}

const EvaluatorInfo: React.FC<EvaluatorInfoProps> = ({ taskData, onViewIndicators }) => {
  const userList = taskData.userList;
  // 查看评价人员分工
  const handleViewIndicators = (record: IndicatorList) => {
    if (record.indicatorIds && record.indicatorIds.length > 0) {
      onViewIndicators(record);
    } else {
      message.info('该评价人员没有分配指标');
    }
  };

  const columns = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      render: (_: any, __: any, index: number) => index + 1,
      width: 80,
    },
    {
      title: '评价人员',
      dataIndex: 'userId',
      key: 'userId',
      render: (userId: string) => {
        try {
          const user = userList.find((userItem: User) => userItem.userId === userId);
          return user?.userName;
        } catch (error) {
          console.error('获取评价人员信息失败:', error);
          return userId;
        }
      },
    },
    {
      title: '评价类型',
      dataIndex: 'type',
      key: 'type',
      render: (type: string) => (type === '1' ? '按指标评价' : '按评价单评价'),
    },
    {
      title: '指标数量',
      dataIndex: 'indicatorIds',
      key: 'indicatorCount',
      render: (indicatorIds: string[]) => indicatorIds?.length || 0,
    },
    {
      title: '操作',
      key: 'action',
      render: (record: any) => (
        <Button
          type="link"
          onClick={() => handleViewIndicators(record)}
        >
          查看分工
        </Button>
      ),
    },
  ];

  if (!taskData || !taskData.indicatorList || taskData.indicatorList.length === 0) {
    return <div className={styles.emptyData}>暂无评价人员数据</div>;
  }

  return (
    <Card className={styles.detailCard}>
      <Table
        columns={columns}
        dataSource={taskData.indicatorList}
        rowKey="userId"
        pagination={false}
        className={styles.tableContainer}
      />
    </Card>
  );
};

export default EvaluatorInfo;
