<template>
	<view class="container-view">
		<!-- <input class="input" type="text" :placeholder="$t('请输入车牌号全称')" placeholder-style="color:#BAC1D0;"> -->
		<input @input="onInput" @blur="inputBlur" @confirm="handleConfirm" :value="hideValue" class="input" type="text"
			:focus="hideFocus" :placeholder="$t('请输入车牌号')" placeholder-style="color:#BAC1D0;">
		<view :class="['confirm-btn', !hideValue && 'btn-disable']" @tap="handleConfirmHandle">
			{{ hideValue ? $t('确认') : $t('请先输入车牌号') }}
		</view>
		<!-- <view class="input-text">{{ $t('温馨提示：各省市因地制宜，制定相关的牌照格式，请按照实际车辆牌照的格式进行填写～') }}</view>
		<view class="title">{{ $t('参考格式') }}</view>
		<view class="content-view">{{ $t('北京') }}<text>123456</text></view> -->
	</view>
</template>

<script module="tools" lang="wxs" src="@/pages/common/wxs/tools.wxs"></script>
<script module="tools" lang="sjs" src="@/pages/common/wxs/tools.sjs"></script>
<script>
	var config = require('../../common/config.js');
	var common = require('../../common/common.js');
	var http = require('../../common/http.js');
	var storage = require('../../common/storage.js');
	export default {
		components: {

		},
		data() {
			return {
				myLocation: {},
				timer: null,
				hideValue: '',
				order_sn: '',
				order_model_id: '',
				isReturnIndex: true, //是否返回首页
				hideFocus: false,
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			this.order_sn = options.order_sn ? options.order_sn : ''
			this.order_model_id = options.order_model_id ? options.order_model_id : ''
			this.myLocation.longitude = options.longitude ? options.longitude : ''
			this.myLocation.latitude = options.latitude ? options.latitude : ''
			//#ifdef MP-ALIPAY
			setTimeout(function() {
				this.hideFocus = true
			}, 500);
			//#endif
			//#ifdef MP-WEIXIN
			this.hideFocus = true
			//#endif
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			if ((this.myLocation.longitude == '') && (this.myLocation.latitude == '')) {
				this.getLocationAndSave()
			}
		},
		methods: {
			getLocationAndSave: function() {
				const me = this;
				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						uni.hideLoading();
						var myLocation = {
							longitude: res.longitude,
							latitude: res.latitude
						}
						me.myLocation = myLocation
						uni.setStorageSync('user_current_location', myLocation);
					},
					fail: function(res) {
						uni.hideLoading();
					},
					complete: function(res) {
						uni.hideLoading();
					}
				});
			},

			onInput(e) {
				this.hideValue = e.detail.value
				this.hideFocus = true
			},

			inputBlur() {
				this.hideFocus = false
			},
			handleConfirmHandle() {
				if (!this.hideValue) {
					common.simpleToast(this.$t('请先输入车牌号'))
					return
				}
				this.handleConfirm()
			},
			handleConfirm(e) {
				e && (this.hideValue = e.detail.value)
				const me = this
				if ((this.myLocation.longitude == '') && (this.myLocation.latitude == '')) { // 未获取到经纬度
					wx.getSetting({
						success(res) {
							if (res.authSetting["scope.userLocation"] == false) { // 判断原因是否为微信小程序内部没有授权定位
								common.simpleToast(this.$t('无法获取位置信息，请检查您的微信定位权限设置'))
							} else {
								common.simpleToast(this.$t('您的手机未授权微信定位权限，请前往手机设置开启权限'))
							}
							me.hideFocus = true
						}
					})
					return
				}
				if (this.hideValue == '') {
					common.simpleToast(this.$t('请输入车牌号'))
					this.hideFocus = true
				} else {
					this.hideFocus = false
					this.submitPlate()
				}
			},

			submitPlate() {
				const pData = {
					longitude: this.myLocation.longitude,
					latitude: this.myLocation.latitude,
					plate_number: this.hideValue
				}
				console.log(this.hideValue)
				const me = this
				http.postApi(config.API_DAYHIRE_CAR_CAR_INFO, pData, (resp) => {
					uni.hideLoading()
					if (resp.data.code === 200) {
						const timestamp = Date.now(); // 获取当前时间戳（毫秒）
						const isOffline = (Math.floor(timestamp / 1000) - resp.data.data.last_comm_time) > 1800
						if (resp.data.data.last_comm_time === 0 || isOffline) {
							common.simpleToast(this.$t('此车辆已离线，请选择其他车辆'))
						} else {
							let carInfo = JSON.stringify(resp.data.data)
							const car_model = resp.data.data.model_info.car_model
							const model_images = resp.data.data.model_info.model_images.split(',')
							const return_imgs = resp.data.data.return_imgs
							var model_id = resp.data.data.model_info.model_id
							if (resp.data.data.has_owner) { //车辆正在被使用
								if (resp.data.data.is_mine) { //是本人在使用
									wx.showModal({
										title: this.$t('提示'),
										content: this.$t('已有正在使用的车辆，是否跳转至车辆详情页')+'?',
										cancelText: this.$t('取消'),
										confirmText: this.$t('确定'),
										success: function(res) {
											this.isReturnHome = false
											uni.navigateTo({
												url: '/pages/battery/battery?plate_number=' +
													me
													.hideValue
											});
										},
										fail: function(res) {},
										complete: function(res) {},
									})
								} else {
									common.simpleToast(this.$t('此车辆正在被使用'))
								}
							} else {
								if (me.order_sn != '') {
									if (this.order_model_id != model_id) { // 预租车型与之前预约车型不一致
										common.simpleToast(this.$t('与预约车型不符'))
									} else {
										this.isReturnHome = false
										uni.navigateTo({
											url: '/pages/activation/activation?plate_number=' + me
												.hideValue + '&order_sn=' + this.order_sn +
												'&car_model=' + car_model + '&model_image=' +
												model_images[0] + '&return_imgs=' + JSON.stringify(
													return_imgs),
											fail() {}
										})
									}

								} else {
									this.isReturnHome = false
									uni.navigateTo({
										url: '/pages/carIntroduce/carIntroduce?carInfo=' +
											encodeURIComponent(carInfo) + '&plate_number=' + me
											.hideValue,
										fail() {}
									})
								}
							}
							setTimeout(() => {
								me.setData({
									plateInputs: Array.from({
										length: 6
									}, () => ({
										value: ''
									})),
									hideValue: '',
									preFullPlateNumber: ''
								})
							}, 500)
						}
					} else {
						common.simpleToast(resp.data.msg)
						me.setData({
							hideValue: '',
						})
					}
				})
			}
		},
		onUnload: function() {
			if (this.timer) {
				clearTimeout(this.timer);
				this.timer = null
			}
		}
	};
</script>

<style>
	@import './manualInput.css';
</style>