<!--
 * @Author: euzhi
 * @Description: 扫码绑定
-->
<template>
	<view class="appCotent">
		<div class="scan">
			<div class="contentMoveCar">
				<div class="car_item__one">
					<div class="item__content">
						<view class="">
							您的专属二维码编号
						</view>
						{{moveCode}}
					</div>
				</div>
				<div class="car_item__three">
					<view class="">
						感谢您使用挪车二维码。
					</view>
					<view style="">
						请完成<text>车辆信息</text>与<text>联系方式</text>的填写以便享受挪车服务。
					</view>
				</div>
			</div>
			<div class="car_info">
				<!-- <view class="select-car" @click="getCarList">
				请选择爱车  &gt;
			</view> -->
				<view class="info_list" style="padding:20rpx 0;">
					<view class="left">
						品牌车型：
					</view>
					<view class="right info" style="width:500rpx;" v-if="carInfo" @click="getCarList">
						<image :src="carInfo.logoUrl" mode=""></image>
						<view>
							<view>
								{{carInfo.brand + "-" + carInfo.seriesName}}
							</view>
							{{carInfo.carModel}}
						</view>
						&gt;
					</view>
					<view class="right" style="width:500rpx;color: #E1251B;" v-else @click="getCarList">
						请选择 &gt;
					</view>
				</view>
				<!-- <view class="info_list">
				<view class="left">
					车型
				</view>
				<view class="right">
					{{carInfo.carModel}}
				</view>
			</view> -->
				<view class="info_list" style="padding:10rpx 0;">
					<view class="left">
						车牌号：
					</view>
					<view class="right" style="width:500rpx;">
						<!-- {{carInfo.carNo || ""}} -->
						<car-number class="car__input" v-model="model1.carNo"></car-number>
					</view>
				</view>
				<!-- <view class="info_list">
				<view class="left">
					行驶里程：
				</view>
				<view class="right">
					{{carInfo.mileage || 0}}KM
				</view>
			</view> -->
				<!-- <div class="scan__tips">
                <div>请填写以下信息</div>
                <div @click="goLoveCar">从爱车选择<span>
                        <image src="@/static/move-car/scan_right.png" /></span></div>
            </div>
            <div class="car__num__self">
                <div class="car__num">车牌号：</div>
                <car-number class="car__input" v-model="model1.carInfo.carNo"></car-number>
            </div>
            <active-form :formDate.sync="formData"></active-form>
			
			<div class="add__user">
			    <u-checkbox-group placement="row">
			        <u-checkbox :checked='checked' @change="changeBtn"></u-checkbox>
			    </u-checkbox-group>
			    请先阅读并同意<span @click="goArticle(2)">《京东汽车挪车牌隐私权政策》</span>
			</div> -->
			</div>
			<!-- <view class="no-car-info">
			<view class="title">
				请完成以下操作
			</view>
			<image src="../../../static/move-car/none.png"></image>
			<view class="no-content">
				暂无爱车信息，请先添加爱车
			</view>
			<view class="no-car-btn" @click="getCarList">
				从爱车选择
			</view>
		</view> -->

			<view class="form-data">
				<view class="form-item">
					<view class="item-text">
						手机号：
					</view>
					<view class="item-input">
						<input type="number" placeholder="请输入手机号" v-model="model1.userMobile">
					</view>
				</view>
				<view class="form-item">
					<view class="item-text">
						验证码：
					</view>
					<view class="item-input flex align-center">
						<input type="number" placeholder="请输入" v-model="model1.verificationCode">
						<view class="code" style="color: #F1251B;" v-if="!smsbtn.status" @click="sendCode">
							{{smsbtn.text}}
						</view>
						<view class="code" v-else>
							{{smsbtn.text}}({{smsbtn.codeTime+'s'}})
						</view>
					</view>
				</view>
			</view>

			<div class="view__btn view__no__bind" @click="confirmBinding">
				绑定车辆
			</div>
			<!-- <div class="use__text" @click="gotoExplanation">使用说明</div> -->
			<div class="use__text" @click="goArticle(2)">挪车牌使用安全说明</div>

			<!-- 爱车选择弹框 -->
			<u-popup :show="selectCar" mode="bottom" round="10" @close="selectCar = false">
				<view class="love_car_list">
					<view class="love_title">
						选择车辆
						<text @click.stop="selectCar = false">x</text>
					</view>
					<view class="love_list">
						<view v-if="carList.length != 0">
							<view class="love_item" v-for="(item,i) in carList" :key="i" @click="radioChange(item)">
								<image :src="item.logoUrl"></image>
								<view class="love_info">
									<view>
										{{item.brand + "-" + item.seriesName}}
									</view>
									<view>
										{{item.carModel}}
									</view>
								</view>
								<view class="radio">
									<image src="../../../static/move-car/radio-active.png"
										v-if="item.id === radioValue"></image>
									<image src="../../../static/move-car/radio.png" v-else></image>
								</view>
							</view>
						</view>
						<view style="padding-top:140rpx;text-align: center;" v-else>
							暂时没有车辆，请添加！
						</view>
					</view>
					<view class="love_btn">
						<button type="primary" @click="addCar">添加车辆</button>
					</view>
				</view>
			</u-popup>
		</div>
	</view>
