<template>
	<view class="content">
		<yu-toast :duration="2000" ref='toasts' verticalAlign="center" :message="toastMessage"></yu-toast>
		<view class="btn">
			<!-- <button type="default" @click="scan">扫描</button> -->
			<input ref="inputs" v-model="code" type="text" placeholder="请扫码或输入入馆码" :focus="isfocus">
		</view>

		<view class="admin">
			<button type="primary" @click="getOrder" plain="true">核销</button>
		</view>

		<view class="admin">
			<button type="primary" @click="openFast" plain="true">快速入场</button>
		</view>
		<!-- 订单信息 -->
		<wyb-popup ref="order" type="bottom" :height="popheight" width="100%" radius="15" :maskClickClose='false'>
			<view class="popup-content">
				<view class='popup-title'>预约信息</view>
				<view class='popup-form' :style="{'height':formheight+'rpx'}">
					<uni-forms :modelValue="orderForm" :label-width="80" label-align='right'>
						<uni-forms-item name="type" label="预约时段">
							<view class='line36'>{{orderForm.dates}}</view>
						</uni-forms-item>
						<uni-forms-item name="type_name" label="入场类型">
							<view class='line36'>{{orderForm.type_name}}</view>
						</uni-forms-item>
						<uni-forms-item v-if="orderForm.type==1" label="单位名称" name="unit">
							<view class='line36'>{{orderForm.unit}}</view>
						</uni-forms-item>
						<uni-forms-item label="联系人" name="name">
							<view class='line36'>{{orderForm.name}}</view>
						</uni-forms-item>
						<uni-forms-item label="手机号" name="mobile">
							<view class='line36'>{{orderForm.mobile}}</view>
						</uni-forms-item>
						<uni-forms-item required label="人数" name="total">
							<uni-easyinput type="number" v-model="orderForm.total" placeholder="请输入人数" />
						</uni-forms-item>
					</uni-forms>
				</view>
				<view class='popup-btn'>
					<button type="primary" @click="cancelOrder" plain="true">取消</button>
					<button type="primary" @click="confirmOrder" plain="true">确认</button>
				</view>
			</view>
		</wyb-popup>
		<!-- 打开快速入场 -->
		<wyb-popup ref="popup" type="bottom" :height="popheight" width="100%" radius="15" :maskClickClose='false'>
			<view class="popup-content">
				<view class='popup-title'>快速入场</view>
				<view class='popup-form' :style="{'height':formheight+'rpx'}">
					<uni-forms :modelValue="fastForm" ref='fastForm' :rules='rules' :label-width="80" label-align='right'>
						<uni-forms-item name="type" label="当前时间">
							<view style='line-height: 36px;'>{{today}} {{rule_time}}</view>
						</uni-forms-item>
						<uni-forms-item required name="type" label="入场类型">
							<uni-data-checkbox v-model="fastForm.type" :localdata="typeList" />
						</uni-forms-item>
						<uni-forms-item v-if="fastForm.type==1" label="单位名称" name="unit">
							<uni-easyinput type="text" v-model="fastForm.unit" placeholder="请输入单位名称" />
						</uni-forms-item>
						<uni-forms-item required label="联系人" name="leader">
							<uni-easyinput type="text" v-model="fastForm.leader" placeholder="请输入姓名" />
						</uni-forms-item>
						<uni-forms-item label="手机号" name="mobile">
							<uni-easyinput type="text" v-model="fastForm.mobile" placeholder="请输入手机号" />
						</uni-forms-item>
						<uni-forms-item required label="人数" name="total">
							<uni-easyinput type="number" v-model="fastForm.total" placeholder="请输入人数" />
						</uni-forms-item>
					</uni-forms>
				</view>
				<view class='popup-btn'>
					<button type="primary" @click="cancelFast" plain="true">取消</button>
					<button type="primary" @click="confirmFast" plain="true">确认</button>
				</view>

			</view>
		</wyb-popup>
	</view>
</template>

