<template>
	<view class="requireWrapper">
		<view v-for="(item, index) in requirement" :key="index">
			<view class="requires" @click="requireDetail(item)">
				<uni-row class="demo-uni-row">
					<uni-col offset="1">
						<view class="requires-col1">需求号：{{item.requireId}}</view>
					</uni-col>
				</uni-row>
				<uni-row class="demo-uni-row">
					<uni-col offset="1" :span="16">
						<view class="requires-col2">需求科目：{{item.subjectSmall}}</view>
					</uni-col>
					<uni-col :span="7">
						<view>
							<button @click.stop="open(item)" size="mini" :type="item.buttonType">{{item.display}}</button>
						</view>
					</uni-col>
				</uni-row>
				<view class="requires-col3">
					<uni-row class="demo-uni-row">
						<uni-col :span="11" offset="1">
							<view>需求金额：{{item.salary}}</view>
						</uni-col>
						<uni-col :span="4" >
							<view style="color: red;">{{item.verifyStatus}}</view>
						</uni-col>
						<uni-col :span="4" >
							<view style="color: red;">{{item.deal}}</view>
						</uni-col>
						<uni-col :span="4" >
							<view style="color: red;">{{item.locked}}</view>
						</uni-col>
					</uni-row>
				</view>
			</view>
		</view>
		
		<uni-popup ref="popup" type="message">
			<uni-popup-message :type="popupType" :message="showMessage" :duration="1000"></uni-popup-message>
		</uni-popup>
		
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	
	export default {
		data() {
			return {
				requirement: [],	// 需求列表
				publishCount: 0,	// 上架数量
				showMessage: '上架成功',
				popupType: 'success'
			};
		},
		onShow() {
			this.getPublishCounts()
		},
		methods: {
			// 查询个人上架了多少课程
			async getPublishCounts() {
				const { data: result } = await uni.$http.get('/education/student-requirements/getPublishCounts')
				
				this.publishCount = result.data.publishCounts
				let courses = []
				courses = result.data.list
				
				for (let i = 0; i < courses.length; i++) {
					if (courses[i].display === '上架') {
						courses[i].display = '下架'
						courses[i].buttonType = 'warn'
					} else {
						courses[i].display = '上架'
						courses[i].buttonType = 'primary'
					}
				}
				this.requirement = courses
			},
			requireDetail(item) {
				uni.navigateTo({
					url: '/subpkg/student/require/student_require_my_detail?requireId=' + encodeURIComponent(item.requireId)
				})
			},
			async open(item) {
				if (item.display === '上架') {
					if (item.verifyStatus !== '已通过') return uni.$showMsg('审核通过才能上架')
					if (item.deal === '已成交') return uni.$showMsg('已成交的需求不能上架')
					if (item.locked === '已锁') return uni.$showMsg('正在锁定的需求不能上架')
					if (this.publishCount >= 4) return uni.$showMsg('最多发布4个需求')
				}
				const queryObj = {
					display: item.display,
					requireId: item.requireId
				}
				const { data: result } = await uni.$http.get('/education/student-requirements/updatePersonDisplay', queryObj)
				if (result.code === 20000) {
					if (item.display === '上架') {
						item.display = '下架'
						this.showMessage = '上架成功'
						this.popupType = 'success'
						item.buttonType = 'warn'
						this.publishCount += 1
						
					} else if (item.display === '下架') {
						item.display = '上架'
						this.showMessage = '下架成功'
						this.popupType = 'error'
						item.buttonType = 'primary'
						this.publishCount -= 1
					}
					this.$refs.popup.open('top')
				}
			}
		}
	}
</script>

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

<style lang="scss" scoped>
.requireWrapper{
		width: 96%;
		height: 100%;
	}
	
.requires {
	background-color: #FFF;
	border: 1px solid #41719C;
	border-radius: 30rpx;
	margin: 10rpx 0 20rpx;
	position: relative;
	
	.requires-col1 {
		margin-top: 15rpx;
	}
	
	.requires-col2 {
		margin: 15rpx 0;
	}
	
	.requires-col3 {
		padding-bottom: 15rpx;
	}
	
	button{
		position: absolute;
		right: 40rpx;
	}
}
</style>
