<template>
	<view class="content">
		<view class='row-between'>
			<view class="">运单号</view>
			<view class="flex align-center">
				<u--input placeholder="运单号" border="none"  v-model="shippingNoteInfos[0].shippingNoteNumber" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">分单号</view>
			<view class="flex align-center">
				<u--input placeholder="分单号" border="none"  v-model="shippingNoteInfos[0].serialNumber" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">始发地区划码</view>
			<view class="flex align-center">
				<u--input placeholder="始发地区划码" border="none"  v-model="shippingNoteInfos[0].startCountrySubdivisionCode" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">目的地区划码</view>
			<view class="flex align-center">
				<u--input placeholder="目的地区划码" border="none"  v-model="shippingNoteInfos[0].endCountrySubdivisionCode" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">始发地经度</view>
			<view class="flex align-center">
				<u--input placeholder="始发地经度" border="none"  v-model="shippingNoteInfos[0].startLongitude" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">始发地纬度</view>
			<view class="flex align-center">
				<u--input placeholder="始发地纬度" border="none"  v-model="shippingNoteInfos[0].startLatitude" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">目的地经度</view>
			<view class="flex align-center">
				<u--input placeholder="目的地经度" border="none"  v-model="shippingNoteInfos[0].endLongitude" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">目的地纬度</view>
			<view class="flex align-center">
				<u--input placeholder="目的地纬度" border="none"  v-model="shippingNoteInfos[0].endLatitude" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">始发地</view>
			<view class="flex align-center">
				<u--input placeholder="始发地" border="none"  v-model="shippingNoteInfos[0].startLocationText" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">目的地</view>
			<view class="flex align-center">
				<u--input placeholder="目的地" border="none"  v-model="shippingNoteInfos[0].endLocationText" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">车牌号</view>
			<view class="flex align-center">
				<u--input placeholder="车牌号" border="none"  v-model="shippingNoteInfos[0].vehicleNumber" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">司机姓名</view>
			<view class="flex align-center">
				<u--input placeholder="司机姓名" border="none"  v-model="shippingNoteInfos[0].driverName" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">时间间隔</view>
			<view class="flex align-center">
				<u--input placeholder="时间间隔" border="none"  v-model="shippingNoteInfos[0].interval" inputAlign='right'
					clearable></u--input>
			</view>
		</view>
		<view class='row-between'>
			<view class="">备注</view>
			<view class="flex align-center">
				<u--input placeholder="备注" border="none"  v-model="remark" inputAlign='right'></u--input>
			</view>
		</view>
		
		<button type="primary" @click="auth()">授权</button>
		<button type="primary" @click="start()">开启定位</button>		
		<button type="primary" @click="send()">发送定位</button>	
		<button type="primary" @click="pause()">暂停定位</button>	
		<button type="primary" @click="restart()">重启定位</button>	
		<button type="primary" @click="stop()">结束定位</button>	
	</view>
</template>

