<template>
	<view class="page-prebook">
		<view class="title flex">
			<img :src="cssUrl+'ten.svg'">
			{{depart.h_name}} {{depart.depart_name}}
		</view>
		<book ref="book" @confirmSubmitEvent="showConfirm" :RegistrationTimeList="RegistrationTimeList" :visitorList="activeVisitors"/>
		<slider-box ref="sliderbox">
			<view class="slider-title PfScMedium">预约信息确认</view>
			<view class="members-box" v-if="needSelectMembers">
				<view class="desc">当前时段余号不足，只能预约{{times.remain_num}}人</view>
				<view class="members">
					<view :class="'memmbers-item'+(item.choose?' active':'')" v-for="(item,key) in activeVisitors" @click="chooseVisitor(key)">
						<img :src="cssUrl+'checkbox_false.svg'">
						<img :src="cssUrl+'checkbox_true.svg'">
						{{item.visitor_name}}
					</view>
				</view>
			</view>
			<view class="item member-style" v-else>
				<view class="left">就诊人：</view>
				<view class="right over2 over5">
					<text v-for="(item,key) in activeVisitors">
						<text v-if="key<=9">{{nums[key]}}</text><text v-else>n.</text>{{item.visitor_name}}
					</text>
				</view>
			</view>
			<view class="item" >
				<view class="left">医生姓名：</view>
				<view class="right">{{doctorInfo.doctor_info.name}}</view>
			</view>
			<view class="item">
				<view class="left">就诊医院：</view>
				<view class="right over2 over5">{{depart.h_name}} {{depart.depart_name}}</view>
			</view>
			<view class="item">
				<view class="left">看诊时间：</view>
				<view class="right orange">{{times.date}} {{times.time_desc}} {{times.start_time}}-{{times.end_time}}</view>
			</view>

			<view class="btns-box flex">
				<view class="btn cancel btn1" hover-class="hover" @click="closeConfirm">我再想想</view>
				<view class="primary btn btn1" hover-class="hover" @click="submitConfim">确认预约</view>
			</view>
		</slider-box>
	</view>
</template>

