/* eslint-disable jsx-a11y/anchor-is-valid */
import { Table, Space } from "antd";
import moment from "moment";
import * as coursesAPI from "../../apis/coursesAPI";
import locale from "antd/es/date-picker/locale/zh_CN";

const columns = (
  handleCreateCourse,
  handleDeleteCourse,
  initTab,
  handleCopyUrl,
  handleRouterToOtherPage,
  isPureWeb,
  handleCompletedLiveCourse,
  currentPlatform // 预留的参数当前的环境
) => {
  // 我创建的列表只有状态是未审核的才能删除和编辑
  const isEditAndDelete = (record) => record.Status === 1 && initTab === 1;
  // 只有状态是已审核的才能分享
  const isShare = (record) => record.Status === 2;
  // 老师端在web不能进入课程 并且我创建的里的课程状态要是已审核或者我申请的已经开始的课程
  const isStartCourse = (record) =>
    record.CourseType === 1 &&
    ((!isPureWeb && initTab === 1 && record.Status === 2) ||
      (initTab === 3 && record.Status === 4));
  // 是否结束课程 需要是直播课、是否壳子、是否是我创建的列表、课程状态是否已开始
  const isEndCourse = (record) =>
    record.CourseType === 1 &&
    !isPureWeb &&
    initTab === 1 &&
    record.Status === 4;
  const tableColumns = [
    {
      title: "课程名称",
      width: 100,
      dataIndex: "Name",
      fixed: "left",
      key: "Name",
    },
    {
      title: "课程形式",
      dataIndex: "CourseType",
      key: "CourseType",
      render: (courseType) => `${getCourseTypeName(courseType)}`,
      width: 150,
    },
    {
      title: "所属专辑",
      dataIndex: "CourseAlbums",
      render: (courseAlbums) => `${getCourseAlbumsName(courseAlbums)}`,
      key: "CourseAlbums",
      width: 150,
    },
    {
      title: "状态",
      dataIndex: "Status",
      key: "Status",
      render: (status) => `${getStatusName(status)}`,
      width: 150,
    },
    {
      title: "开始时间",
      dataIndex: "StartTime",
      key: "StartTime",
      render: (startTime) => `${getStartTimeName(startTime)}`,
      width: 150,
    },
    {
      title: "可见范围",
      dataIndex: "ViewRange",
      key: "ViewRange",
      render: (viewRange) => `${getViewRangeName(viewRange)}`,
      width: 150,
    },
    {
      title: "播放量",
      dataIndx: "PlayCount",
      key: "PlayCount",
      width: 150,
    },
    {
      title: "创建时间",
      dataIndex: "CreateTime",
      key: "CreateTime",
      render: (createTime) => `${getStartTimeName(createTime)}`,
      width: 150,
    },
    {
      title: "操作",
      key: "operation",
      fixed: "right",
      width: 250,
      render: (_, record) => (
        <Space size="middle">
          {isEditAndDelete(record) && (
            <a
              onClick={() => {
                handleCreateCourse(record.Code);
              }}
            >
              修改
            </a>
          )}
          {isEditAndDelete(record) && (
            <a
              onClick={() => {
                handleDeleteCourse(record.Code);
              }}
            >
              删除
            </a>
          )}
          {isShare(record) && (
            <a onClick={() => handleCopyUrl(record.CourseShareUrl)}>分享</a>
          )}
          {isStartCourse(record) && (
            <a
              onClick={() =>
                coursesAPI.sendCourseEntryNotice(
                  `target=joinCourse?code=${record.Code}`
                )
              }
            >
              {initTab === 1 ? "开始课程" : "进入课程"}
            </a>
          )}
          {isEndCourse(record) && (
            <a onClick={() => handleCompletedLiveCourse(record.Code)}>
              结束课程
            </a>
          )}
        </Space>
      ),
    },
  ];
  if (initTab === 2) {
    return [
      ...tableColumns.slice(0, tableColumns.length - 1),
      {
        title: "操作",
        key: "operation",
        fixed: "right",
        width: 250,
        render: (_, record) => (
          <Space size="middle">
            {record.Status === 2 && (
              <a onClick={() => handleCopyUrl(record.CourseLiveUrl)}>分享</a>
            )}

            {record.CourseType === 1 && record.Status === 4 && (
              <a
                onClick={() => {
                  handleRouterToOtherPage(
                    "/video_course_player/" +
                      record.Code +
                      "/" +
                      currentPlatform
                  );
                }}
              >
                进入课程
              </a>
            )}
          </Space>
        ),
      },
    ];
  } else {
    return tableColumns;
  }
};
// 专辑表头
const albumColumns = (
  handleCreateAlbum,
  handleDeleteAlbum,
  initTab,
  handleCopyUrl
) => {
  return [
    {
      title: "专辑名称",
      width: 100,
      dataIndex: "Name",
      key: "Name",
    },

    {
      title: "开始时间",
      dataIndex: "StartTime",
      key: "StartTime",
      render: (startTime) => `${getStartTimeName(startTime)}`,
      width: 150,
    },
    {
      title: "可见范围",
      dataIndex: "ViewRange",
      key: "ViewRange",
      render: (viewRange) => `${getViewRangeName(viewRange)}`,
      width: 150,
    },
    {
      title: "创建时间",
      dataIndex: "CreateTime",
      key: "CreateTime",
      render: (createTime) => `${getStartTimeName(createTime)}`,
      width: 150,
    },
    {
      title: "操作",
      key: "operation",
      fixed: "right",
      width: 250,
      render: (_, record) => (
        <Space size="middle">
          <a
            onClick={() => {
              handleCreateAlbum(record.Code);
            }}
          >
            修改
          </a>
          <a
            onClick={() => {
              handleDeleteAlbum(record.Code);
            }}
          >
            删除
          </a>
          <a onClick={() => handleCopyUrl(record.CourseShareUrl)}>分享</a>
        </Space>
      ),
    },
  ];
};

