<template>
	<view class="pageBg">
		
	<view class="pageBgImg">
		<topNavbar title="未绑定的车辆"></topNavbar>
		<view class="pad">
			<view class="searcBox">
				<searchRow placeholder="搜索车牌号" @searchFn="searchFn"></searchRow>
			</view>
			<view class="ul">
				<view class="card" v-for="(item,index) in list" :key="index">
					<view class="name">车牌号：{{item.licnum}}</view>
					<view class="text">车辆型号：{{item.manufacturer}}</view>
					<view class="flex-b">
						<view class="text">培训车型：{{item.perdritype}}</view>
						<view class="btnBg" @click="bindClick(item)">立即绑定</view>
					</view>
				</view>
			</view>
			
			<view style="padding-bottom: 20rpx;" v-if="list.length">
				<u-loadmore :status="status" />
			</view>
			<nodata v-if="!list.length&&status=='nomore'"></nodata>
		</view>
	</view>
	</view>
</template>

<script>
	import { carPage, coachBinding } from '@/config/api.js'
	export default {
		data() {
			return {
				list: [],
				params: {
					pageNo: 1,
					pageSize: 20,
					coachId: 0
				},
				total: 20,
				status: 'loading'
			}
		},
		onLoad() {
			this.params.schoolId = this.vuex_schoolId
			this.carPageFn()
		},
		onPullDownRefresh() {
			this.list = []
			this.params.pageNo = 1
			this.carPageFn().then(()=>{uni.stopPullDownRefresh()})
		},
		onReachBottom() {
			if(this.total>this.list.length) {
				this.carPageFn()
			}
		},
		methods: {
			async carPageFn() {
				const {data: res} = await carPage(this.params)
				this.params.pageNo ++
				this.list.push(...res.list)
				this.total = res.total
				if(this.list.length>=this.total) this.status = 'nomore'
				console.log(res)
			},
			searchFn(val) {
				console.log(val)
				this.params.licnum = val
				this.list = []
				this.params.pageNo = 1
				this.carPageFn()
			},
			async coachBindingFn(item) {
				let obj = {
					"carId": item.id,
					"coachId": this.vuex_coachId,
					"coachName": this.vuex_userInfo.name
				}
				const res = await coachBinding(obj)
				if(res.code==0) {
					this.$u.toast('绑定成功')
					setTimeout(()=>{
						uni.navigateBack()
					}, 1500)
				}
				console.log(res)
			},
			bindClick(item) {
				let title = `确定要将 ${item.licnum} 教练车绑定到您的名下吗？`
				let _this = this
				uni.showModal({
					title,
					success: function(res) {
						if (res.confirm) {
							_this.coachBindingFn(item)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
				
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.ul {
		padding-top: 20rpx;
	  .card {
		  padding: 20rpx;
		  margin-bottom: 20rpx;
	    .name {
			font-size: 28rpx;
			font-weight: 550;
			color: $themC;
	    }
	
	    .text {
			margin-top: 16rpx;
			font-size: 26rpx;
			color: #333;
	    }
	
	    .flex-b {
	      .text {
	
	      }
	
	      .btnBg {
			  height: 60rpx;
			  line-height: 60rpx;
			padding: 0 28rpx;
	      }
	    }
	  }
	}
</style>