<template>
		<view>
	<view>
		<my-coursedetail v-if="showDetail" :item="course" :location="location" :marker="marker" :timeAM="timeAM" :timePM="timePM" :timeEvening="timeEvening" buttonMessage="用作模板极速发布" :isShowCollect="false" :isShowPrivate="true" :isShowBtn="showButton" @publishButton="publishButton"></my-coursedetail>
	</view>
	<view class="button-wrapper">
		<button class="button1" @click="changeDisplay">{{DisplayBtnName}}</button>
		<button class="button2" @click="deleteThisCourse">删除</button>
	</view>
		</view>
</template>

<script>	
	import formTime from '@/mixins/form-time.js'
	export default {
		mixins: [formTime],
		data() {
			return {
				publishCount: 0,
				DisplayBtnName: "上架",
				showButton: false,
				showDetail: false,
				course: {},
				location: [],
				marker: [{
					id: 1,
					joinCluster:true,
					latitude: 0,
					longitude: 0,
					width: 40,
					height: 40,
					iconPath: '/static/location.png',
				}],
			};
		},
		onLoad(option) {			
					//	console.log(option)
			if (option.courseId !== undefined) {
				const courseId = decodeURIComponent(option.courseId)
				this.getCourseDetailById(courseId)
				if (option.code !== undefined) {
					const code = decodeURIComponent(option.code)
					if (code == 0){this.showButton = true }
					}
			}
		},
		methods:{
			async deleteThisCourse(){
				const {data: result} = await uni.$http.get('/education/teacher-courses/deleteCourseByCourseId', {courseId: this.course.courseId})
					if (result.code === 20000) { 
						uni.$showMsg('删除成功')
						//uni.redirectTo（关闭当前页面，跳转到其他页面）
						//uni.navigateTo 保留当前页面，跳转到应用内的某个页面，使用uni.navigateBack可以返回到原页面。
						uni.redirectTo({
							url:'/subpkg/teacher/course/teacher_course_pattern'
						})
					}else{
						uni.$showMsg('删除失败')
					}
			},
			// 上架，下架处理
			async changeDisplay(){
				// 查询个人上架了多少课程
				const { data: result1 } = await uni.$http.get('/education/teacher-courses/showPersonCourse')
				//console.log(result)
				this.publishCount = result1.data.publishCounts
				
				if (this.course.display === '下架') {
					if (this.course.verifyStatus !== '已通过') return uni.$showMsg('审核通过才能上架')
					if (this.course.deal === '已成交') return uni.$showMsg('已成交的课程不能上架')
					if (this.course.locked === '已锁') return uni.$showMsg('正在锁定的需求不能上架')
					if (this.publishCount >= 4) return uni.$showMsg('最多上架4个课程，上架失败')
				}	
				const queryObj = {
					display:  this.DisplayBtnName,
					courseId: this.course.courseId
				}
				const { data: result } = await uni.$http.get('/education/teacher-courses/updatePersonDisplay', queryObj)
				if (result.code === 20000) {
					if (this.DisplayBtnName === '上架') {
						this.course.display = '上架'
						 uni.$showMsg('上架成功')
						 this.DisplayBtnName = '下架'
						 
					} else if (this.DisplayBtnName === '下架') {
						this.course.display = '下架'
						 uni.$showMsg('下架成功')
						this.DisplayBtnName = '上架'
					}
				}
			},
			// 获取课程详情
			async getCourseDetailById(id) {
				const {data: result} = await uni.$http.get('/education/teacher-courses/getPersonCourseDetailById', {courseId: id})
				this.course = result.data.one
			//	console.log(this.course)
				this.formatForm()
				if(this.course.display == "上架") {
					this.DisplayBtnName ="下架"
				}else{
					this.DisplayBtnName ="上架"
				}
			},
			// 上课表格格式化
			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
			},
			publishButton() {
				uni.redirectTo({
					url:'/subpkg/teacher/course/teacher_course_add?item=' + encodeURIComponent(JSON.stringify(this.course))
				})
			}
		}
	}
</script>

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

<style lang="scss" scoped>
	/* 底部按钮 */
		.button-wrapper{
	padding-top: 60rpx;
		    display: flex;
		    width: 100%;
		    justify-content: space-around;
		   // position: fixed;
		   // bottom: 40rpx;
	padding-bottom: 60rpx;
		}
		.button1,
		.button2{
		    font-size: 30rpx;
		    padding: 0rpx 60rpx;
		    border-radius: 50rpx;
		    color: white;
		}
		
		.button1{
			background-color: #3ed598;
		}
		
		.button2{
			
			background-color: #35b882;
		}
</style>
