<template>
		<view>
	<view>
		<my-requiredetail v-if="showDetail" :item="item" :location="location" :marker="marker" :timeAM="timeAM" :timePM="timePM" :timeEvening="timeEvening" :isShowPrivate="isShowPrivate"  buttonMessage="用作模板极速发布"  :isShowBtn="showButton" @publishButton="publishButton" ></my-requiredetail>
	</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,
				isShowPrivate: true,
				item: {},
				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.requireId !== undefined) {
				const requireId = decodeURIComponent(option.requireId)
				this.getStuNeedDetailById(requireId)
				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/student-requirements/deleteNeedById', {requireId:  this.item.requireId})
					if (result.code === 20000) { 
						uni.$showMsg('删除成功')
						uni.redirectTo({
							url:'/subpkg/student/require/student_require_pattern'
						})
					}else{
						uni.$showMsg('删除失败')
					}
			},
			// 上架，下架处理
			async changeDisplay(){
				// 查询个人上架了多少课程
				const { data: result1 } = await uni.$http.get('/education/student-requirements/getPublishCounts')
				//console.log(result)
				this.publishCount = result1.data.publishCounts
				
				if (this.item.display === '下架') {
					if (this.item.verifyStatus !== '已通过') return uni.$showMsg('审核通过才能上架')
					if (this.item.deal === '已成交') return uni.$showMsg('已成交的课程不能上架')
					if (this.item.locked === '已锁') return uni.$showMsg('正在锁定的需求不能上架')
					if (this.publishCount >= 4) return uni.$showMsg('最多上架4个，上架失败')
				}	
				const queryObj = {
					display:  this.DisplayBtnName,
					requireId: this.item.requireId
				}
				const { data: result } = await uni.$http.get('/education/student-requirements/updatePersonDisplay', queryObj)
				if (result.code === 20000) {
					if (this.DisplayBtnName === '上架') {
						this.item.display = '上架'
						 uni.$showMsg('上架成功')
						 this.DisplayBtnName = '下架'
						 
					} else if (this.DisplayBtnName === '下架') {
						this.item.display = '下架'
						 uni.$showMsg('下架成功')
						this.DisplayBtnName = '上架'
					}
				}
			},
			// 获取需求详情
			async getStuNeedDetailById(id) {
				const {data: result} = await uni.$http.get('/education/student-requirements/getPublishStuNeedsDetailById', {requireId: id})
				if (result.data.one.profilePhoto.length == 0) {
					if(result.data.one.sex == "男"){
						result.data.one.profilePhoto = "../../static/boy.png"
					}else{
						result.data.one.profilePhoto = "../../static/girl.png"
					}
				}
				this.item = result.data.one
				this.formatForm()
				if(this.item.display == "上架") {
					this.DisplayBtnName ="下架"
				}else{
					this.DisplayBtnName ="上架"
				}
			},
			// 上课表格格式化
			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
			},

			publishButton() {
				uni.redirectTo({
					url:'/subpkg/student/require/student_require_add?item='
					+ encodeURIComponent(JSON.stringify(this.item))
				})
			}
		}
	}
</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>
