<template>
	<view class="book-page btPadding" v-if="bookInfo">
		<view class="list-box">
			<list-box class="item-wrapper" @editevent="edit" v-if="item.show" v-for="(item,key) in list" :key="key" :indexkey="key" :info="item" :leftTitle="item.leftTitle" :rightTitle="item.rightTitle?item.rightTitle:''">
				<!-- #ifdef H5 -->
				<view class="item-inner" v-html="item.custom" v-if="item.custom" @click="edit" :data-item="item" :data-key="key"></view>
				<!-- #endif -->
			</list-box>
		</view>

		<view class="bot-con" v-if="hasRecord && visitorInfo && (visitorInfo.single_items || visitorInfo.treatment_package)">
			<view class="title PfScMedium">治疗记录</view>
			<view class="visitor-account">
				<VisitorAccount :isOpen="visitorAccountOpen" :visitorInfo="visitorInfo" :open="true"/>
			</view>
		</view>

		<view class="bot-con" v-if="notice">
			<view class="title PfScMedium">就诊提醒</view>
			<view class="content" v-html="notice"></view>
		</view>
		<view class="btPadding pagebt"></view>

		<view class="fixedBot btPadding">
			<view class="submit">
				<view :class="'btn cancel btn1'+(!showCancelBook ?' btnauto': '') +  (((bookInfo.status == 2 || bookInfo.status == 1) && bookInfo.is_checked != '1') ?' btn3' : '')" hover-class="hover" @click="reback">返回</view>
				<view class="btn primary btn1" hover-class="hover" v-if="showCancelBook && bookInfo.status != 2" @click="cancelBook">取消预约</view>
			</view>
		</view>

		<view :class="'ccpop'+(popkey==1?' bed-edit-pop':'')">
			<uni-popup ref="popup" type="bottom" background-color="#fff">
				<view class="edit-pop-title PfScSemibold">{{poptitle}}</view>
				<user-card :ref="'usercard_'+key" v-for="(item, key) in visitorList" :dataitem="item" @clickEvent="changeType" :index="key" :showuinfo="false"></user-card>
				<view class="submit flex submit-popup">
					<view :class="'btn cancel'+(' btn1')" hover-class="hover" @click="closepop">取消</view>
					<view class="btn primary btn1" hover-class="hover" @click="submitEdit">确认修改</view>
				</view>

			</uni-popup>
		</view>


	</view>
</template>

