// 平台状态总览相关接口
import {moduleRequest} from '@/utils/request'
import ForEach from "lodash-es/forEach";

const request = moduleRequest(`/api/webapp/`)

// 模拟数据
const mockData = {
	// 平台统计数据
	platformStats: {
		code: 200,
		message: '获取成功',
		data: {
			totalCourses: 128,
			openedCourses: 97,
			totalTeachers: 34,
			totalStudents: 1256
		}
	},

	// 热门课程数据
	hotCourses: {
		code: 200,
		message: '获取成功',
		data: [
			{
				id: 1,
				rank: 1,
				name: 'Python数据分析',
				teacher: '王老师',
				visits: '2,345',
				references: 56
			},
			{
				id: 2,
				rank: 2,
				name: 'JavaScript前端开发',
				teacher: '李老师',
				visits: '2,123',
				references: 48
			},
			{
				id: 3,
				rank: 3,
				name: '机器学习基础',
				teacher: '张老师',
				visits: '1,987',
				references: 41
			},
			{
				id: 4,
				rank: 4,
				name: '管理学原理',
				teacher: '赵老师',
				visits: '1,876',
				references: 39
			},
			{
				id: 5,
				rank: 5,
				name: '大学英语',
				teacher: '钱老师',
				visits: '1,765',
				references: 35
			}
		]
	},

	// 开课信息数据
	courseInfo: {
		code: 200,
		message: '获取成功',
		data: {
			records: [
				{
					id: 1,
					name: 'Python数据分析',
					teacher: '王老师',
					startDate: '2024-03-01',
					studentCount: 320,
					visits: '2,345'
				},
				{
					id: 2,
					name: 'JavaScript前端开发',
					teacher: '李老师',
					startDate: '2024-03-10',
					studentCount: 280,
					visits: '2,123'
				},
				{
					id: 3,
					name: '机器学习基础',
					teacher: '张老师',
					startDate: '2024-03-15',
					studentCount: 210,
					visits: '1,987'
				},
				{
					id: 4,
					name: '管理学原理',
					teacher: '赵老师',
					startDate: '2024-03-20',
					studentCount: 180,
					visits: '1,876'
				},
				{
					id: 5,
					name: '大学英语',
					teacher: '钱老师',
					startDate: '2024-03-25',
					studentCount: 266,
					visits: '1,765'
				}
			],
			total: 5,
			current: 1,
			size: 10
		}
	},

	// 课程访问统计数据
	courseVisitStats: {
		code: 200,
		message: '获取成功',
		data: {
			courseName: '',
			visitData: [120, 156, 178, 210, 234, 245, 267],
			dateRange: []
		}
	}
}

// 获取平台统计数据
export const getPlatformStats = async (params) => {
	// // 模拟异步请求
	// return new Promise((resolve) => {
	// 	setTimeout(() => {
	// 		resolve(mockData.platformStats)
	// 	}, 300)
	// })

	let systemStatus = await request('platformStatusOverview/systemStatus', params, 'get')
	console.log('010', systemStatus)

	// 平台统计数据
	let json = {
		code: 200,
		message: '获取成功',
		data: {
			totalCourses: systemStatus.COURSE_TOTAL,
			openedCourses: systemStatus.COURSE_OPEN_TOTAL,
			totalTeachers: systemStatus.TEACHER_TOTAL,
			totalStudents: systemStatus.STUDENT_TOTAL
		}
	}

	return json
}