</template>

<script>
	import CarNumber from "@/common/codecook-carnumber/codecook-carnumber.vue";
	import CryptoJS from "crypto-js";
	export default {
		components: {
			CarNumber,
		},
		data() {
			return {
				model1: {
					moveCode: "",
					carNo: "",
					userMobile: "",
					verificationCode: "",
					carId: ""
				},
				carInfo: null,
				formData: [
					// {
					//     id: "kjjns", //id必须唯一 可以是数字
					//     placeholder: "请输入",
					//     label: "车牌号", // 提示输入名
					//     type: "text", //类型
					//     rules: {
					//         name: "carNo", //字段名 即提交给后端的字段
					//         value: "",
					//         verify: false, //是否开启校验
					//         errMess: "车牌号未填写", //校验不通过的错误提示
					//     },
					// },
					// {
					//     id: "kjjns", //id必须唯一 可以是数字
					//     placeholder: "请输入",
					//     label: "品牌：", // 提示输入名
					//     type: "text", //类型
					//     rules: {
					//         name: "brand", //字段名 即提交给后端的字段
					//         value: "",
					//         verify: false, //是否开启校验
					//         errMess: "品牌未填写", //校验不通过的错误提示
					//     },
					// },
					// {
					//     id: "kjjns", //id必须唯一 可以是数字
					//     placeholder: "请输入",
					//     label: "车型：", // 提示输入名
					//     type: "text", //类型
					//     rules: {
					//         name: "carModel", //字段名 即提交给后端的字段
					//         value: "",
					//         verify: false, //是否开启校验
					//         errMess: "车型未填写", //校验不通过的错误提示
					//     },
					// },
					{
						id: "kjjns", //id必须唯一 可以是数字
						placeholder: "请输入",
						label: "手机号：", // 提示输入名
						type: "text", //类型
						rules: {
							name: "phone", //字段名 即提交给后端的字段userMobile
							value: "",
							verify: false, //是否开启校验
							errMess: "手机号未填写", //校验不通过的错误提示
						},
					},
					{
						id: "uisdfjks",
						placeholder: "请输入验证码",
						label: "验证码：",
						type: "text",
						//   oneKeyPhone:true,
						rules: {
							name: "yzm",
							value: "", //字段值
							verify: false,
							errMess: "验证码未填写",
						},
					},
				],
				//挪车码
				moveCode: '',
				//绑定挪车码信息
				moveCodeInfo: null,
				//选择车辆id
				carId: '',
				carList: [],
				checked: false,
				selectCar: false,
				radioValue: "",
				smsbtn: {
					text: '发送验证码',
					status: false,
					codeTime: 60
				}, // 验证码
				clickStatus: false, // 节流
				key:null, // 加密key
				iv:null  // 加密iv
			}
		},
		methods: {
			changeBtn() {
				this.checked = !this.checked;
			},
			// 跳转到使用说明页面
			gotoExplanation() {
				uni.navigateTo({
					url: '/pagesD/pages/personal/instructions'
				});
			},
			goLoveCar() {
				uni.navigateTo({
					url: '/pagesB/pages/move-car/loveCar?price=1'
				});
			},
			// 选择爱车
			radioChange(item) {
				this.carInfo = item
				this.radioValue = item.id
				this.selectCar = false
				this.model1.carNo = item.carNo
				this.model1.carId = item.id
			},
			// 获取爱车列表
			getCarList() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				let opts = {
					url: '/app/vehicleAdmin/list',
					method: 'get'
				};
				this.http.httpTokenRequest(opts, {}).then(res => {
					if (res.statusCode == 200) {
						uni.hideLoading()
						this.carList = res.data || []
						this.selectCar = true
					}
				})
			},
			// 添加车辆
			addCar() {
				this.selectCar = false
				uni.navigateTo({
					url: '/pages/transitionPage'
				});
			},
			// 发送验证码
			sendCode() {
				if (this.model1.userMobile.length == 0) {
					uni.showToast({
						title: "联系方式不能为空!",
						icon: "none",
					});
					return;
				}
				let regMobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
				if (!regMobile.test(this.model1.userMobile)) {
					uni.showToast({
						icon: 'none',
						title: '请先填写正确的手机号'
					});
					return;
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				let opts = {
					url: '/app/smsCode/binding/',
					method: 'post'
				};
				this.http.httpTokenRequest(opts, {
					phone: this.model1.userMobile
				}).then(res => {
					if (res.statusCode == 200) {
						uni.showToast({
							title: "别着急！短信已经发送了~",
							icon: 'none',
							duration: 1500
						});
						uni.hideLoading()
						if (this.clickStatus == false) {
							this.clickStatus = true
							let timer = setInterval(() => {
								if (this.smsbtn.codeTime > 0) {
									this.smsbtn.status = true
									this.smsbtn.text = "已发送";
									this.smsbtn.codeTime--;
								} else if (this.smsbtn.codeTime <= 0) {
									this.smsbtn.codeTime = 60;
									this.smsbtn.text = "重新获取";
									this.smsbtn.status = false
									this.clickStatus = false
									clearInterval(timer);
								}
							}, 1000);
						}
					}
				})
			},
			confirmBinding() {
				if (!this.carInfo) {
					uni.showToast({
						title: "请选择绑定的爱车!",
						icon: "none",
					});
					return;
				}
				if (this.model1.userMobile.length == 0) {
					uni.showToast({
						title: "联系方式不能为空!",
						icon: "none",
					});
					return;
				}
				if (this.model1.verificationCode.length == 0) {
					uni.showToast({
						title: "验证码不能为空!",
						icon: "none",
					});
					return;
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				let opts = {
					url: '/app/moveCode/bind',
					method: 'put'
				};
				this.http.httpTokenRequest(opts, this.model1).then(res => {
					if (res.data.code == 200) {
						//展示绑定成功
						uni.showToast({
							title: "绑定成功",
							icon: "success",
						});
						uni.hideLoading()
						setTimeout(()=>{
							uni.navigateTo({
								url: '/pagesB/pages/move-car/myMoveCar'
							})
						},500)
					} else if(res.data.code == 1000000002){
						uni.showToast({
							title: res.data.msg,
							icon: "none",
						});
						this.setAES(this.moveCode,this.key)
					}  else {
						//绑定失败，展示失败信息
						uni.hideLoading()
						uni.showToast({
							title: res.data.msg,
							icon: "none",
						});
					}
				})
			},
			// 跳转协议政策页面
			goArticle(index) {
				let opts = {
					url: '',
					method: 'get'
				};
				if (index == 1) {
					opts.url = "/app/user/registerAgreement"
				} else {
					opts.url = "/app/user/ofPrivacy"
				}
				this.http.httpTokenRequest(opts, {}).then(res => {
					if (res.data.code == 200) {
						let data = res.data.msg
						uni.setStorageSync("articelContent", data)
						uni.navigateTo({
							url: '/pages/binding/article'
						});
					}else{
						uni.showToast({
							title: res.data.msg,
							icon: "none",
						});
					}
				})
			},
			// 获取加密key
			getKey(){
				let opts = {
					url: '/app/user/c/i',
					method: 'get'
				};
				this.http.httpTokenRequest(opts, {}).then(res => {
					if (res.data.code == 200) {
						let data = res.data.data
						try{
							let str = data.s3//.padStart(8,0)
							let ivs = data.s3
							let str2 = data.l1
							if(str2.length == 4){
								let str3 = str2.substring(0,2)
								let str4 = str2.substring(2,4)
								this.key = str.substring(str3,str4)
								let str5;
								// if(ivs.length > 8){
								// 	str5 = ivs.substring(ivs.length-8,ivs.length)
								// 	this.iv = String(str2 + str5).padStart(16,0)
								// }else{
								// 	str5 = ivs.padStart(8,0)
								// 	this.iv = String(str2 + str5).padStart(16,0)
								// }
								if(ivs.length > 8){
									str5 = ivs.substring(ivs.length-8,ivs.length)
									this.iv = String(str2 + str5)//.padStart(16,0)
									while(this.iv.length<16){
										let num = ''
										num+='0'
										this.iv = num + this.iv;
									}
								}else{
									// str5 = ivs//.padStart(8,0)
									while(ivs.length<8){
										let num = ''
										num += '0'
										ivs = num + ivs;
									}
									if(ivs.length == 8){									
										this.iv = String(str2 + ivs)//.padStart(16,0)
										while(this.iv.length<16){
											let num2 = ''
											num2 += '0'
											this.iv = num2 + this.iv;
										}
									}
								}
								this.setAES(this.moveCode,this.key) 
							}
						}catch(err){
							
						}
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: "none",
						});
					}
				})
			},
			setAES(str,keys) {
				// const data = CryptoJS.enc.Utf8.parse(str);
				const key = CryptoJS.enc.Utf8.parse(keys);
				const iv = CryptoJS.enc.Utf8.parse(this.iv);
				var option = {
					iv: iv,
					mode: CryptoJS.mode['CBC'],//看需求变“CBC”
					padding: CryptoJS.pad['Pkcs7']//看需求变“ZeroPadding”
				};
				var encrypted = CryptoJS.AES.encrypt(str, key, option);
				const words = encrypted.ciphertext.toString();
				
				// var hex = CryptoJS.enc.Utf8.stringify(words);
				// return hex
				this.model1.moveCode = words 
			},
		},
		onLoad(option) {
			this.moveCode = option.moveCode
			this.getKey()
		},
		onShow: function() {
			//console.log( this.$store.state.selectedCar);
		}
	}