<script>
	import listBox from '@/components/listBox.vue';
	import visitorAccount from '@/components/visitorAccount.vue'
	import userCard from '@/components/userCard.vue';

	export default {
		options: { styleIsolation: "shared" },
		data() {
			return {
				statusList:{'9':'text_wait', '1':'text_ing', '2':'text_down', '3':'text_out_num', '4':'text_expire', '5':'text_cancle'},
				statusStyleList:{'0':'status_wait', '1':'status_ing', '2':'status_down', '3':'status_out_num', '4':'status_expire','5':'status_cancle'},
				visitorInfo:{},
				status:'',
				notice:"",
				visit_id:0,
				visitorAccountOpen:true,
				hasRecord:false,
				list:[
					{'leftTitle':'候诊状态','custom':"",show:true,edit:false},
					{'leftTitle':'所在床位','rightTitle':'',show:false,edit:false},
					{'leftTitle':'就诊人','rightTitle':'',show:true,edit:false},
					{'leftTitle':'就诊医院','rightTitle':'',show:true,edit:false},
					{'leftTitle':'就诊科室','rightTitle':'',show:true,edit:false},
					{'leftTitle':'医生','rightTitle':'',show:true,edit:false},
					{'leftTitle':'排队情况','custom':"",show:true,edit:false},
					{'leftTitle':'就诊时间','custom':"",show:true,edit:false}
				],
				showCancelBook:false,
				bookId:false,
				visitor_id:0,
				depart_id:false,
				bookInfo:false,
				showReBook:false,
				cssUrl:this.cssUrl,
				wait_type:-1,
				visitorList:[
					{name:'张三',type:1,showuinfo:false}
				],
				popkey:-1,
				poptitle:''
			}
		},
		onLoad(option) {
			if(option.id) this.bookId = option.id
			if(option.depart_id) this.depart_id = option.depart_id
			if(option.visitor_id) this.visitor_id = option.visitor_id
		},
		onShow() {
			if(!this.bookId || !this.depart_id){
				this.$func.toPage('/pages/index/index')
				return;
			}
			this.getVisitorInfo()
			this.getBookDetail()
		},
		components:{
			listBox,
			visitorAccount,
			userCard
		},
		methods: {
			async getVisitorInfo() {
				var obj = new Object()
				obj.visit_id = this.bookId
				obj.visitor_id = this.visitor_id
				obj.depart_id = this.depart_id
				obj.visit_record = 1
				this.$http.req('/api/user/get_visitor_info', obj, 'POST').then(data=>{
					if(data == -1) return
					data.detail = true
					data.istreat = true
					this.hasRecord = true
					this.visitorInfo = {...data,visit_id:this.bookId}
				});
			},
			hasrecord(res) {
				this.hasRecord = res == 1 ? true : false
			},
			submitEdit(e){
				var obj = this.$refs.usercard_0[0].getData()
				var that = this
				var param = new Object()
				param.visit_id = that.bookId
				param.bed_desc = obj.value

				if(this.popkey == 0) {
					param.wait_type = obj.type
				}else{
					param.only_set_bed_desc = 1;
				}

				that.$http.req('api/user/set_wait_type', param, 'POST').then(data=>{
					if(data == -1) return
					uni.showToast({
						title:"修改成功",
						mask:true,
						image:'/static/images/gou.svg',
						times:false,
						duration: 1500,
						success() {
							that.closepop()
							that.getBookDetail()
						}
					})
				});
			},
			closepop(){
				this.$refs.popup.close()
			},
			edit(e){
				var dataset = e.currentTarget.dataset
				var item = dataset.item
				var key = dataset.key
				if(!item.edit) return
				this.popkey = key
				this.poptitle = '候诊状态'
				if(this.popkey == 1) {
					this.poptitle = '所在床位'
				}
				this.$refs.popup.open('bottom')
				setTimeout(function(){
					this.$refs.usercard_0[0].setTypeValue(this.wait_type)
					if(this.bookInfo.bed_desc && this.bookInfo.bed_desc != '未备注床位') this.$refs.usercard_0[0].setType(this.bookInfo.bed_desc)
				}.bind(this));

			},
			cancelBook(){
				var that = this
				uni.showModal({
					title:'取消预约',
					content:'确认取消预约?',
					success(res) {
						if(!res.confirm) return
						that.$http.req('api/user/cancel_appoint', {
							appoint_id:that.bookId,
							depart_id:that.depart_id
						},
						 'POST').then(data=>{
							if(data == -1) return
							that.getBookDetail()
							uni.showToast({
								title:"取消预约成功",
								mask:true,
								image:'/static/images/gou.svg',
								times:false,
								duration: 1500,
								success() {
									that.$func.toPage('/pages/prebook_list/prebook_list')
								}
							})
						});
					}
				})
			},
			rebook(){
				this.$func.toPage('/pages/index/index')
			},
			reback(){
				this.$func.toPage('/pages/index/index')
			},
			changeType() {
			},
			// #ifdef MP-WEIXIN
			getStatusTextStyle(status) {
				var statusTextStyle = 'display:block;border-radius:'+uni.rpx2px(6)+'px;font-size:'+uni.rpx2px(26)+'px;height:'+uni.rpx2px(40)+'px;line-height:'+uni.rpx2px(38)+'px;padding:0 '+uni.rpx2px(18)+'px;box-sizing:border-box;text-align:center;width:auto;';
				switch(status) {
					case '0':
						return statusTextStyle + 'background:#F8FFF7;border:'+uni.rpx2px(1)+'px solid #5BD07A;color:#58CA7F;';
					case '1':
						return statusTextStyle + 'border:'+uni.rpx2px(1)+'px solid #5FA1CB;background:#F7FCFF;color:#5FA1CB;';
					case '2':
						return statusTextStyle + 'background:#FCFCFC;border:'+uni.rpx2px(1)+'px solid #999999;color:#999999;';
					case '3':
						return statusTextStyle + 'background:#FFFAF7;color:#FE6710;border:'+uni.rpx2px(1)+'px solid #FE6710;';
					case '4':
					return  statusTextStyle + 'background:#FFEEEE;border:'+uni.rpx2px(1)+'px solid #FD0000;color:#FD3B3B;';
					case '5':
						return  statusTextStyle + 'background:#FCFCFC;border:'+uni.rpx2px(1)+'px solid #AEB0B8;color:#B6B8BF;';
					default:
						return statusTextStyle;
				}
			},
			getBookDateStatusStyle(status) {
				var bookDateStyle = 'height:'+uni.rpx2px(45)+'px;font-size:'+uni.rpx2px(32)+'px;line-height:'+uni.rpx2px(45)+'px;';
				switch(status) {
					case '1':
						return bookDateStyle+'color:#666666;';
					case '2':
						return bookDateStyle+'color:#999999;';
					case '3':
						return bookDateStyle+'color:#FE6710;';
					case '4':
						return bookDateStyle+'color:#FD3B3B;';
					case '5':
						return bookDateStyle+'color:#B6B8BF;';
					case '9':
						return bookDateStyle+'color:#58CA7F;';
					default:
						return bookDateStyle+'#999999';
				}
			},
			// #endif
			getBookDetail(){
				this.$http.req('api/user/get_appoint_detail', {appoint_id:this.bookId,depart_id:this.depart_id}, 'POST').then(data=>{
					if(data == -1) {
						this.bookInfo = false
						return
					}

					var bookInfo = data
					this.bookInfo = bookInfo
					this.status = this.statusStyleList[bookInfo.status]
					if((bookInfo.status==0 || bookInfo.status==2) && bookInfo.is_checked != '1') {
						this.showCancelBook = true
					}else{
						this.showCancelBook = false
					}

					var status_desc = bookInfo.status_desc;
					if(bookInfo.status == 0) {
						if(bookInfo.bed_desc) {
							status_desc = "床位候诊"
							this.wait_type = 1
						}else{
							status_desc = "等待叫号"
							this.wait_type = 0
						}
					}

					// #ifdef H5
					this.list[0].custom = "<view class='bi flex'><view class='"+this.status+" status'><view class='over status_text'>"+status_desc+"</view></view>"
					this.list[1].custom = "<view class='bi flex'><text>"+bookInfo.bed_desc+"</text>"
					// #endif

					// #ifdef MP-WEIXIN
					var bistyle = 'display:flex;align-items:center;column-gap:'+uni.rpx2px(12)+'px;';
					var biimgstyle = 'width:'+uni.rpx2px(42)+'px;height:'+uni.rpx2px(42)+'px;';
					var statusStyle = 'display:flex;justify-content:flex-end;';

					var textWaitStyle = 'color:#58CA7F;';
					var textIngStyle = 'color:#5FA1CB;';
					var textDownStyle = 'color:#999999;';
					var textOutNumStyle = 'color:#FE6710;';
					var textExpireCancleStyle = 'color:#B6B8BF;';

					this.list[0].custom = "<div style='"+bistyle+"'><div class='"+this.status+" status' style='"+statusStyle+"'><div class='over status_text' style='"+this.getStatusTextStyle(bookInfo.status)+"'>"+status_desc+"</div></div>"
					this.list[1].custom = "<div style='"+bistyle+"'><span>"+bookInfo.bed_desc+"</span>"
					// #endif


					if(bookInfo.bed_desc) {
						this.list[1].show = true
					}else{
						this.list[1].show = false
					}

					if(bookInfo.status == 0) {
						this.list[0].custom += '<img style="'+biimgstyle+'" src="'+this.cssUrl+'bi.svg">'
						this.list[1].custom += '<img style="'+biimgstyle+'" src="'+this.cssUrl+'bi.svg">'
						this.list[0].edit = true
						this.list[1].edit = true
					}else{
						this.list[0].leftTitle = '状态'
						this.list[0].edit = false
						this.list[1].edit = false
					}

					// #ifdef H5
					this.list[0].custom += '</view>'
					this.list[1].custom += '</view>'
					// #endif

					// #ifdef MP-WEIXIN
					this.list[0].custom += '</div>'
					this.list[1].custom += '</div>'
					// #endif

					this.list[2].rightTitle = bookInfo.name
					this.list[3].rightTitle = bookInfo.hospital_name
					this.list[4].rightTitle = bookInfo.depart_name
					this.list[5].rightTitle = bookInfo.doctor_name ? bookInfo.doctor_name : '无'
					this.list[6].show = false

					// #ifdef H5
					this.list[7].custom = "<view class='book-date "+this.statusList[bookInfo.status]+"'>"+bookInfo.visit_date+' '+bookInfo.week_desc+' '+bookInfo.visit_time+"</view>"
					// #endif

					// #ifdef MP-WEIXIN
					this.list[7].custom = "<div class='book-date "+this.statusList[bookInfo.status]+"' style='"+this.getBookDateStatusStyle(bookInfo.status)+"'>"+bookInfo.visit_date+' '+bookInfo.week_desc+' '+bookInfo.visit_time+"</div>"
					// #endif

					this.notice = bookInfo.notice;

				});
			}
		}
	}
