import React, { useRef } from 'react';
import { Tag } from 'antd';
import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components';
import { getCmsInterviewList } from '@/services/Management/list';

const interviewStatusMap: Record<string, { text: string; color: string }> = {
  pending: { text: '待确认', color: 'blue' },
  accepted: { text: '已接受', color: 'green' },
  rejected: { text: '已拒绝', color: 'red' },
  completed: { text: '已完成', color: 'purple' },
};

const interviewMethodMap: Record<string, { text: string; color: string }> = {
  online: { text: '线上', color: 'blue' },
  offline: { text: '线下', color: 'orange' },
};

const InterviewList: React.FC = () => {
  const actionRef = useRef<ActionType>();

  const columns: ProColumns<API.Management.InterviewInvitation>[] = [
    {
      title: '求职者姓名',
      dataIndex: 'applicantName',
      valueType: 'text',
      align: 'center',
      width: 100,
      render: (_, record) => record.applicantName || '-',
    },
    {
      title: '联系方式',
      dataIndex: 'applicantPhone',
      valueType: 'text',
      align: 'center',
      width: 130,
      hideInSearch: true,
      render: (_, record) => record.applicantPhone || '-',
    },
    {
      title: '应聘岗位',
      dataIndex: 'jobTitle',
      valueType: 'text',
      align: 'center',
      width: 150,
      render: (_, record) => record.jobTitle || record.jobName || '-',
    },
    {
      title: '公司名称',
      dataIndex: 'companyName',
      valueType: 'text',
      align: 'center',
      width: 150,
      hideInSearch: true,
    },
    {
      title: '面试时间',
      dataIndex: 'interviewTime',
      valueType: 'text',
      align: 'center',
      width: 160,
      hideInSearch: true,
    },
    {
      title: '面试方式',
      dataIndex: 'interviewMethod',
      valueType: 'text',
      align: 'center',
      width: 80,
      hideInSearch: true,
      render: (_, record) => {
        const m = interviewMethodMap[record.interviewMethod || ''];
        if (!m) return '-';
        return <Tag color={m.color}>{m.text}</Tag>;
      },
    },
    {
      title: '面试官姓名',
      dataIndex: 'interviewerName',
      valueType: 'text',
      align: 'center',
      width: 100,
      hideInSearch: true,
      render: (_, record) => record.interviewerName || '-',
    },
    {
      title: '面试官联系方式',
      dataIndex: 'contactPhone',
      valueType: 'text',
      align: 'center',
      width: 140,
      hideInSearch: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      valueType: 'text',
      align: 'center',
      width: 80,
      render: (_, record) => {
        const st = interviewStatusMap[record.status || ''];
        if (!st) return <Tag color="default">未知</Tag>;
        return <Tag color={st.color}>{st.text}</Tag>;
      },
    },
    {
      title: '备注',
      dataIndex: 'remark',
      valueType: 'text',
      align: 'center',
      width: 150,
      hideInSearch: true,
      render: (_, record) => record.remark || '-',
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      valueType: 'text',
      align: 'center',
      width: 160,
      hideInSearch: true,
    },
  ];

  return (
    <ProTable<API.Management.InterviewInvitation>
      actionRef={actionRef}
      rowKey="id"
      columns={columns}
      headerTitle="面试列表"
      request={async (params) => {
        const { current, pageSize, ...searchParams } = params;
        const response = await getCmsInterviewList({
          ...searchParams,
          pageNum: current,
          pageSize,
        });
        return {
          data: response.rows || [],
          total: response.total || 0,
          success: true,
        };
      }}
      search={{
        labelWidth: 'auto',
      }}
      pagination={{
        defaultPageSize: 10,
        showSizeChanger: true,
      }}
    />
  );
};

export default InterviewList;
