import request from '../request'
import dayjs from 'dayjs'
import Mock from 'mockjs'

const cleanData = (item) => {
	return {
		pid: item.teachingVideoTypeId,
		videoUrl: item.fileUrl,
		cover: item.coverFileUrl,
		title: item.title,
		remarks: item.videoDescribe,
		permission:item.permission,
		duration: dayjs(item.videoDuration * 1000).subtract(8, 'hour').format('HH:mm:ss'),
	}
}

//学车视频api
class CarVideo {
	/**
	 * 获取短视频列表
	 */
	async getShortVideoList(params) {
		const res = await request({
			url: '/open-api/teachingDsp/dsp/list',
			method: 'get',
			params
		})

		const list = res.rows.map((item) => {
			return {
				videoUrl: item.fileUrl,
				cover: item.coverFileUrl,
				title: item.title,
				remarks: item.videoDescribe,
				duration: dayjs(item.videoDuration * 1000).subtract(8, 'hour').format('HH:mm:ss'),
			}
		})

		const data = {
			code: 200,
			msg: '请求成功',
			data: {
				chapterName: '短视频',
				list: list,
				total: res.total
			}
		}

		return data
	}
	/**
	 * 获取学车视频列表
	 */
	async getCarVideoList(id) {
		let res = await request({
			url: `https://zzjs.zzxcx.net/prod-api/open-api/teachingVideo/info/getTeachingVideoByTypeId`,
			params: {
				videoTypeId: id
			}
		})

		res.data = res.data.map(item => {
			return {
				pid: item.teachingVideoTypeId,
				videoUrl: item.fileUrl,
				cover: item.coverFileUrl,
				title: item.title,
				permission:item.permission,
				remarks: item.videoDescribe,
				duration: dayjs(item.videoDuration * 1000).subtract(8, 'hour').format(
					'HH:mm:ss'),
			}
		})

		return res
	}
	/**
	 * 获取视频列表树形结构
	 */
	async getTreeList() {
		let res = await request({
			url: `https://zzjs.zzxcx.net/prod-api/open-api/teachingVideo/info/treeList`
		})


		const mapFn = (obj) => {
			let newObj = {}
			newObj.chapterName = obj.typeName
			newObj.id = obj.id
			newObj.children = obj.children.map(mapFn)
			newObj.list = obj.videos.map(cleanData)

			return newObj
		}


		res.data = res.data.map(mapFn)

		return res
	}
	/**
	 * 查询视频列表
	 */
	async getSearchList(keyword) {

		const res = await request({
			url: `open-api/teachingVideo/info/search`,
			params: {
				keyword
			}
		})
		
		const list = res.data.map((item) => {
			return {
				pid: item.teachingVideoTypeId,
				videoUrl: item.fileUrl,
				cover: item.coverFileUrl,
				title: item.title,
				remarks: item.videoDescribe,
				duration: dayjs(item.videoDuration * 1000).subtract(8, 'hour').format('HH:mm:ss'),
			}
		})
		
		const data = {
			chapterName: keyword,
			list: list
		}
		
		res.data = data

		return res
	}
}


export default new CarVideo
