<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> -->
			<!-- <uni-easyinput ref="inputs" v-model="code" type="text" placeholder="请扫码或输入入馆码" :focus="isfocus"/> -->
			
			<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>
	</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:'',
				code: "",
				isfocus: true,
			}
		},
		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.code = ''
			this.isfocus = true
			
		},
		methods: {
			openToast(message){
				this.toastMessage = message
				this.$refs.toasts.show()
			},
			openFast(){
				uni.redirectTo({
					url:'/pages/index/fastenter'
				})
			},
			// 核销
			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 {
								if(Object.keys(res.data).length==0){
									that.openToast('未查询到信息')
									return
								}
								let resdata = res.data
								if(resdata.order.type==2){
									that.usecode()
								}else{
									uni.redirectTo({
										url:'/pages/index/usecode?code='+that.code
									})
								}
							}
						} else {
							that.openToast('请求失败')
						}
					},
					fail() {
				
					}
				})
			},
			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,
						all_order:1
					},
					success(res) {
						if (res.statusCode == 200) {
							if (res.data.errcode) {
								that.openToast(res.data.errmsg)
							} else {
								that.openToast('核销成功')
								that.code = ''
								that.isfocus = true
							}
						} else {
							that.openToast(res.data.errmsg)
						}
			
			
					},
					fail() {
			
					}
				})
			}
			

		}
	}
</script>

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