// 获取热门课程列表
export const getHotCourses = async (params) => {
	// return new Promise((resolve) => {
	// 	setTimeout(() => {
	// 		resolve(mockData.hotCourses)
	// 	}, 300)
	// })

	let popularCourses = await request('platformStatusOverview/popularCourses', params, 'get')
	console.log('011', popularCourses)
	let data = []
	// COLLECT_COUNT:1
	// COURSE_ID:"1955176675276976130"
	// COURSE_NAME:"机电实训楼"
	// TEACHER_NAME:"王五"
	// VIEW_COUNT:289
	ForEach(popularCourses.popularCourses, (item) => {
		data.push({
			id: item.COURSE_ID,
			rank: 1,
			name: item.COURSE_NAME,
			teacher: item.TEACHER_NAME,
			visits: item.VIEW_COUNT,
			references: item.COLLECT_COUNT
		})
	})


// 热门课程数据
// 	hotCourses: {
// 		code: 200,
// 			message: '获取成功',
// 			data: [
// 			{
// 				id: 1,
// 				rank: 1,
// 				name: 'Python数据分析',
// 				teacher: '王老师',
// 				visits: '2,345',
// 				references: 56
// 			},
	// 平台统计数据
	let json = {
		code: 200,
		message: '获取成功',
		data
	}

	return json
}
function timestampToYYYYMMDD(timestamp) {
	let date
	if(timestamp == undefined){
		date = new Date();
		return date.toISOString().split('T')[0];
	}
	 date = new Date(timestamp);
	return date.toISOString().split('T')[0];
}
// 获取开课信息列表
export const getCourseInfo = async (params = {}) => {
	const {current = 1, size = 10} = params

	// return new Promise((resolve) => {
	// 	setTimeout(() => {
	// 		const response = {
	// 			...mockData.courseInfo,
	// 			data: {
	// 				...mockData.courseInfo.data,
	// 				current,
	// 				size
	// 			}
	// 		}
	// 		resolve(response)
	// 	}, 300)
	// })
	let allCourses = await request('platformStatusOverview/allCourses', params, 'get')
	console.log('022', allCourses)
	let records = []
	ForEach(allCourses.allCourses, (item) => {
		records.push({
			id: item.COURSE_ID,
			name: item.COURSE_NAME,
			teacher: item.TEACHER_NAME,
			startDate: timestampToYYYYMMDD(item.START_TIME),
			studentCount: item.STUDENT_COUNT,
			visits: item.VIEW_COUNT,
		})
	})
	let json = {
		code: 200,
		message: '获取成功',
		data : {
			records,
			total: 5,
			current: 1,
			size: 10
		},

	}

	return json
	// data: {
	// 	records: [
	// 		{
	// 			id: 1,
	// 			name: 'Python数据分析',
	// 			teacher: '王老师',
	// 			startDate: '2024-03-01',
	// 			studentCount: 320,
	// 			visits: '2,345'
	// 		},
	// 	],
	// 		total: 5,
	// 		current: 1,
	// 		size: 10
	// }



	// {
	// 	"TEACHER_ID": "1935565895337304066",
	// 	"TEACHER_NAME": "王五",
	// 	"VIEW_COUNT": 34,
	// 	"COURSE_ID": "1954728193252552705",
	// 	"STUDENT_COUNT": 0,
	// 	"ID": "1954819544520286208",
	// 	"COURSE_NAME": "农业课"
	// }


}

// 获取课程访问统计
export const getCourseVisitStats = async (params) => {
	const {courseId} = params

	// return new Promise((resolve) => {
	// 	setTimeout(() => {
	// 		// 生成近7天的日期
	// 		const days = []
	// 		const today = new Date()
	// 		for (let i = 6; i >= 0; i--) {
	// 			const date = new Date(today)
	// 			date.setDate(date.getDate() - i)
	// 			const month = (date.getMonth() + 1).toString().padStart(2, '0')
	// 			const day = date.getDate().toString().padStart(2, '0')
	// 			days.push(`${month}-${day}`)
	// 		}
	//
	// 		// 生成模拟访问数据
	// 		const baseVisits = [120, 156, 178, 210, 234, 245, 267]
	// 		const visitData = baseVisits.map((base) => {
	// 			const randomFactor = 0.8 + Math.random() * 0.4
	// 			return Math.round(base * randomFactor)
	// 		})
	//
	// 		const response = {
	// 			...mockData.courseVisitStats,
	// 			data: {
	// 				courseId,
	// 				visitData,
	// 				dateRange: days
	// 			}
	// 		}
	// 		resolve(response)
	// 	}, 300)
	// })
	let courseVisitTrend = await request('platformStatusOverview/courseVisitTrend', params, 'get')
	console.log('033', courseVisitTrend)
	const days = []
	const today = new Date()
	for (let i = 6; i >= 0; i--) {
		const date = new Date(today)
		date.setDate(date.getDate() - i)
		const month = (date.getMonth() + 1).toString().padStart(2, '0')
		const day = date.getDate().toString().padStart(2, '0')
		days.push(`${month}-${day}`)
	}
	let visitData = []
	ForEach(courseVisitTrend.visitTrend, (item) => {
		visitData.push(item.visitCount)
	})
	let json = {
		code: 200,
		message: '获取成功',
		data : {
			courseId,
			visitData,
			dateRange: days
		},

	}

	return json
}
