<template>
	<view class="add">
		<u-navbar title="添加排班" :background="background" :border-bottom='false'></u-navbar>
		<view class="form">
			<view class="item">
				<view class="label">
					排班日期
				</view>
				<view class="right">
					{{details.day}}
				</view>
			</view>
			<view class="item" v-if="details.time_id!=0">
				<view class="label">
					排班时间
				</view>
				<view class="right">
					{{details.start_time}}--{{details.end_time}}
				</view>
			</view>
			<view class="item" v-if="details.time_id==0">
				<view class="label">
					排班时间
				</view>
				<view class="right">
					<picker mode="selector" :range="time_set" :value="index" 
						:range-key="'range'" @change="bindTimeChange">
						<view class="uni-input">{{index ? time_set[index].range : '请选择时间段'}}</view>
					</picker>
				</view>
			</view>
			<view class="save" @click="add">
				确认添加
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background: 'rgba(255,255,255,0)',
				details: '',
				time_set: '', //时间段
				index: '',
			
			}
		},
		onLoad(options) {
			if(this.is_weixin()){
				this.navTitle()
			}
			this.details = options
			this.getTimeSet()
		},
		methods: {
			//判断是否是微信
			is_weixin() {
				let ua = navigator.userAgent.toLowerCase();
				return ua.indexOf('micromessenger') != -1;
			},
			navTitle() {
				let navTitle = document.getElementsByTagName('uni-page-head');
				navTitle[0].style.display = 'none'
			},
			//选择时间段
			bindTimeChange(e) {
				let index = e.detail.value
				console.log(e.detail.value, '-------------')
				this.index = index
				this.details.time_id = this.time_set[index].id
				this.details.start_time=this.time_set[index].start_time
				this.details.end_time=this.time_set[index].end_time
				console.log(this.range,'++++++++++++++++++++++')
			},
			//时间段配置
			getTimeSet() {
				this.$u.get('/mentor/time').then(res => {
					console.log(res, '--------')
					let list = res.data.list
					list.map(item => {
						item.range = item.start_time + '-' + item.end_time
					})
					this.time_set = list
				})
			},
			//添加
			add() {
				let data = {
					status: 1,
					teacher_id: this.$store.state.vuex_user.type_id,
					day: this.details.day,
					time_ids: this.details.time_id
				}
				this.$u.post('/mentor/scheduling', data).then(res => {
					console.log(res, '添加排班')
					if (res.code == 200) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						setTimeout(function() {
							uni.navigateBack()
						}, 2000);

					} else {

					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		height: 100%;
		background-image: url(../../static/images/add-bg.png);
		background-size: 100%;
		background-repeat: no-repeat;

		.form {
			margin: 0 28px;
			// background-color: #18B566;
			margin-top: 190px;

			.item {
				padding: 18px 20px;
				background-color: rgba(237, 244, 228, 1);
				opacity: 1;
				border-radius: 8px;
				display: flex;
				align-items: center;
				margin-bottom: 15px;

				.label {
					font-size: 16px;
					font-family: PingFang SC;
					font-weight: bold;
					line-height: 20px;
					color: #282828;
					opacity: 1;
				}

				.right {
					text-align: right;
					flex: 1;
					font-size: 12px;
					font-family: PingFang SC;
					font-weight: 400;
					line-height: 20px;
					color: #282828;
					opacity: 0.42;

					.no {
						font-size: 12px;
						font-family: PingFang SC;
						font-weight: 400;
						line-height: 20px;
						color: #282828;
						opacity: 0.42;
					}
				}
			}

			.save {
				height: 56px;
				background: #96203a;
				opacity: 1;
				border-radius: 8px;
				line-height: 56px;
				text-align: center;
				font-size: 18px;
				margin-top: 63px;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				opacity: 1;
			}
		}
	}
</style>