<script>
	import wybPopup from '@/components/wyb-popup/wyb-popup.vue';
	import yuToast from '@/components/yu-toast/yu-toast.vue';
	export default {
		components: {
			wybPopup,
			yuToast
		},
		data() {
			return {
				// hosts:'https://gbyuyue.szgmbwg.org.cn',
				hosts: 'https://gb-test.ali251.langye.net',
				toastMessage:'',
				code: "",
				isfocus: true,
				popheight: 0,
				formheight: 0,
				orderForm: {
				},
				today:'',
				rule_time:'',
				fastForm: {
					date: '',
					rule_id: '',
					unit: "",
					leader: "",
					card_type: "",
					idcard: '',
					total: 1,
					mobile: '',
					type: 2,
					is_disability: 0
				},
				rules:{
					leader: {
						rules: [{
							required: true,
							errorMessage: '请输入联系人',
						}]
					},
					total: {
						rules: [{
							required: true,
							errorMessage: '请输入人数',
						}]
					},
					mobile:{
						rules: [{
							pattern:/^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/,
							errorMessage: '请输入正确的手机号',
						}]
					}
				},
				typeList: [{
					value: 2,
					text: '个人'
				}, {
					value: 1,
					text: '团队'
				}]
			}
		},
		onShow() {
			let that = this
			this.popheight = uni.getSystemInfoSync().screenHeight * 2 * .7
			this.formheight = this.popheight * .6
			uni.getNetworkType({
				success: function(res) {
					console.log(res.networkType); //网络类型 wifi、2g、3g、4g、ethernet、unknown、none
					if (res.networkType === "none") {
						console.log("当前无网络");
						that.openToast('当前无网络')
					} else {

					}
				}
			});
		},
		methods: {
			openToast(message){
				this.toastMessage = message
				this.$refs.toasts.show()
			},
			// 快速入场
			openFast() {
				this.getRule()
			},
			cancelFast(e) {
				this.fastForm = {
						date: '',
						rule_id: '',
						unit: "",
						leader: "",
						card_type: "",
						idcard: '',
						total: 1,
						mobile: '',
						type: 2,
						is_disability: 0
					},
					this.$refs.popup.hide()
			},
			confirmFast() {
				var that = this
				this.$refs.fastForm.validate().then(res=>{
					var url = this.hosts + '/api/mobile/other/fast-enter'
					uni.request({
						url: url,
						method: "post",
						data: that.fastForm,
						success(res) {
							// that.$refs.popup.hide()
							if (res.statusCode == 200) {
								if (res.data.errcode) {
									that.openToast(res.data.errmsg)
								} else {
									that.openToast('提交成功')
									that.fastForm.leader = ''
									that.fastForm.mobile = ''
									that.$refs.popup.hide()
								}
							} else {
								that.openToast('请求失败')
							}
						},
						fail() {
					
						}
					})
					
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})
			},
			getRule(){
				var date = new Date()
				var year = date.getFullYear()
				var month = date.getMonth()+1<10? '0' + parseInt(date.getMonth()+1) : parseInt(date.getMonth()+1)
				var day = date.getDate()<10? '0' + date.getDate() : date.getDate()
				var hour = date.getHours()
				var today = year + '-' + month + '-' + day
				var that = this
				var url = this.hosts + '/api/mobile/visit/get-calendar'
				uni.request({
					url: url,
					method: "GET",
					data: {
						start_date: today,
						end_date: today
					},
					success(res) {
						console.log(res)
						if (res.statusCode == 200) {
							if (res.data.errcode) {
								that.openToast(res.data.errmsg)
								return
							} else {
								var rules = res.data[0].rules
								for(var r of rules){
									if(hour>=parseInt(r.start_time)&&hour<=parseInt(r.end_time)){
										that.fastForm.date = today
										that.fastForm.rule_id = r.id
										that.today = today
										that.rule_time = r.start_time + '-' + r.end_time
									}
								}
								if(that.today==''){
									that.openToast('当前时段不可预约')
									return
								}
								that.$refs.popup.show()
							}
						} else {
							that.openToast('请求失败')
						}
					},
					fail() {
				
					}
				})
			},



			// 核销
			getOrder() {
				var that = this
				if(that.code==''){
					this.openToast('核销码不能为空')
					return
				}
				var url = this.hosts + '/api/mobile/other/code-show'
				uni.request({
					url: url,
					method: "GET",
					data: {
						code: that.code
					},
					success(res) {
						if (res.statusCode == 200) {
							if (res.data.errcode) {
								that.openToast(res.data.errmsg)
							} else {
								console.log(res)
								if(Object.keys(res.data).length==0){
									that.openToast('未查询到信息')
									return
								}
								let resdata = res.data
								
								that.orderForm.dates = resdata.order.date+' ' + resdata.order.time
								that.orderForm.type = resdata.order.type
								that.orderForm.type_name = resdata.order.type==1?'团队':'个人'
								that.orderForm.mobile = resdata.order.mobile
								if(resdata.order.type==2){
									that.orderForm.name = resdata.order_detail.name?resdata.order_detail.name:""
								}else{
									that.orderForm.name = resdata.order.leader?resdata.order.leader:""
									that.orderForm.unit = resdata.order.unit?resdata.order.unit:""
									
								}
								that.orderForm.total = resdata.order.total
								that.$forceUpdate();
								that.$refs.order.show()
							}
						} else {
							that.openToast('请求失败')
						}
					},
					fail() {

					}
				})
			},
			cancelOrder(e) {
				this.orderForm = {}
				this.code = "";
				this.$refs.order.hide()
				this.isfocus = true
				
			},
			confirmOrder() {
				this.usecode()
			},
			usecode() {
				var url = this.hosts + '/api/mobile/other/code-use'
				let that = this;
				that.isfocus = false
				uni.request({
					url: url,
					method: "GET",
					data: {
						code: that.code,
						total:that.orderForm.total
					},
					success(res) {
						if (res.statusCode == 200) {
							if (res.data.errcode) {
								that.openToast(res.data.errmsg)
								that.code = "";
								that.isfocus = true
								that.$refs.order.hide()
							} else {
								that.openToast('核销成功')
								that.code = ""
								that.isfocus = true
								that.orderForm = {}
								that.$refs.order.hide()
							}
						} else {
							that.openToast(res.data.errmsg)
							that.code = "";
							that.isfocus = true
							that.$refs.order.hide()
						}


					},
					fail() {

					}
				})
			}

		}
	}
</script>

<style scoped>
	.content {}
	/deep/ .yu-toast-container{
		z-index:99999999
	}
	.btn input {
		border: 1px solid #ddd;
		height: 40px;
		padding: 15px 10px;
		margin: 10px;
		margin-top: 30px;
		font-size: 26px;
	}

	.admin {
		text-align: center;
	}

	.admin button {
		margin: 10px;
		margin-top: 20px;
		padding: 10px;
		font-size: 24px;

	}

	.popup-content {}

	.popup-title {
		text-align: center;
		font-size: 20px;
		padding: 10px;
	}

	.popup-form {
		margin: 20px;
		overflow-y: auto;
	}

	/deep/ .uni-data-checklist {
		margin-top: 5px !important;
	}

	.popup-btn {
		text-align: center;
		position: fixed;
		bottom: 10px;
		width: 100%;
	}

	.popup-btn button {
		width: 40%;
		display: inline-block;
		margin: 0 10px;
	}

	.line36 {
		line-height: 36px;
	}
</style>