// mixins/countdownMixin.js
var bluetooth = require('@/common/bluetooth.js');
import {getFunctionTag,setFunctionTag,getUserCurrentLocation} from '@/common/storage.js';

var config = require('@/common/config.js');
var common = require('@/common/common.js');
var http = require('@/common/http.js');
import i18n from '@/locale/index.js'
export default {
  data() {
    return {
		scrollTop: 0,
      carOnline: false, 
	  statusBarHeight:0,
	  popText:'',
	  cmdType:'',
	  myLocation:{},
	  popupControlShow:false
    };
  },
  onLoad(){
	 this.myLocation = getUserCurrentLocation()
	 this.locationMiXin()
  },
   mounted(){
  	  this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight || 0
  },
  methods: {
	  async locationMiXin(){
		  let myLocation = await this.$location()
		   this.myLocation = {
			   latitude:`${myLocation.latitude}`,
			   longitude:`${myLocation.longitude}`,
		   }
	  },
		tapOpenControl(e){
			const _carOnline= uni.getStorageSync('car_info').online == 0 //在线
			console.log('_carOnline',this._carOnline);
			const {name,type}=e
			this.setData({
				carOnline:_carOnline,
				popText:name,
				cmdType:type,
				popupControlShow:true
			})
			console.log(this.popupControlShow);
		},
		changClick(tab){
			const typeArr=['tirePressure','batteryInfo','navigation']
			const isOther=typeArr.findIndex(t => t === tab.type)!==-1
			console.log('isOther',isOther);
			if(isOther){
				const {name,type}=tab
				this.setData({
					popText:name,
					cmdType:type,
				})
				this.tapBlueToothCmd()
			}else{
				this.tapOpenControl(tab)
			}
		},
		closePopup(){
			this.popupControlShow=false
		},
		tapBlueToothCmd(cmd,type){
			console.log(cmd,type,'cmd,type');
			const car_info= uni.getStorageSync('car_info');
			const isCarLocation = (cmd=='more')?true:type
			if(this.cmdType=='batteryInfo'){
				uni.navigateTo({
					url:`/pages/batteryDetail/batteryDetail`
				})
			}else if(this.cmdType=='navigation'){
				const {
					address,
					latitude,
					longitude,
					car_name
				} =car_info
				uni.openLocation({
					latitude: latitude - 0,
					longitude: longitude - 0,
					scale: 15,
					name: car_name,
					address: address,
					success: function (res) {},
				})
				//获取胎压
			}else if(this.cmdType=='tirePressure'){
				this.bluetoothCmd()
			}else{
				console.log(this.carOnline,'this.carOnline');
				// 判断车辆是否在线状态 true 在线调用接口 不在线提示连接蓝牙
				if (this.carOnline) {
					if (this.cmdType == 'turnOnOrOff') {
						const switchType = this.contrilList.find(item => item.isTurnOn).isTurnOn
						const pData = {
							car_sn: car_info.car_sn,
							switch: switchType
						}
						const me=this
						common.loading();
						http.postApi(config.API_FLK_CAR_SWITCH, pData, (resp) => {
							uni.hideLoading();
							if (resp.data.code === 200) {
								common.simpleToast(me.popText + '成功');
								const activeTag = me.contrilList.map(item => {
									if('isLock' in item){
										item.isTurnOn = (item.isTurnOn == 1) ? 0 : 1
										item.name = i18n.t((item.isTurnOn == 1) ? '关机' : '开机')
									}
									return item
								})
								const tag = getFunctionTag().tag
								setFunctionTag({
									activeTag,
									tag
								})
								me.$emit('loadCarDetail',pData,car_sn)
							} else {
								common.simpleToast(resp.data.msg);
							}
						});
					}else{
						const testArr=[
							{type:'findCar',opt_type:1},
							{type:'openSeatBag',opt_type:0},
							{type:'openTailBox',opt_type:2},
							]
						const pData = testArr.find(i=>i.type===this.cmdType)
						const me=this
						common.loading();
						http.postApi(config.API_FLK_CAR_REMOTE_CONTROL, {...pData,car_sn: car_info.car_sn}, (resp) => {
							uni.hideLoading();
							if (resp.data.code === 200) {
								common.simpleToast(me.popText + '成功');
							} else {
								common.simpleToast(resp.data.msg);
							}
						});
					}
				} else {
					this.bluetoothCmd(isCarLocation)
				}
			}
			this.popupControlShow=false
		},
		bluetoothCmd(isCarLocation){
			const me=this
			const car_info= uni.getStorageSync('car_info');
			const app = getApp();
			console.log('test',app.globalData.connectionState[car_info.car_sn]);
			//蓝牙是否已经连接 未连接提示去连接 已连接下发对应指令
			const isBluetoothConnect = app.globalData.connectionState[car_info.car_sn].device
			if(isBluetoothConnect){
				//改成判断开关锁
				// const isTurnOn=this.contrilList.find(item => item.isTurnOn).isTurnOn==1
				const bluetoothCommands = {
					// 'turnOnOrOff': isTurnOn?bluetooth.turnOnCar:bluetooth.turnOffCar,
					'findCar': bluetooth.findCarCmd,
					'openSeatBag': bluetooth.openCarSeat,
					'openTailBox': bluetooth.openCarTrunk,
					'tirePressure': bluetooth.getCarPressure
				};
				common.loading();
				const command = bluetoothCommands[this.cmdType];
				const car_sn = uni.getStorageSync('car_info').car_sn
				if (command) {
					command(car_sn, () => {
						uni.hideLoading();
						if(this.cmdType=='openSeatBag'||this.cmdType=='openTailBox'){
							common.simpleToast('操作成功');
						}
						console.log(`发送${this.popText}指令结束`);
					});
				}
			}else{
				uni.showModal({
					title: '提示',
					content: '当前车辆处于离线，是否前往开启蓝牙配对操作车辆？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
							if(isCarLocation){
								uni.switchTab({
									url:'/pages/index/index'
								})
							}else{
								me.$emit('toBluetooth')
							}
							
						}
					},
					fail: function(res) {},
					complete: function(res) {},
				})
			}
		},
		// async loadData() {
		// 				return new Promise((resolve) => {
		// 					// setTimeout(resolve, 1000); // 模拟 5 秒请求时间
		// 					// this.trunOn(resolve)
		// 				});
		// 			},

	},

 
  beforeDestroy() {
  },
};