<script>
	import Book from '@/components/bookBox.vue'
	import SliderBox from '@/components/sliderBox.vue'
	export default {
		onLoad(op) {
		},
		data() {
			return {
				cssUrl:this.cssUrl,
				needSelectMembers:false,
				doctorId:false,
				RegistrationTimeList:[],
				hospitalId:false,
				hospital:false,
				activeVisitors:[],
				nums:['①','②','③','④','⑤','⑥','⑦','⑧','⑨','⑩'],
				times:false,
				chooseVisitorCount:0,
				departId:false,
				depart:false,
				visit_nums_id:false
			}
		},
		components:{
			Book,
			SliderBox
		},
		onShow() {
			var activeVisitors = uni.getStorageSync('active_visitors')
			if(!activeVisitors){
				this.$func.toPage('/pages/index/index')
				return
			}

			this.activeVisitors = JSON.parse(activeVisitors)
			this.departId = uni.getStorageSync('depart_id')
			this.depart = uni.getStorageSync('depart')
			var doctorInfo = uni.getStorageSync('doctor_info')
			if(!doctorInfo){
				this.$pop.modelShow('请先选择要预约的医生', '/pages/index/index')
				return
			}
			this.doctorInfo = JSON.parse(doctorInfo)
			if(!this.departId && this.depart) {
				this.$pop.modelShow('请先选择要预约的医院科室', '/pages/index/index')
				return
			}
			this.depart = JSON.parse(this.depart)
			this.getAvailabletRegistrationTime()
		},
		methods: {
			chooseVisitor(key){
				var visitor = this.activeVisitors[key]
				var choose = visitor.choose ? false : true
				var chooseCount = 0
				for (var i = 0; i < this.activeVisitors.length; i++) {
					if(this.activeVisitors[i].choose) ++chooseCount
				}

				if(this.times.left_count <= chooseCount && choose){
					this.$pop.modelShow('最多预约'+this.times.left_count+'人')
					return
				}

				visitor.choose = choose
				this.$set(this.activeVisitors, key, visitor)
			},
			tips(){
				uni.removeStorageSync('active_visitors')
				uni.showToast({
					title:"成功预约"+this.chooseVisitorCount+"人",
					mask:true,
					image:this.cssUrl+'gou.svg',
					times:false,
					success() {
						setTimeout(function(){
							uni.navigateTo({
								url:"/pages/prebook_list/prebook_list"
							})
						}, 1500)
					}
				})
			},
			getAvailabletRegistrationTime(refresh = true, chooseDay = '') {
				this.$http.req('get_appoint_times',{depart_id:this.departId}, 'GET').then(data=>{
					if(data == -1) return
					// data.days = [
					// 	{
					// 		date: "2024-04-17", type: 2, type_text: "上午出诊", weekday: "周一", 
					// 		am: [
					// 			{
					// 				"start": "12:00",
					// 				"end": "12:30",
					// 				"bed_count": 0
					// 			},
					// 			{
					// 				"start": "13:00",
					// 				"end": "14:30",
					// 				"bed_count": 2
					// 			}
					// 		],
					// 		pm: []
					// 	},
					// 	{
					// 		date: "2024-04-18", type: 5, type_text: "未开放预约", weekday: "周二", am: [], pm: []
					// 	},
					// 	{
					// 		date: "2024-04-19", type: 3, type_text: "全天出诊", weekday: "周三", am: [
					// 			{
					// 				"start": "12:00",
					// 				"end": "12:30",
					// 				"bed_count": 0
					// 			},
					// 			{
					// 				"start": "13:00",
					// 				"end": "14:30",
					// 				"bed_count": 2
					// 			}
					// 		], pm: [
					// 			{
					// 				"start": "12:00",
					// 				"end": "12:30",
					// 				"bed_count": 0
					// 			},
					// 			{
					// 				"start": "13:00",
					// 				"end": "14:30",
					// 				"bed_count": 8
					// 			}
					// 		]
					// 	},
					// 	{
					// 		date: "2024-04-20", type: 4, type_text: "下午出诊", weekday: "周四", am: [], pm: [
					// 			{
					// 				"start": "12:00",
					// 				"end": "12:30",
					// 				"bed_count": 0
					// 			},
					// 			{
					// 				"start": "13:00",
					// 				"end": "14:30",
					// 				"bed_count": 1
					// 			}
					// 		]
					// 	},
					// 	{
					// 		date: "2024-04-21", type: 1, type_text: "全天休息", weekday: "周五", am: [], pm: []
					// 	},
					// ]

					// if(!refresh){
					// 	// data.days[3].pm[1].bed_count=0
					// 	// data.days[3].type=1
					// 	data.days = [
					// 		{
					// 			date: "2024-04-17", type: 2, type_text: "上午出诊", weekday: "周一", 
					// 			am: [
					// 				{
					// 					"start": "12:00",
					// 					"end": "12:30",
					// 					"bed_count": 0
					// 				},
					// 				{
					// 					"start": "13:00",
					// 					"end": "14:30",
					// 					"bed_count": 2
					// 				}
					// 			],
					// 			pm: []
					// 		},
					// 		{
					// 			date: "2024-04-18", type: 5, type_text: "未开放预约", weekday: "周二", am: [], pm: []
					// 		}
					// 	]
					// }

					this.RegistrationTimeList = data
					if(this.RegistrationTimeList.length>0) {
						if(refresh || chooseDay>=this.RegistrationTimeList.length) {
							this.$refs.book.dataInit(this.RegistrationTimeList[0])
						}else{
							this.$refs.book.chooseDay(chooseDay, this.RegistrationTimeList[chooseDay])
						}
					}
				});
			},
			closeConfirm() {
				this.$refs.sliderbox.closeConfirm()
			},
			appoint() {
				var param = new Object()
				param.depart_id = this.departId
				param.visit_nums_id = this.visit_nums_id
				var uids = new Array()
				for (var i = 0; i < this.activeVisitors.length; i++) {
					var visitor = this.activeVisitors[i]
					if(!this.needSelectMembers){
						uids.push(visitor.id)
						continue
					}
					if(!visitor.choose) continue
					uids.push(visitor.id)
				}

				if(uids.length <= 0){
					this.$pop.modelShow('请选择预约就诊人')
					return
				}

				if(uids.length > this.times.remain_num){
					this.$pop.modelShow('当前时段余号不足，只能预约'+this.times.remain_num+'人')
					return
				}
				param.visitor_ids = uids.join(',')

				this.chooseVisitorCount = uids.length
				this.$http.req('appoint', param).then(data=>{
					if(data == -1) return
					this.tips()
				});
			},
			appointCheck(times, chooseDay='', visit_nums_id){
				var param = new Object()
				param.depart_id = this.departId
				this.visit_nums_id = param.visit_nums_id = visit_nums_id

				var visitor_ids = new Array();
				for (var i = 0; i < this.activeVisitors.length; i++) {
					visitor_ids.push(this.activeVisitors[i].id)
				}
				param.visitor_ids = visitor_ids.join(',')

				this.times = param
				this.times.date = times.date.date
				this.times.start_time = times.start
				this.times.end_time = times.end
				this.times.time_desc = '下午'
				if(times.type == 1) this.times.time_desc = '上午'

				this.$http.req('check_appoint_nums', param).then(data=>{
					if(data == -1) {
						this.getAvailabletRegistrationTime(false, chooseDay)
						return
					}

					if(data.remain_num < this.activeVisitors.length){
						this.needSelectMembers = true
						this.$set(this.times, 'remain_num', data.remain_num)
						this.$refs.sliderbox.showConfirm()
					}else{
						this.$refs.sliderbox.showConfirm()
					}
				});
			},
			showConfirm(times) {
				this.appointCheck(times, times.key, times.id)
			},
			submitConfim(){
				this.appoint()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-prebook{
		width: 100%;
		height: 100vh;
		overflow-y: auto;
		background: #F8F8F8;
		.title{
			margin-top: 24rpx;
			align-items: center;
			width: 100%;
			box-sizing: border-box;
			padding: 0 41rpx;
			line-height: 36rpx;
			font-size: 30rpx;
			color: #949699;
			img{
				width: 26rpx;
				height: 26rpx;
				margin-right: 15rpx;
			}
		}
		.slider-title{
			width: 100%;
			text-align: center;
			color: #000000;
			margin-top: 52rpx;
			line-height: 50rpx;
			height: 50rpx;
			font-size: 32rpx;
			letter-spacing: 2rpx;
		}
		.member-style{
			margin-top: 82rpx;
			text{
				margin-right: 10rpx;
				text{
					margin-right: 5rpx;
				}
			}
		}
		.item{
			display: flex;
			box-sizing: border-box;
			padding: 0 53rpx;
			min-height: 42rpx;
			line-height: 42rpx;
			font-size: 30rpx;
			margin-bottom: 24rpx;
			.left{
				width: 159rpx;
				flex-shrink: 0;
				color: #999999;
			}
			.right{
				flex-grow: 1;
				color: #242833;
				&.orange{
					color: #FD7100;
				}
			}
		}
		.item:last-of-type{
			margin-bottom: 62rpx;
		}
		.btns-box{
			justify-content: space-between;
			width: 750rpx;
			height: 124rpx;
			padding: 0 50rpx;
			box-sizing: border-box;
			align-items: center;
			background: #FFFFFF;
			margin-bottom: 17rpx;
			box-shadow: 0rpx -2rpx 6rpx 0rpx rgba(181,181,181,0.13);
			margin-top: 62rpx;
		}
		.members-box{
			margin-top: 52rpx;
			padding-left: 60rpx;
			width: 100%;
			box-sizing: border-box;
			.desc{
				height: 50rpx;
				font-size: 30rpx;
				color: #999999;
				line-height: 50rpx;
				letter-spacing: 1rpx;
				margin-bottom: 20rpx;
			}
			.members{
				width: 630rpx;
				min-height: 200rpx;
				max-height: 230rpx;
				overflow-y: auto;
				background: #F7F7F7;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				padding: 32rpx 40rpx;
				box-sizing: border-box;
				font-size: 28rpx;
				color: #000000;
				line-height: 40rpx;
				margin-bottom: 52rpx;
				row-gap: 50rpx;
				.memmbers-item{
					width: 50%;
					overflow-y: auto;
					display: flex;
					flex-shrink: 0;
					align-items: center;
					word-break: break-all;
					img{
						width: 36rpx;
						height: 36rpx;
						margin-right: 26rpx;
					}
					img:first-of-type{
						display: block;
					}
					img:last-of-type{
						display: none;
					}
					&.active{
						img:first-of-type{
							display: none;
						}
						img:last-of-type{
							display: block;
						}
					}
				}
			}
		}
	}
</style>
