// mixins/countdownMixin.js
var bluetooth = require('@/common/bluetooth.js');
import {getFunctionTag,setFunctionTag} from '@/common/storage.js';
var app = getApp();
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 {
      carOnline: false, 
	  statusBarHeight:0,
	  popText:'',
	  cmdType:'',
	  popupControlShow:false
    };
  },
   mounted(){
  	  this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight || 0
  },
  methods: {
		tapOpenControl(e){
			console.log(111);
			const _carOnline=uni.getStorageSync('car_info').online!=0 //在线
			console.log(this.popupControlShow);
			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(isCarLocation=false){
			const car_info= uni.getStorageSync('car_info');
			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{
				// 判断车辆是否在线状态 true 在线调用接口 不在线提示连接蓝牙
				if (this.carOnline) {
					if(this.cmdType=='turnOnOrOff'){
						const switchType=this.contrilList.find(item => item.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=>{
									item.isTurnOn=(item.isTurnOn==1)?0:1
									item.name=i18n.t((item.isTurnOn==1)?'关机':'开机')
									return item
								})
								const tag=getFunctionTag().tag
								setFunctionTag({activeTag,tag})
								
							} 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, (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 isBluetoothConnect = app.globalData.connectionStateChangeFunc[car_info.car_sn]
			if(isBluetoothConnect){
				const isTurnOn=this.contrilList.find(item => item.isTurnOn).isTurnOn==1
				console.log(isTurnOn,this.cmdType,this.contrilList);
				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];
				if (command) {
					command('900000997', () => {
						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) {},
				})
			}
		}
	},

 
  beforeDestroy() {
  },
};