<script>
	//插件对象
	var sdkwx = uni.requireNativePlugin('Hdgq-LocSdkWX');
	import * as config from '@/config/index.js'
	export default {
		data() {
			return {
				title: 'Hello',
				vehicleNumber:"辽HG6663",//车牌号
				driverName:"孙亚洲",//司机姓名
				remark:"测试",//备注
				appId: "", //网络货运企业APP的唯一标识
				appSecurity: "", //网络货运企业在省平台申请的接入安全码
				enterpriseSenderCode: "", //网络货运企业在省平台申请的企业发送代码
				environment: "debug", //环境:“debug”接入测试环境,“release”接入正式环境。
				shippingNoteInfos:[{
					shippingNoteNumber:"CY20221206018406",
					serialNumber:"0000",
					startCountrySubdivisionCode:"210804",
					endCountrySubdivisionCode:"210804",
					startLongitude: "122.22580",
					startLatitude:"40.63070",
					endLongitude:"122.15483",
					endLatitude: "40.26812",
					startLocationText:"营口市",
					endLocationText:"营口市",
					vehicleNumber:"辽HG6663",
					// vehicleNumber:"辽PD0990",
					driverName:"孙亚洲",
					interval:"5000" 
				}],//运单信息列表,一辆车运单数最大支持数为 10
				timer:"",
				// columns[["[00]车辆损坏","[01货主临时改变收货位置]",""]]
			}
		},
		onLoad() {
			// console.log("appId-------------------",uni.getAccountInfoSync().miniProgram.appId)
			// let phoneType = uni.getSystemInfoSync().platform //判断手机类型
			// if( phoneType == "android"){
			// 	this.appId = "uni.UNIDCD13AC"
			// }else if(phoneType == "ios"){
			// 	this.appId = "uni.UNI9C76CB6"
			// }
			//隐私合规接口,HBuildx3.3.3及其以后的版本需要调用合规检查
			var isContains = true;//隐私权政策是否包含高德开平隐私权政策  true是包含
			var isShow = true;//隐私权政策是否弹窗展示告知用户 true是展示 
			sdkwx.updatePrivacyShow(isContains, isShow);
			
			var isAgree = true;//隐私权政策是否取得用户同意  true是用户同意
			sdkwx.updatePrivacyAgree(isAgree);
			//检查定位权限
			sdkwx.checkLocationPermission(function(res){
				console.log(res);
			});
		},
		methods: {
			auth() { 
				let phoneType = uni.getSystemInfoSync().platform //判断手机类型
				this.enterpriseSenderCode = config.def().enterpriseSenderCode
				this.environment = config.def().environment
				if (phoneType == "android") {
					this.appId = config.def().androidAppId
					this.appSecurity = config.def().androidAppSecurity
				} else if (phoneType == "ios") {
					// this.appId = "uni.UNIDCD13AC"
					// this.appSecurity = "b01e4805276646eeb25f0fad91de97a4f32e8c2c0e0d42e5b24cc4737e9f040f"
					this.appId = config.def().iosAppId
					this.appSecurity = config.def().iosAppSecurity
				}
				//授权
				// var appId = "uni.UNI9C76CB6";//网络货运企业APP的唯一标识
				// var appSecurity = "24c593b7037347e08857a6ab05fd231b0509f5bb7cb741078f524f506ad32db8";//网络货运企业在省平台申请的接入安全码
				// var enterpriseSenderCode = "23106960";//网络货运企业在省平台申请的企业发送代码
				// var environment = "debug";//环境:“debug”接入测试环境,“release”接入正式环境。
				sdkwx.auth(this.appId, this.appSecurity, this.enterpriseSenderCode, this.environment, function(res) {
					console.log(res);
					if (res.type == "onSuccess") {
						//成功
						console.log(res,"成功");
					} else if (res.type == "onFailure"){
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}
					uni.showModal({
						content:JSON.stringify(res)
					});
				});
			},
			start(){
				var that = this
				//开启定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				// var startParam = uni.getStorageSync("sdkStartParam")
				// if(startParam){
				// 	this.$helper.sdkStart(startParam.carNo,startParam.driverName,startParam.remark,startParam.shippingNoteInfos)
				// }
				console.log("更换前",shippingNoteInfos)
				// shippingNoteInfos = startParam.shippingNoteInfos
				// shippingNoteInfos[0].shippingNoteNumber = 'CY20221028860222'
				console.log("更换后",shippingNoteInfos)
				sdkwx.start(shippingNoteInfos[0].vehicleNumber,shippingNoteInfos[0].driverName, remark, shippingNoteInfos, function(res){
					console.log(res);
					if (res.type == "onSuccess") {
					//成功
					console.log("开启定位成功！！！")
					console.log(res)
					var shippingNoteInfos = res.data[0]; //运单信息列表
					console.log(shippingNoteInfos, "查看返回的时间")
					if (shippingNoteInfos) {
						let dateTtime = Number(shippingNoteInfos.interval + 1000)
						console.log("开始定时", dateTtime)
						// that.timer = setTimeout(() => {
						// 	console.log("定时")
						// 	that.send()
						// }, dateTtime);
					}
					} else if (res.type == "onFailure"){
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}
					uni.showModal({
						content:JSON.stringify(res)
					});
				});			   
			},
			send(){
				var that = this
				//发送定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.send(shippingNoteInfos[0].vehicleNumber,shippingNoteInfos[0].driverName, remark, shippingNoteInfos, function(res){
					var shippingNoteInfos = res.data[0]; //运单信息列表
					if (res.type == "onSuccess") {
						console.log("App.vue发送定位成功")
						console.log(res);
						console.log("查看返回的时间",shippingNoteInfos)
						if (shippingNoteInfos) {
							let dateTime = Number(shippingNoteInfos.interval + 1000)
							console.log("定时开始", dateTime)
							that.timer = setTimeout(() => {
								//TODO 
								that.send()
							}, dateTime);
						}
					} else if (res.type == "onFailure") {//失败
						console.log("App.vue发送定位失败", res)
						var errorCode = res.data.errorCode; //错误码
						var errorMsg = res.data.errorMsg; //错误描述
						let dateTime1 = Number(shippingNoteInfos.interval + 1000)
						console.log("定时开始", dateTime1)
						that.timer = setTimeout(() => { //失败也需要定时上传定位(为了解决频繁调用接口问题)
							that.send()
						}, dateTime1);
					}
					uni.showModal({
						content:JSON.stringify(res)
					});
				});	
			},
			pause(){
				//暂停定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.pause(shippingNoteInfos[0].vehicleNumber,shippingNoteInfos[0].driverName, remark, shippingNoteInfos, function(res) {
					console.log(res);
					if (res.type == "onSuccess") {
						//成功
					} else if (res.type == "onFailure"){
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}
					uni.showModal({
						content:JSON.stringify(res)
					});
				});			 			
			},
			restart(){
				//重启定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.restart(shippingNoteInfos[0].vehicleNumber,shippingNoteInfos[0].driverName, remark, shippingNoteInfos, function(res) {
					console.log(res);
					
					if (res.type == "onSuccess") {
						//成功
						var shippingNoteInfos = res.data;//运单信息列表
					} else if (res.type == "onFailure"){
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}
						uni.showModal({
							content:JSON.stringify(res)
						});
				});			
			},
			stop(){
				var that = this
				//结束定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.stop(shippingNoteInfos[0].vehicleNumber,shippingNoteInfos[0].driverName, remark, shippingNoteInfos, function(res) {
					console.log(res);
					if (res.type == "onSuccess") {
						clearInterval(that.timer)
						console.log("结束成功！！！",res)
						//成功
					} else if (res.type == "onFailure"){
						console.log("结束失败！！！",res)
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}	
					uni.showModal({
						content:JSON.stringify(res)
					});
				});			
			},
		}
		
	}
</script>

<style>
	.content {
		background: white;
		margin: 20rpx;
		border-radius: 20rpx;
		padding: 20rpx;
		
		.place {
			width: 80%;
			text-align: right;
		}
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	.row-between {
		margin: 20rpx 0;
	}
</style>
