import { Button, Space, Tag } from 'antd';
import type { ProColumns } from '@ant-design/pro-components';
import type { OrgType, OrgVO } from './types';

export const orgTypeMeta: Record<OrgType, { text: string; color: string }> = {
  HQ: { text: '总部', color: 'gold' },
  BRANCH: { text: '分公司', color: 'blue' },
  DEALER: { text: '经销商', color: 'green' },
  AGENT: { text: '代理商', color: 'purple' },
  DEPT: { text: '部门', color: 'cyan' },
  TEAM: { text: '团队', color: 'orange' },
};

/** 兼容后端返回未知类型，避免直接索引得到 undefined。 */
export const getOrgTypeMeta = (type?: string) => {
  const key = String(type ?? '').toUpperCase() as OrgType;
  return orgTypeMeta[key] ?? { text: type || '-', color: 'default' };
};

export const createDepartmentColumns = (options: {
  leaderNameMap: Map<number, string>;
  onCreateChild: (record: OrgVO) => void;
  onEdit: (record: OrgVO) => void;
  onToggle: (record: OrgVO) => void;
  onDelete: (record: OrgVO) => void;
}): ProColumns<OrgVO>[] => [
  {
    title: '部门名称',
    dataIndex: 'orgName',
    width: 260,
    fieldProps: { placeholder: '请输入部门名称' },
  },
  {
    title: '组织类型',
    dataIndex: 'orgType',
    width: 120,
    valueType: 'select',
    valueEnum: {
      HQ: { text: '总部' },
      BRANCH: { text: '分公司' },
      DEALER: { text: '经销商' },
      AGENT: { text: '代理商' },
      DEPT: { text: '部门' },
      TEAM: { text: '团队' },
    },
    render: (_value, record) => {
      const meta = getOrgTypeMeta(record.orgType);
      return (
        <Tag color={meta.color} bordered={false}>
          {meta.text}
        </Tag>
      );
    },
  },
  {
    title: '负责人',
    dataIndex: 'leaderId',
    width: 140,
    search: false,
    render: (_value, record) => {
      if (!record.leaderId) return '-';
      return options.leaderNameMap.get(record.leaderId) ?? `#${record.leaderId}`;
    },
  },
  {
    title: '所在地区',
    dataIndex: 'region',
    width: 140,
    search: false,
    render: (_value, record) => record.region || '-',
  },
  {
    title: '排序',
    dataIndex: 'sortOrder',
    width: 80,
    search: false,
    render: (_value, record) => record.sortOrder ?? '-',
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 100,
    valueType: 'select',
    valueEnum: {
      true: { text: '启用' },
      false: { text: '停用' },
    },
    render: (_value, record) => (
      <Tag color={record.status ? 'green' : 'default'} bordered={false}>
        {record.status ? '启用' : '停用'}
      </Tag>
    ),
  },
  {
    title: '操作',
    key: 'action',
    valueType: 'option',
    width: 260,
    render: (_value, record) => (
      <Space size={0}>
        <Button type="link" size="small" onClick={() => options.onCreateChild(record)}>
          新增子部门
        </Button>
        {/* 总部为租户根组织，不允许编辑 */}
        <Button
          type="link"
          size="small"
          disabled={record.orgType === 'HQ'}
          onClick={() => options.onEdit(record)}
        >
          编辑
        </Button>
        <Button type="link" size="small" onClick={() => options.onToggle(record)}>
          {record.status ? '停用' : '启用'}
        </Button>
        <Button type="link" size="small" danger onClick={() => options.onDelete(record)}>
          删除
        </Button>
      </Space>
    ),
  },
];