</script>

<style lang="scss" scoped>

.book-page{
	height: 100vh;
	width: 100%;
	background: #F6F6F6;
	overflow-y: auto;
	box-sizing: border-box;

	.ccpop{
		&.bed-edit-pop{
			::v-deep{
				.uni-data-checklist{
					display: none !important;
				}
				.bed-wrapper{
					display: block !important;
				}
				.card-cus-txt{
					display: none;
				}

			}
		}
		::v-deep{
			uni-view[name="content"]{
				border-radius: 40rpx;
			}
		}
	}
	.list-box,.bot-con{
		width: calc(100% - 34rpx);
		height: auto;
		margin: 17rpx auto 0;
		overflow: hidden;
		border-radius: 0 0 8rpx 8rpx;
	}
	.edit-pop-title{
		font-size: 36rpx;
		line-height: 50rpx;
		letter-spacing: 3.17rpx;
		color: #333333;
		margin: 42rpx 0 46rpx 0;
		text-align: center;
	}

	.submit-popup{
		justify-content: center;
		column-gap: 50rpx;

		width: 100%;
		height: 124rpx;
		background: #FFFFFF;
		backdrop-filter: blur(54.4rpx);
		box-shadow: 0rpx -2rpx 6rpx 0rpx rgba(181, 181, 181, 0.1319);
		align-items: center;

		.btn.btn1{
			border-radius: 12rpx;
			width: 300rpx;
			height: 90rpx;
		}
	}
	.submit{
		.btn.btn1.btnauto{
			width: 642rpx;
			height: 92rpx;
			background: #F1F2F3;
			border-radius: 16rpx;
			font-size: 32rpx;
			color: #616266;
		}
	}

	// #ifdef H5
	.list-box{
		.item-wrapper{
			.item-inner ::v-deep{
					.bi{
						align-items: center;
						column-gap: 12rpx;
						img{
							width: 42rpx;
							height: 42rpx;
						}
					}
				 .status{
					display: flex;
					justify-content: flex-end;
					.status_text{
						display: flex;
						border-radius: 6rpx;
						font-size: 26rpx;
						height: 40rpx;
						line-height: 38rpx;
						padding:  0 10rpx;
						box-sizing: border-box;
						min-width: 90rpx;
						display: block;
						text-align: center;
						min-width: 128rpx;
					}
					&.status_wait{
						.status_text{
							background: #F8FFF7;
							border: 2rpx solid #5BD07A;
							color: #58CA7F;
						}
					}
					&.status_out_num{
						.status_text{
							background: #FFFAF7;
							color: #FE6710;
							border: 2rpx solid #FE6710;
						}
					}
					&.status_down{
						.status_text{
							background: #FCFCFC;
							border: 2rpx solid #999999;
							color: #999999;
						}
					}
					&.status_expire,&.status_cancle{
						.status_text{
							background: #FCFCFC;
							border: 2rpx solid #AEB0B8;
							color: #B6B8BF;
						}
					}
					&.status_ing{
						.status_text{
							border: 2rpx solid #5FA1CB;
							background: #F7FCFF;
							color: #5FA1CB;
						}
					}
				}

				.book-date{
					height: 45rpx;
					font-size: 32rpx;
					line-height: 45rpx;
				}
				.text_wait{
					color: #58CA7F;
				}
				.text_ing{
					color: #5FA1CB;
				}
				.text_down{
					color: #999999;
				}
				.text_out_num{
					color: #FE6710;
				}
				.text_expire,.text_cancle{
					color: #B6B8BF;
				}
			}
		}
	}
	// #endif
	.bot-con{
		background: #FFFFFF;
		margin-top: 15rpx;
		padding: 40rpx 24rpx;
		box-sizing: border-box;
		overflow: hidden;
		min-height: 430rpx;
		.title{
			height: 44rpx;
			font-size: 32rpx;
			color: #000000;
			line-height: 44rpx;
			letter-spacing: 2rpx;
			margin-bottom: 16rpx;
		}
		.content{
			margin-top: 16rpx;
			font-size: 32rpx;
			color: #666666;
			line-height: 46rpx;
			padding-bottom: 124rpx;
		}
	}
	.pagebt{
		opacity: 0;
	}
	.fixedBot{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 124rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -2rpx 6rpx 0rpx rgba(181,181,181,0.13);
		backdrop-filter: blur(20rpx);
		display: flex;
		.submit{
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			column-gap: 50rpx;
		}
	}
}
</style>