const getCourseTypeName = (courseType) => {
  switch (courseType) {
    case 1:
      return "直播课程";
    case 2:
      return "视频课程";
    case 3:
      return "多媒体课程";
    default:
      return "暂无";
  }
};

const getStatusName = (status) => {
  switch (status) {
    case 1:
      return "待审核";
    case 2:
      return "已审核";
    case 3:
      return "已拒绝";
    case 4:
      return "已开始";
    case 5:
      return "已结束";
    case 6:
      return "已取消";
    case 7:
      return "延时";
    case 8:
      return "不在线";
    default:
      return "暂无";
  }
};
const getViewRangeName = (viewRange) => {
  switch (viewRange) {
    case 0:
      return "全部可见";
    case 1:
      return "仅国内可见";
    case 2:
      return "仅国外可见";
    default:
      return "暂无";
  }
};
const getStartTimeName = (startTime) => {
  const dateObj = moment(startTime).local();
  const formattedTime = dateObj.format("YYYY-MM-DD HH:mm:ss");
  return formattedTime;
};

const getCourseAlbumsName = (courseAlbums) => {
  if (!courseAlbums || courseAlbums.length === 0) return "暂无";
  let courseAlbumsNameList = [];
  for (let i of courseAlbums) {
    courseAlbumsNameList.push(i.Name);
  }
  return courseAlbumsNameList.join("、");
};
// const data = [];
// for (let i = 0; i < 100; i++) {
//   data.push({
//     key: i,
//     name: `Edward ${i}`,
//     age: 32,
//     address: `London Park no. ${i}`,
//   });
// }
const CourseTable = ({
  coursesData,
  totalSize,
  isCourse,
  isPureWeb,
  initTab,
  handleTableChange,
  currentPage,
  handleCreateCourse,
  handleCreateAlbum,
  handleDeleteCourse,
  handleDeleteAlbum,
  handleCopyUrl,
  handleRouterToOtherPage,
  handleCompletedLiveCourse,
  currentPlatform,
}) => (
  <Table
    onRow={(record) => {
      return {
        onClick: (event) => {
          if (event.target.tagName !== "A") {
            let path;
            if (isCourse) {
              path = "/detail/" + record.Code + "/course";
            } else {
              path = "/detail/" + record.Code + "/album";
            }
            handleRouterToOtherPage(path);
          }
        },
      };
    }}
    locale={{ emptyText: "暂无数据" }}
    columns={
      isCourse
        ? columns(
            handleCreateCourse,
            handleDeleteCourse,
            initTab,
            handleCopyUrl,
            handleRouterToOtherPage,
            isPureWeb,
            handleCompletedLiveCourse,
            currentPlatform
          )
        : albumColumns(
            handleCreateAlbum,
            handleDeleteAlbum,
            initTab,
            handleCopyUrl
          )
    }
    dataSource={coursesData}
    pagination={{
      current: currentPage,
      pageSize: 10,
      total: totalSize,
    }}
    onChange={(pagination, filters) => {
      handleTableChange(pagination, filters);
    }}
    scroll={{
      x: 1500,
      y: 505,
    }}
  />
);
export default CourseTable;