</script>

<style lang="scss" scoped>
	.scan {
		/deep/ .form-container .line .input {
			text-align: right !important;
			padding-right: 30rpx;
		}

		.use__text {
			text-align: center;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			text-decoration: underline;
			color: #3A86F7;
		}

		.add__user {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
			padding-left: 40rpx;
			margin-bottom: 20rpx;

			span {
				color: rgba(58, 134, 247, 1);
				padding: 0 5rpx;
			}
		}

		.view__btn {
			width: 670rpx;
			height: 76rpx;
			text-align: center;
			line-height: 76rpx;
			margin: 50rpx auto 0;
			font-size: 32rpx;
		}

		.view__no__bind {
			background: url('@/static/move-car/move-car-view2.png') no-repeat;
			margin-bottom: 30rpx;
			background-size: contain;
			color: rgba(255, 255, 255, 1);
		}

		/deep/ .p-l14 {
			padding-left: 0 !important;
		}

		.u-checkbox-group {
			display: inline-block !important;
		}

		/deep/.u-checkbox-group--row {
			display: inline-block !important;
		}

		.contentMoveCar {
			background-color: #fff;
			padding: 40rpx 40rpx 10rpx;

			.item__label {
				font-size: 34rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #333333;
				line-height: 42rpx;
			}

			.item__label__one {
				padding-right: 34rpx;
			}

			.item__content {
				width: 500rpx;
				padding: 15rpx 0;
				font-size: 40rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #F1251B;
				text-align: center;
				background: url(@/static/move-car/move-car-code5.png) no-repeat;
				background-size: 100% 100%;
				letter-spacing: 6rpx;

				view {
					font-size: 30rpx;
					font-weight: 500;
					margin-bottom: 6rpx;
					letter-spacing: 0;
				}
			}

			.car_item__one {
				display: flex;
				align-items: center;
				margin-bottom: 30rpx;
				justify-content: center;
			}

			.car_item__three {
				margin-top: 40rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #F88D28;
				line-height: 36rpx;
				// text-indent: 2em;
				text-align: center;
				box-sizing: border-box;
				padding: 16rpx 30rpx 26rpx;
				background: #FEF4E9;
				border-radius: 10rpx;

				text {
					font-size: 28rpx;
					font-weight: bold;
				}
			}
		}

		.moveCarMsg {
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 28rpx 1rpx rgba(232, 232, 232, 0.2);
			border-radius: 20rpx;
			padding: 32rpx 40rpx;

			.car__num__self {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.car__num {
					width: 160rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}

				.car__input {
					width: 100%;
				}
			}

			.scan__tips {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 30rpx;

				>div:nth-of-type(1) {
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}

				>div:nth-of-type(2) {
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #F88D28;

					span {
						image {
							width: 11rpx;
							height: 18rpx;
						}

						margin-left: 10rpx;
					}
				}
			}
		}

		// 已绑定车辆信息
		.car_info {
			background: #FFFFFF;
			background-color: #fff;
			padding: 0 40rpx;
			position: relative;
			top: -6rpx;

			.select-car {
				line-height: 80rpx;
				text-align: right;
				font-size: 28rpx;
				color: #E1251B;
			}

			.info_list {
				line-height: 90rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				// border-bottom:2rpx solid rgba(0,0,0,.1);

				.left {
					font-size: 32rpx;
				}

				.right {
					font-size: 28rpx;
					color: #666;
					line-height: 1.8;
					word-wrap: break-word;
					text-align: right;

					&.info {
						display: flex;
						align-items: center;
						justify-content: flex-end;
						text-align: left;

						// padding:20rpx 0;
						image {
							max-width: 48rpx;
							height: 48rpx;
						}

						>view {
							margin: 0 10rpx;
							line-height: 1.5;

							>view {
								font-weight: bold;
								font-size: 32rpx;
								color: #333;
							}
						}
					}

					.car__input {
						width: 100%;
					}
				}
			}

			.car_btn {
				width: 400rpx;
				line-height: 76rpx;
				background: #FFFFFF;
				border: 2rpx solid #DDDDDD;
				border-radius: 10rpx;
				text-align: center;
				margin: 30rpx auto 0;
				font-size: 32rpx;
				color: #666;
			}
		}

		.no-car-info {
			width: 100%;
			background-color: #fff;
			text-align: center;
			padding: 30rpx 0;

			.title {
				font-size: 28rpx;
				color: #666;
			}

			image {
				width: 130rpx;
				height: 130rpx;
				margin: 40rpx 0 20rpx;
			}

			.content {
				font-size: 28rpx;
			}

			.no-car-btn {
				width: 480rpx;
				font-size: 32rpx;
				color: #E1251B;
				text-align: center;
				line-height: 80rpx;
				border-radius: 50rpx;
				border: 2rpx solid #F4B1AD;
				margin: 30rpx auto 0;
			}
		}

		.form-data {
			position: relative;
			top: -6rpx;
			background-color: #fff;
			margin-bottom: 40rpx;
			box-sizing: border-box;
			padding: 0 40rpx;

			.form-item {
				height: 100rpx;
				line-height: 100rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				box-sizing: border-box;

				&:first-child {
					border-bottom: 2rpx solid rgba(0, 0, 0, .1);
				}

				.item-text {
					font-size: 32rpx;
				}

				.item-input {
					font-size: 28rpx;
					text-align: right;
					color: #333;
					display: flex;
					justify-content: space-around;
					align-items: center;

					.code {
						height: 50rpx;
						line-height: 50rpx;
						font-size: 26rpx;
						padding-left: 20rpx;
						border-left: 2rpx solid rgba(0, 0, 0, .1);
						margin-left: 20rpx;
						color: #BBBBBB;
					}
				}

				.car-num {
					width: 500rpx;
				}
			}
		}

		// 爱车列表
		.love_car_list {
			padding: 30rpx 40rpx;

			.love_title {
				text-align: center;
				font-size: 36rpx;
				font-weight: bold;
				margin-bottom: 30rpx;
				position: relative;

				text {
					position: absolute;
					right: 20rpx;
					top: -4rpx;
					font-size: 40rpx;
					font-weight: 500;
					color: #666;
				}
			}

			.love_list {
				height: 400rpx;
				overflow: auto;

				.love_item {
					padding: 20rpx 0;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-top: 2rpx solid #E8E8E8;

					&:first-child {
						border: none;
					}

					image {
						width: 100rpx;
						height: 100rpx;
					}

					.love_info {
						width: 450rpx;

						>view {
							&:first-child {
								font-size: 34rpx;
								font-weight: bold;
								margin-bottom: 10rpx;
							}

							&:last-child {
								font-size: 28rpx;
								color: #666;
							}
						}
					}

					.radio {
						width: 60rpx;

						>image {
							width: 48rpx;
							height: 48rpx;
						}
					}
				}
			}

			.love_btn {
				padding: 50rpx;

				button {
					height: 80rpx;
					line-height: 80rpx;
					border-radius: 50rpx;
					background: linear-gradient(150deg, #E1251B, #E14A1B);
					box-shadow: 0 6rpx 13rpx 2rpx rgba(225, 50, 27, 0.17);
					border: 0;
					font-size: 32rpx !important;
				}
			}
		}
	}
</style>
