import { Tag } from 'antd';
import type { ProColumns } from '@ant-design/pro-components';
import type {
  CustomerLevel,
  CustomerStage,
  CustomerType,
  CustomerVO,
} from './types';

const customerTypeMeta: Record<CustomerType, { text: string; color: string }> = {
  ENTERPRISE: { text: '企业', color: 'blue' },
  PERSONAL: { text: '个人', color: 'default' },
};

const levelMeta: Record<CustomerLevel, { text: string; color: string }> = {
  VIP: { text: 'VIP', color: 'purple' },
  A: { text: 'A 类', color: 'gold' },
  B: { text: 'B 类', color: 'blue' },
  C: { text: 'C 类', color: 'default' },
};

const stageMeta: Record<CustomerStage, { text: string; color: string }> = {
  NEW_LEAD: { text: '新线索', color: 'default' },
  PENDING_ASSIGN: { text: '待分配', color: 'orange' },
  CONTACTING: { text: '沟通中', color: 'processing' },
  NEEDS_COMM: { text: '需求沟通', color: 'cyan' },
  QUOTING: { text: '报价中', color: 'blue' },
  NEGOTIATING: { text: '谈判中', color: 'gold' },
  DEAL: { text: '已成交', color: 'success' },
  LOST: { text: '已流失', color: 'error' },
};

const renderTags = (tags?: string) => {
  const items = tags?.split(/[,，]/).map((tag) => tag.trim()).filter(Boolean) ?? [];
  if (!items.length) return '-';

  return (
    <>
      {items.map((tag) => (
        <Tag key={tag} bordered={false}>
          {tag}
        </Tag>
      ))}
    </>
  );
};

export const customerColumns: ProColumns<CustomerVO>[] = [
  {
    title: '客户名称',
    dataIndex: 'name',
    width: 220,
    ellipsis: true,
    copyable: true,
    search: false,
  },
  {
    title: '客户类型',
    dataIndex: 'customerType',
    width: 100,
    search: false,
    render: (_value, record) => {
      const meta = record.customerType ? customerTypeMeta[record.customerType] : null;
      return meta ? (
        <Tag color={meta.color} bordered={false}>
          {meta.text}
        </Tag>
      ) : (
        '-'
      );
    },
  },
  {
    title: '所属行业',
    dataIndex: 'industry',
    width: 130,
    ellipsis: true,
    search: false,
    render: (_value, record) => record.industry ?? '-',
  },
  {
    title: '所在地区',
    dataIndex: 'region',
    width: 120,
    ellipsis: true,
    search: false,
    render: (_value, record) => record.region ?? '-',
  },
  {
    title: '联系人',
    dataIndex: 'contactName',
    width: 110,
    search: false,
    render: (_value, record) => record.contactName ?? '-',
  },
  {
    title: '联系电话',
    dataIndex: 'contactPhone',
    width: 140,
    search: false,
    render: (_value, record) => record.contactPhone ?? '-',
  },
  {
    title: '客户等级',
    dataIndex: 'level',
    width: 100,
    search: false,
    render: (_value, record) => {
      const meta = record.level ? levelMeta[record.level] : null;
      return meta ? (
        <Tag color={meta.color} bordered={false}>
          {meta.text}
        </Tag>
      ) : (
        '-'
      );
    },
  },
  {
    title: '客户阶段',
    dataIndex: 'stage',
    width: 110,
    search: false,
    render: (_value, record) => {
      const meta = record.stage ? stageMeta[record.stage] : null;
      return meta ? (
        <Tag color={meta.color} bordered={false}>
          {meta.text}
        </Tag>
      ) : (
        '-'
      );
    },
  },
  {
    title: '归属销售员',
    dataIndex: 'ownerId',
    width: 130,
    valueType: 'digit',
    tooltip: '按归属销售员 ID 筛选，不填表示全部',
    fieldProps: { placeholder: '销售员 ID', min: 1, precision: 0 },
    render: (_value, record) => (record.ownerId ? `#${record.ownerId}` : '-'),
  },
  {
    title: '最近跟进',
    dataIndex: 'lastFollowAt',
    width: 170,
    valueType: 'dateTime',
    search: false,
  },
  {
    title: '标签',
    dataIndex: 'tags',
    width: 180,
    search: false,
    render: (_value, record) => renderTags(record.tags),
  },
];
