import React, { useState, useEffect } from 'react';
import { Table, Card, Rate, Image, Tag, Space, message } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { getReviews, Review } from '../api/review';
import dayjs from 'dayjs';

const ReviewList: React.FC = () => {
  const [loading, setLoading] = useState(false);
  const [reviews, setReviews] = useState<Review[]>([]);
  const [total, setTotal] = useState(0);
  const [page, setPage] = useState(1);
  const [pageSize, setPageSize] = useState(10);

  const fetchReviews = async () => {
    setLoading(true);
    try {
      const res = await getReviews({ page, pageSize });
      setReviews(res.list);
      setTotal(res.total);
    } catch (error) {
      message.error('获取评价列表失败');
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    fetchReviews();
  }, [page, pageSize]);

  const columns: ColumnsType<Review> = [
    {
      title: '订单号',
      dataIndex: ['order', 'orderNo'],
      key: 'orderNo',
      width: 180,
    },
    {
      title: '房源',
      dataIndex: ['room', 'title'],
      key: 'roomTitle',
      width: 200,
      ellipsis: true,
    },
    {
      title: '评分',
      dataIndex: 'rating',
      key: 'rating',
      width: 150,
      render: (rating: number) => <Rate disabled value={rating} />,
    },
    {
      title: '评价内容',
      dataIndex: 'content',
      key: 'content',
      ellipsis: true,
      render: (content: string) => content || '-',
    },
    {
      title: '图片',
      dataIndex: 'images',
      key: 'images',
      width: 120,
      render: (images: string[]) => {
        if (!images || images.length === 0) return '-';
        return (
          <Image.PreviewGroup>
            <Space>
              {images.slice(0, 3).map((img, idx) => (
                <Image
                  key={idx}
                  src={img}
                  width={40}
                  height={40}
                  style={{ objectFit: 'cover' }}
                />
              ))}
              {images.length > 3 && <span>+{images.length - 3}</span>}
            </Space>
          </Image.PreviewGroup>
        );
      },
    },
    {
      title: '匿名',
      dataIndex: 'isAnonymous',
      key: 'isAnonymous',
      width: 80,
      render: (isAnonymous: boolean) => (
        <Tag color={isAnonymous ? 'blue' : 'default'}>
          {isAnonymous ? '是' : '否'}
        </Tag>
      ),
    },
    {
      title: '评价时间',
      dataIndex: 'createdAt',
      key: 'createdAt',
      width: 180,
      render: (date: string) => dayjs(date).format('YYYY-MM-DD HH:mm:ss'),
    },
  ];

  return (
    <Card title="评价管理" bordered={false}>
      <Table
        columns={columns}
        dataSource={reviews}
        rowKey="id"
        loading={loading}
        pagination={{
          current: page,
          pageSize,
          total,
          showSizeChanger: true,
          showTotal: (total) => `共 ${total} 条`,
          onChange: (page, pageSize) => {
            setPage(page);
            setPageSize(pageSize);
          },
        }}
      />
    </Card>
  );
};

export default ReviewList;
