<template>
	<view>
	<navBar :name="$t('请选择设备')" type="select"/>
	<view class="car-list">
		<view v-for="(item,index) of carList" :key="index" @click="clickItem(item)"  :class="['car-list-card',  car_info.car_sn == item.car_sn?'car-list-card-i' :'']">
		    <view class="flex-row">
		    	<view class="car-name" :class="{ 'car-name-i' : car_info.car_sn == item.car_sn}">{{item.car_name}}</view>
		    	<!-- <view class="car-name1">{{item.car_sn}}</view> -->
		    </view>
			<image v-if="car_info.car_sn == item.car_sn && isNearLockCheck" class="icon" src="/static/resource/images/gyq_ly.png" mode=""></image>
			<!-- https://qiniu.bms16.com/Fg8_p7083jpsy8BXG4bR6yMs7jQX -->
			<image class="img" :src="item.model_images" mode="aspectFit"></image>
			<view v-if="car_info.car_sn == item.car_sn" class="select">
				<image class="icon1" src="/static/resource/images/gyq_select.png" mode=""></image>
				<text>{{ $t('当前选择') }}</text>
			</view>
		</view>
		<!-- <view class="options">
			<view @click="navCarDetail" class="btn btn-left">{{ $t('{{ $t('租赁设备') }}') }}</view>
			<view class="btn btn-right">{{ $t('{{ $t('绑定设备') }}') }}</view>
		</view> -->
		<!-- <Confirm
        v-model="showConfirm"
        :dialog-info="{
          text: $t('{{ $t('是否切换到车辆') }}' + clickItemData.car_name ),
          showCancelButton: true
        }"
        @confirm="unbindSubmit"
      /> -->
	</view>
</view>
</template>

<script>
 import Confirm from '@/component/comPopup/Confirm'
	var config = require('../../common/config.js');
	
	var config = require('../../common/config.js');
	var common = require('../../common/common.js');
	var http = require('../../common/http.js');
	var storage = require('../../common/storage.js');
		var bluetooth = require('@/common/bluetooth.js');
	export default {
		components: {
			Confirm,
		},
		data() {
			return {
				showConfirm: false,
				carList:[],
				car_sn:"",
				car_info:{},
				clickItemData:{},
				isNearLockCheck:false
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			const app = getApp();
			this.isNearLockCheck=app.globalData.nearLockCheck
			this.loadUserCarList()
		},
		methods: {
			navCarDetail(){
				uni.switchTab({
					url: '/pages/service/service'
				})
			},
			clickItem(item){
				if(item.car_sn == this.car_info.car_sn) return
				
				this.clickItemData = item
				// console.log(item);
				this.showConfirm = true
				// uni.setStorageSync('car_info',item)
				// this.car_info = item
				this.unbindSubmit()
			},
			unbindSubmit(){
				let car_sn = this.clickItemData.car_sn
				if(!car_sn) return
				const me = this
				common.loading()
				http.postApi(config.API_FLK_CAR_DETAIL, {car_sn}, (resp) => {
					uni.hideLoading();
					if (resp.data.code === 200) {
						resp.data.data.car_sn = car_sn
						uni.setStorageSync('car_info', resp.data.data);
						me.car_info = resp.data.data
						const app = getApp();
						//清空当前设备蓝牙相关
						app.globalData.nearLockCheck=false
						app.globalData.nearLockInfo={}
						app.globalData.sensitivityType=0
						app.globalData.connectionState={}
						
						me.bluetoothClose()
						common.simpleToast(this.$t('切换成功！'))
						setTimeout(()=> {
							uni.navigateBack({
								delta:1
							})
						}, 900)
					} else {
						common.simpleToast(resp.data.msg);
					}
				})
				
			},
			loadUserCarList(){
				this.car_info = uni.getStorageSync('car_info') || {};
				const me = this
				http.postApi(config.API_FLK_CAR_DEVICE_LIST, {}, (resp) => {
					if (resp.data.code === 200) {
						const carList=resp.data.data.list
						resp.data.data.list.map(item=>{
							item.model_images = item.model_images.split(',')[0]
						})
						me.setData({
							carList,
						})
					} else {
						common.simpleToast(resp.data.msg);
					}
				})
			},
			bluetoothClose: function() {
				bluetooth.closeBluetoothAdapter();
				bluetooth.closeDevice(
					this.car_info.car_sn,
					() => {
						console.log(this.$t('关闭蓝牙连接'));
						// this.setData({
						// 	bt_loading: false
						// });
					},
					() => {}
				);
				bluetooth.offCharacteristicStateChange(this.car_info.car_sn, 'index');
				bluetooth.offConnectionStateChange(this.car_info.car_sn, 'index');	 
			},
		}
	};
</script>
<style lang="scss" scoped>
.car-list{
    padding: 24rpx;
}
.car-list-card{
	width:100%;
    height: 596rpx;
    background: linear-gradient( 135deg, #DEE0E8 0%, rgba(255,255,255,0.4) 50%, #F1F3F4 100%);
    border-radius: 40rpx;
    border: 6rpx solid #FFF;
    position: relative;
	padding: 30rpx;
	margin-bottom: 20rpx;
	.select{
		position: absolute;
		right: 0;
		top: 0;
		width: 164rpx;
		height: 78rpx;
		text-align: center;
		.icon1{
			width: 164rpx;
			height: 78rpx;
			position: absolute;
			left: 0;
			top: 0;
			
		}
		text{
			position: relative;
			z-index: 1;
			font-family: PingFangSC, PingFang SC;
			font-weight: 600;
			font-size: 28rpx;
			color: #FFFFFF;
			line-height: 28rpx;
			margin-top: 10rpx;
			display: block;
		}
	}
	.icon{
		position: absolute;
		width: 48rpx;
		height: 48rpx;
		left: 30rpx;
		top: 120rpx;
	}
	.img{
		width: 520rpx;
		height: 400rpx;
		display: block;
		margin: auto;
		margin-top: 40rpx;
	}
}
.car-list-card-i{
    background: linear-gradient( 135deg, #DEEAFF 0%, rgba(220,244,251,0.4) 50%, #DAFFF6 100%);
}
.car-name{
    font-family: PingFangSC, PingFang SC;
    font-weight: 600;
    font-size: 36rpx;
    color: #828DA2;
	width: 460rpx;
	text-overflow: ellipsis;
	overflow: hidden;
	white-space: nowrap;
}
.car-name-i{
    color: #060809;
}
.options {
        padding: 0 28rpx;
        display: flex;
        justify-content: space-between;
        margin-bottom: 44rpx;
		margin-top: 44rpx;
        .btn {
            width: 336rpx;
            height: 98rpx;
            border-radius: 50rpx;
            border: 4rpx solid #060809;
            font-size: 36rpx;
            display: flex;
            align-items: center;
            justify-content: center;
        }
  
        .btn-left {
            color: #060809;
            background: rgba(255, 255, 255, 0.6);
			margin-right: 22rpx;
        }
  
        .btn-right {
            color: #fff;
            background: #060809;
        }
    }
</style>