<template>
	<view class="sub" :style="'padding-top:'+padtop+'px'">
		<navbar :back="false" title="扫码核销" :bgcolor="backgroundColor" color="#fff" fixed zIndex="1010" :center="true"	:custom='true' @getTop="getTop" back="true"/>
		
<!-- 		<div class="jhyyes">
			<img src="/static/images/sm.png" alt="" class="img">
		</div>
		<view class="naver" @click="smit">
			扫码核销
		</view> -->
	</view>
</template>

<script>
	import url from "@/util/url";
	export default {
		data() {
			return {
				padtop:this.$http._GET.customBarH||0,
				backgroundColor: 'linear-gradient(90deg, #A00517, #E93030)',
			};
		},
		onLoad() {
			this.scanAction()
		},
		onShow() {
			
			// this.uehg = 1
			// this.scanAction()
			// console.log(34)
			
		},
		onHide() {
			
			// this.uehg = 2
			// console.log(344)
			
		},
		methods:{
			getTop(e){
				this.padtop=e
			},
			smit(){
				this.scanAction()
			},
			
			   //扫码功能
			scanAction() {
				var that=this;
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode', 'barCode'],
					success: function (red) {
						// 最好?id=1&name=测试
						// console.log(res,9)
						console.log(red,999)
						
						return
						let result=red.result;
						if (typeof result == 'string') {
							    try {
							        var obj=JSON.parse(decodeURIComponent(result));
							        if(typeof obj == 'object' && obj ){
										console.log(7)
							        }else{
										console.log(9)
										uni.showToast({
											title:'扫码查询失败,请检查二维码是否正确',
											icon:"none"
										})
										uni.navigateBack({
											delta:1
										})
							            return ;
							        }
							
							    } catch(e) {
							       uni.navigateBack({
							       	delta:1
							       })
								   uni.showToast({
								   	title:'扫码查询失败,请检查二维码是否正确',
								   	icon:"none"
								   })
							       return
							    }
							}
						var res=JSON.parse(decodeURIComponent(red.result))
						// console.log(res)
						// return
						if(!res.menuId|| !res.phone||!res.idCard){
							uni.navigateBack({
								delta:1
							})
							uni.showToast({
								title:'扫码查询失败,请检查二维码是否正确',
								icon:"none"
							})
							return
						}
						// var now=new Date().getTime()
						// var oldnow=res.time;
						// if(oldnow > now) {
						//     console.log("二维码在有效期内");
						// } else {
						// 	uni.showToast({
						// 		title:'二维码已过期，请重新生成',
						// 		icon:"none"
						// 	})
						// 	return
						// }
						var businessId=uni.getStorageSync('businessId')
						var params={
							"businessId": businessId,
							"policyId": res.menuId,
							"creditPhone": res.phone,
							"idCard": res.idCard,
						}
						that.$http.post('system/record',params).then(red=>{
							if(red.code == 200 ) {
								uni.redirectTo({
									url:"/pages/index/success"
								})
							}else{
								uni.navigateBack({
									delta:1
								})
								uni.showToast({
									title: red.msg,
									duration: 1000,
									icon: 'none'
								});
								
							} 
						})
						// console.log(res)
						// console.log('条码类型：' + res.scanType);
						// console.log('条码内容：' + res.result);
					},
					fail:function(e){
						console.log(e)
						if (e && e.errMsg && e.errMsg.indexOf('scanCode:fail cancel') != -1) {
						    return;
						}
						uni.showToast({
							title:'扫码失败',
							icon:"none"
						})
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.sub{box-sizing: border-box;width: 100%;min-height: 100vh;}
	.jhyyes{
		display: flex;
		justify-content: center;
		margin-top: 200upx;
	}
	.img{
		width: 258upx;
		height: 251upx;
		// margin:  auto;
		// margin-left: 50%;
		// transform: translateX(-50%);
	}
	
	
	.naver{
		width: 416rpx;height:84rpx;width: 208px;
		height: 42px;
		background: linear-gradient(90deg, #A00517, #E93030);
		border-radius: 21px;display: flex;align-items: center;justify-content: center;font-size: 30rpx;color: #ffffff;margin:150rpx auto 0;
		}

</style>
