<template>
	<view>
		<my-coursedetail v-if="showDetail" :item="course" :location="location" :marker="marker" :timeAM="timeAM" :timePM="timePM" :timeEvening="timeEvening" buttonMessage="邀请" :collect="collect" :isShowCollect="true" @collecting="collecting" @publishButton="invite"></my-coursedetail>

		<my-invite :isShowInvite="isShowInvite" :item11="myNeeds" @cancelChosed="cancelChosed" @commitChosed="commitChosed" @clickRadio="clickRadio"></my-invite>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import formTime from '@/mixins/form-time.js'
	export default {
		mixins: [formTime],
		computed: {
			...mapState('m_user', ['userinfo'])
		},
		data() {
			return {
				showDetail: false,
				isShowInvite: false,
				initeNeeds: '',
				myNeeds: [],
				collect: false,
				course: {},
				location: [],
				marker: [{
					id: 1,
					joinCluster:true,
					latitude: 0,
					longitude: 0,
					width: 40,
					height: 40,
					iconPath: '/static/location.png',
				}],
			};
		},
		onLoad(option) {
			if (option.item !== undefined) {
				this.course = JSON.parse(decodeURIComponent(option.item))
				this.getCourseDetailById(this.course.courseId)
			}
			if (option.courseId !== undefined) {
				this.getPersonCourseDetailById(option.courseId)
			}
			this.getIsCollection()
		},
		methods:{
			async getPersonCourseDetailById(id) {
				const {data: result} = await uni.$http.get('/education/teacher-courses/getPersonCourseDetailById', {courseId: id})
				this.course = result.data.one
				if (this.course.viewedCount === null) {
					this.course.viewedCount = 0
				}
				this.formatForm()
			},
			// 获取课程详情
			async getCourseDetailById(id) {
				const {data: result} = await uni.$http.get('/education/teacher-courses/getCourseDetailById', {courseId: id})
				const item = result.data.one
				this.course = Object.assign(this.course, item)
				if (this.course.viewedCount === null) {
					this.course.viewedCount = 0
				}
				this.formatForm()
			},
			// 上课表格格式化
			formatForm() {
				this.location = this.course.locationAl.split(",")
				this.marker[0].latitude = Number(this.location[0])
				this.marker[0].longitude = Number(this.location[1])
				this.courseWeekday = this.course.teachTime.split(",")
				this.parseCourseWeekday()
				this.showDetail = true
			},
			// 查看是否收藏过
			async getIsCollection() {
				const { data: result } = await uni.$http.get('/education/my-favorite-courses/findPersonCollect')
				const list = result.data.requires
				if (list !== undefined) {
					for (let i = 0; i < list.length; i++) {
						if (this.course.courseId === list[i]) {
							this.collect = true
						}
					}
				}
			},
			// 收藏、取消收藏
			async collecting() {
				const queryObj = {
					courseId: this.course.courseId
				}
				// 没有收藏过
				if (!this.collect) {
					const { data: result1 } = await uni.$http.get('/education/my-favorite-courses/collect', queryObj)
					if (result1.message === '收藏成功') {
						return this.collect = true
					} 
					if (result1.message === '收藏失败') {
						return uni.$showMsg('收藏失败')
					}
				}
				// 收藏过
				if (this.collect) {
					const { data: result } = await uni.$http.get('/education/my-favorite-courses/cancelCollect', queryObj)
					if (result.message === '取消收藏成功') {
						return this.collect = false
					} else if (result.message === '取消收藏失败') {
						return uni.$showMsg('取消收藏失败')
					}
				}
			},
			async showMyStuNeeds() {
				const { data: result } = await uni.$http.get('/education/student-requirements/queryPersonSomeInfo')
				this.myNeeds = result.data.list
			},
			invite() {
				this.isShowInvite = true
				this.showMyStuNeeds()
			},
			cancelChosed() {
				this.isShowInvite = false
			},
			async commitChosed() {
				if (this.initeNeeds === '') return uni.$showMsg('请选择邀请的课程')
				this.initeNeeds.toUid = this.course.uid
				this.initeNeeds.courseId = this.course.courseId
				this.initeNeeds.fromUname = this.initeNeeds.name
				this.initeNeeds.toUname = this.course.name
				
				const {data: result} = await uni.$http.post('/education/invite-info/sendTeacherInvite', this.initeNeeds)
				uni.$showMsg(result.message)
				this.isShowInvite = false
			},
			clickRadio(item) {
				this.initeNeeds = item
			}
		}
	}
</script>

<!-- 设置页面背景 -->
<style lang="scss">
	page{
		height: 100%;
		// background-color: #FFF;
	}
</style>

<style lang="scss" scoped>
</style>
