<template>
	<view style="background-color: #E2F0D9;">
		<my-requiredetail v-if="showDetail" :item="item" :location="location" :marker="marker" :timeAM="timeAM" :timePM="timePM" :timeEvening="timeEvening" :collect="collect" :isShowCollect="isShowCollect" buttonMessage="邀请" @collecting="collecting" @publishButton="invite"></my-requiredetail>
		
		<my-invite :isShowInvite="isShowInvite" :item11="myCourses" @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,
				initeCourse: '',
				myCourses: [],
				isShowInvite: false,
				isShowCollect: true,
				collect: false,
				item: {},	// 需求详情
				location: [],
				marker: [{
					id: 1,
					joinCluster:true,
					latitude: 0,
					longitude: 0,
					width: 40,
					height: 40,
					iconPath: '/static/location.png',
				}],
			};
		},
		onLoad(option) {
			if (option.requireId !== undefined) {
				const requireId = decodeURIComponent(option.requireId)
				this.getStuNeedsDetailById(requireId)
			}
			this.getIsCollection()
		},
		methods: {
			// 获取需求详情
			async getStuNeedsDetailById(id) {
				const {data: result} = await uni.$http.get('/education/student-requirements/getStuNeedsDetailById', {requireId: id})
				this.item = result.data.one
				this.formatForm()
			},
			// 上课表格格式化
			formatForm() {
				this.location = this.item.locationAl.split(",")
				this.marker[0].latitude = Number(this.location[0])
				this.marker[0].longitude = Number(this.location[1])
				this.courseWeekday = this.item.courseWeekday.split(",")
				this.parseCourseWeekday()
				this.showDetail = true
			},
			// 查看是否收藏过
			async getIsCollection() {
				const { data: result } = await uni.$http.get('/education/my-favorite-needs/findPersonCollect')
				const list = result.data.requires
				if (list !== null) {
					for (let i = 0; i < list.length; i++) {
						if (this.item.requireId === list[i]) {
							this.collect = true
						}
					}
				}
			},
			// 收藏、取消收藏
			async collecting() {
				const queryObj = {
					requireId: this.item.requireId
				}
				if (!this.collect) {
					const { data: result1 } = await uni.$http.get('/education/my-favorite-needs/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-needs/cancelCollect', queryObj)
					if (result.message === '取消收藏成功') {
						return this.collect = false
					}
					if (result.message === '取消收藏失败') {
						return uni.$showMsg('取消收藏失败')
					}
				}
			},
			invite() {
				this.isShowInvite = true
				this.showMyCourse()
			},
			async showMyCourse() {
				const { data: result } = await uni.$http.get('/education/teacher-courses/queryPersonSomeInfo')
				this.myCourses = result.data.list
				const name = result.data.name
				if (this.myCourses !== undefined) {
					for (let i = 0; i < this.myCourses.length; i++) {
						this.myCourses[i].fromUname = name
					}
				}
			},
			cancelChosed() {
				this.isShowInvite = false
			},
			async commitChosed() {
				if (this.initeCourse === '') return uni.$showMsg('请选择邀请的课程')
				this.initeCourse.toUid = this.item.uid
				this.initeCourse.requireId = this.item.requireId
				this.initeCourse.salary = this.item.salary
				this.initeCourse.toUname = this.item.name
				
				const {data: result} = await uni.$http.post('/education/invite-info/sendInvite', this.initeCourse)
				uni.$showMsg(result.message)
				this.isShowInvite = false
			},
			clickRadio(item) {
				this.initeCourse = item
			}
		}
	}
</script>

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