import React, { useState, useEffect } from 'react';
import CourseInformationItem from './CourseInformationItem';
import FPlatformEnum from '../../utils/enum';
import { Empty } from 'antd';

// 课程目录
const CourseInformation = ({ courseItems, onChildData, platform }) => {
  const [sortedCourses, setSortedCourses] = useState([]);

  useEffect(() => {
    // 按照 updateTime 进行降序排序
    const sortedData = courseItems.map((item, index) => {
      item = { ...item, index: index };
      return item;
    });
    // const sortedData = [...courseItems];
    setSortedCourses(sortedData);
  }, [courseItems]);
  const handleChildData = (data) => {
    onChildData(data);
  };

  return (
    <div className=''>
      <div className='px-5 flex items-center justify-between font-bold text-gray-500 text-sm py-2 border-b border-gray-300'>
        <div className='w-20'>序号</div>
        <div className='w-1/2'>资料名称</div>
        {FPlatformEnum.byIndex(platform).isWeb && (
          <div className='w-1/4'>操作</div>
        )}
      </div>
      {sortedCourses.length === 0 && (
        <Empty description={false} style={{ height: '100%' }} />
      )}
      {sortedCourses.map((course) => (
        <CourseInformationItem
          key={course.Code}
          course={course}
          onChildData={handleChildData}
          platform={platform}
        />
      ))}
    </div>
  );
};

export default CourseInformation;
