// aislop-ignore-file: duplicate-block -- 列渲染结构相似且字段不同，逻辑已组件化
import { Button, Popconfirm, Space, Tag } from 'antd';
import { InboxOutlined, LogoutOutlined, SwapOutlined } from '@ant-design/icons';
import PermissionButton from '../../components/PermissionButton';
import { message } from '../../ui/app-message';

export interface OccupancyRow {
  id: number;
  studentId: number;
  roomId: number;
  checkInDate?: string;
  billingStartDate?: string;
  billingEndDate?: string;
  checkOutDate?: string | null;
  status?: string;
  student?: { id?: number; name?: string; studentNo?: string } | null;
  room?: { id?: number; roomNumber?: string; building?: string } | null;
  bed?: { bedNumber?: string } | null;
  locker?: { lockerNumber?: string } | null;
}

export interface OccupancyColumnContext {
  readonly: boolean;
  canPurge: boolean;
  canDelete: boolean;
  onPurge: (id: number, name: string) => void;
  onArchive: (id: number) => Promise<unknown> | unknown;
  onCheckOut: (record: OccupancyRow) => void;
  onTransfer: (record: OccupancyRow) => void;
}

const buildOccupancyDataColumns = () => {
  return [
    {
      title: '学生',
      width: 120,
      render: (_: unknown, r: OccupancyRow) => r.student?.name || '-',
    },
    {
      title: '宿舍',
      width: 120,
      render: (_: unknown, r: OccupancyRow) => r.room?.roomNumber || '-',
    },
    {
      title: '床位',
      width: 80,
      render: (_: unknown, r: OccupancyRow) => r.bed?.bedNumber || '-',
    },
    {
      title: '柜子',
      width: 80,
      render: (_: unknown, r: OccupancyRow) => r.locker?.lockerNumber || '-',
    },
    { title: '入住日期', dataIndex: 'checkInDate', width: 110 },
    { title: '计费起始', dataIndex: 'billingStartDate', width: 110 },
    {
      title: '退宿日期',
      dataIndex: 'checkOutDate',
      width: 110,
      render: (v: any) => v || <Tag color="green">在住</Tag>,
    },
    { title: '计费截止', dataIndex: 'billingEndDate', render: (v: any) => v || '-' },
    { title: '退宿原因', dataIndex: 'checkOutReason', render: (v: any) => v || '-' },
  ];
};

const buildOccupancyActionColumn = (ctx: OccupancyColumnContext) => {
  const { readonly, canPurge, canDelete, onPurge, onArchive, onCheckOut, onTransfer } = ctx;
  return {
    title: '操作',
    width: 220,
    render: (_: any, record: OccupancyRow) =>
        readonly ? (
          <Space>
            <Tag color="#999">已归档</Tag>
            {canPurge ? (
              <Button
                size="small"
                danger
                type="link"
                onClick={() => onPurge(record.id, record.student?.name || `记录${record.id}`)}
              >
                删除
              </Button>
            ) : null}
          </Space>
        ) : !record.checkOutDate ? (
          <Space>
            <PermissionButton
              permission="occupancy:checkout"
              size="small"
              icon={<LogoutOutlined />}
              onClick={() => onCheckOut(record)}
            >
              退宿
            </PermissionButton>
            <PermissionButton
              permission="occupancy:transfer"
              size="small"
              icon={<SwapOutlined />}
              onClick={() => onTransfer(record)}
            >
              换房
            </PermissionButton>
          </Space>
        ) : (
          <Space>
            <Tag>已退宿</Tag>
            {canDelete ? (
              <Popconfirm
                title="确定归档此记录？"
                onConfirm={async () => {
                  try {
                    await onArchive(record.id);
                    message.success('归档成功');
                  } catch {
                    // 错误提示由 useApiMutation 统一处理
                  }
                }}
              >
                <Button size="small" danger icon={<InboxOutlined />}>
                  归档
                </Button>
              </Popconfirm>
            ) : null}
          </Space>
        ),
  };
};

export const buildOccupancyColumns = (ctx: OccupancyColumnContext) => {
  return [...buildOccupancyDataColumns(), buildOccupancyActionColumn(ctx)];
};
