<template>
	<view class="content">
		<yu-toast :duration="2000" ref='toasts' verticalAlign="center" :message="toastMessage"></yu-toast>
		<!-- 打开快速入场 -->
			<view class="popup-content">
				<view class='popup-title'>快速入场</view>
				<view class='popup-form'>
					<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>
	</view>
</template>

<script>
	import yuToast from '@/components/yu-toast/yu-toast.vue';
	export default {
		components: {
			yuToast
		},
		data() {
			return {
				hosts:'https://gbyuyue.szgmbwg.org.cn',
				// hosts: 'https://gb-test.ali251.langye.net',
				toastMessage:'',
				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

			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 {

					}
				}
			});
			this.getRule()
		},
		methods: {
			openToast(message){
				this.toastMessage = message
				this.$refs.toasts.show()
			},
			// 快速入场
			cancelFast(e) {
				uni.redirectTo({
					url:'/pages/index/index'
				})
			},
			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 = ''
									setTimeout(function(){
										uni.redirectTo({
											url:'/pages/index/index'
										})
									},1500)
									
								}
							} 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('当前时段不可预约')
									setTimeout(function(){
										uni.redirectTo({
											url:'/pages/index/index'
										})
									},1500)
									return
								}
							}
						} else {
							that.openToast('请求失败')
						}
					},
					fail() {
				
					}
				})
			},

		}
	}
</script>

<style scoped>
	.content {}
	/deep/ .yu-toast-container{
		z-index:99999999
	}
	/deep/ .uni-forms-item{
		margin-bottom:40rpx!important
	}
	.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: scroll;
		margin-top: 0;
	}

	/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>