<template>
	<view class="w-h-100">
		<view class="u-page">
			<u-list>
				<u-list-item v-for="(item, index) in truckList" :key="index">
					<u-cell v-if="idDefaultTruck==item.id" :title="item.truckCode + '-' + item.truckTypeName">
						<u-avatar slot="icon" shape="square" size="35" :src="item.url? item.url: defaultAvatar"
							customStyle="margin: -3px 5px -3px 0"></u-avatar>
							<span style="color: #e4493c;" class="item-span" slot="right-icon">已默认</span>
					</u-cell>
					<u-cell v-else :title="item.truckCode + '-' + item.truckTypeName">
						<u-avatar slot="icon" shape="square" size="35" :src="item.url? item.url: defaultAvatar"
							customStyle="margin: -3px 5px -3px 0"></u-avatar>
							<span @click="setChecked(item.id)" class="item-span" slot="right-icon">设置默认</span>
					</u-cell>
				</u-list-item>
				<u-list-item :key="10000">
					<u-cell size="large" title="新增车辆" isLink url="/pages/index/addTruck"> </u-cell>
				</u-list-item>
			</u-list>
		</view>
	</view>
</template>

<script>
	import request from '@/common/libs/request.js';
	export default {
		mounted() {
			this.initData()
		},
		data() {
			return {
				idDefaultTruck:"",
				checked:false,
				defaultAvatar: 'http://dn-dounengyin.qbox.me/Fvz4y4_hhBt0CaY9_8PMm8IeXc8p',
				truckList: [

				]

			}
		},
		methods: {
			setChecked(id){
				let _that = this;
				var trukData = {
					"id":id
				}
				var truckInfo = {
					 "truckInfo": JSON.stringify(trukData),
				};
				request.httpPost("/dnyDriver/changeDriverTruck", truckInfo, false).then(res => {
					console.log(res);
					request.httpPost("/dnyDriver/getDriverInfo", {}).then(res => {
						if (res.httpCode == 200) {
							uni.setStorageSync("idDefaultTruck", res.data.idDefaultTruck);
							_that.idDefaultTruck =  res.data.idDefaultTruck
						} else {
							uni.showToast({
								title: res.msg,
								icon: "error"
							})
						}
					});
					uni.showToast({
						title: "设置默认成功",
						icon: "none"
					})
				
					if (res.httpCode == 200) {
						_that.initData();
					} else {
						uni.showToast({
							title: res.msg,
							icon: "error"
						})
					}
				})
			},
			initData() {
				let _that = this;
				_that.idDefaultTruck = uni.getStorageSync('idDefaultTruck');
				request.httpPost("/dnyDriver/getTruckInfoList", {}).then(res => {
					if (res.httpCode == 200) {
						_that.truckList = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: "error"
						})
					}
				});
			}
		}
	}
</script>

<style>
	.cu-bar {
		padding-left: 60rpx;
		padding-top: 30rpx;
	}
	.item-span{
		
	}
	.avatar-txt span {
		display: block;
		width: 100;
	}

	.list-con {
		margin-top: 50rpx;
	}

	.w-h-100 {
		height: 100%;
		width: 100%;
	}

	.uni-row .flex-item-30 {
		color: #8799a3;
	}

	.flex-item-30 {
		flex-basis: 30%;
	}

	.flex-item-70 {
		flex-basis: 70%;
	}
</style>