<template>
	<view class="box">
		<!-- 搜索 -->
		<view class="" style="padding:10rpx 20rpx;">
			<u-search :showAction="true" bgColor='white' @search="queryCarFn" placeholder="搜索车牌号" actionText="搜索"
				:animation="true" v-model="queryCar" @custom='queryCarFn'></u-search>
		</view>
		<view style="height: 80vh;" v-if="carList.length==0">
			<u-empty mode="search" marginTop='100' icon="/static/img/empty/data.png">
			</u-empty>
		</view>
		<view v-else class="car-list" v-for="item,index in carList" :key="index">
			<view class="car-info">
				<view class="car-info-l">
					<view class="">
						<u--image :src="item.pic" width="150rpx" height="150rpx" style="margin-right:20rpx"></u--image>
					</view>
					<view class="car-info-c">
						<view class="lincense">
							车牌号: {{item.license}}
						</view>
						<!-- <view class="lincense">
							行驶里程: 1212KM
						</view> -->
						<view class="lincense" v-if="item.flag">
							当前状态: 当前使用中
						</view>
						<view class="lincense" v-else>
							当前状态: 闲置中
						</view>

					</view>
				</view>
				<!--  -->
				<view class="car-info-r">
					<view class="" style="margin: 10rpx 50rpx;">
						<u-checkbox-group v-model="item.checked" placement="column" @change="checkboxChange(item.id)">
							<u-checkbox>
							</u-checkbox>
						</u-checkbox-group>
					</view>
					<view class="" v-show="item.id==carCheckedArr[0] &&type!=2">
						选择该车辆为监管车辆
					</view>
				</view>

			</view>
		</view>
		<view class="bottom">
			<view class="" @tap="open">
				已选择{{carCheckedArr.length}}辆车
			</view>
			<view class="bottom-btn" @tap="buyCarFn">
				<u-button type="primary" style="background-color: #0122C7;border: none;" text="确定"></u-button>
			</view>
		</view>

		<!-- 组件 -->
		<u-popup :show="showPop" mode="bottom" @close="close" @open="open">
			<view class="pop">
				<view class="check-car-list" v-for="item,index in carList" v-show="item.checked.length" :key="index">
					<u--image :src="item.pic" width="150rpx" height="150rpx"></u--image>
					<view class="" style="margin-top: 10rpx;">
						{{item.license}}
					</view>
					<view class="pop-check">
						<u-checkbox-group v-model="item.checked" @change="popChange(item.id)">
							<u-checkbox>
							</u-checkbox>
						</u-checkbox-group>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script scoped>
	// import { methods } from '../../uni_modules/uview-ui/libs/mixin/mixin'
	import {
		canBuyCarListApi,
		buyCarApi
	} from "@/api/property.js"
	import {
		Toast
	} from "../../libs/uniApi"
	export default {
		data() {
			return {
				queryCar: '',

				carList: [],
				carListI: [],
				showPop: false,
				carCheckeList: [],
				carCheckedArr: [],
				type: "",
			}
		},
		methods: {
			buyCarFn() {


				if (this.carCheckedArr.length == 0) {
					Toast("请先选择车辆!")
					return
				}

				let data = []
				this.carList.forEach(item => {
					this.carCheckedArr.forEach(items => {
						if (item.id == items) {
							data.push({
								id: item.id,
								license: item.license
							})
						}
					})
				})
				let datas = JSON.stringify(data)

				buyCarApi({
					cars: datas
				}).then(res => {
					Toast("申请成功!")
					setTimeout(() => {
						uni.redirectTo({
							url: '/subpkg/property/index'
						})
					}, 1000)

				})
				// uni.navigateTo({
				// 	url: '/subpkg/property/index'
				// })
			},
			queryCarFn() {
				this.carList = []
				this.carListI.forEach(item => {
					// console.log(item.license)
					if (item.license.includes(this.queryCar)) {
						this.carList.push(item)
					}
				})
			},
			checkboxChange(i) {
				if (this.carCheckedArr.includes(i)) {
					// let index=this.ca
					let index = this.carCheckedArr.indexOf(i)
					// console.log(index)
					this.carCheckedArr.splice(index, 1)
					// this.arr
				} else {
					this.carCheckedArr.push(i)
				}
				console.log(this.carCheckedArr)
			},
			popChange(index) {
				this.checkboxChange(index)
				// console.log(this.carCheckedArr)
			},
			close() {
				this.showPop = false
			},
			open() {
				if (this.carCheckedArr.length == 0) return
				this.showPop = true
			},
			change(e) {
				console.log(e)
			}
		},

		onLoad(option) {
			this.type = option.type
			// console.log(option.license)
			canBuyCarListApi().then(res => {
				res.data.forEach(item => {
					if (item.license == option.license) {
						item.flag = true
					}
				})

				this.carListI = res.data
				this.carList = Array.from(this.carListI);
			})

		}

	}
</script>

<style lang="scss" scoped>
	.box {
		padding-bottom: 100rpx;
	}

	.car-list {
		padding: 20rpx;
		background-color: #fff;

		.car-info {
			display: flex;
			justify-content: space-between;
			margin-bottom: 20rpx;

			.car-info-l {
				display: flex;
			}

			.car-info-c {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
			}

			.car-info-r {
				max-width: 20vw;
			}
		}
	}

	.pop {
		min-height: 40vh;
		display: flex;
		flex-wrap: wrap;
		box-sizing: border-box;
		padding: 20rpx 10rpx;
		padding-right: 0;
		padding-bottom: 100rpx;

		.check-car-list {
			// margin-right: 30rpx;
			width: 182.5rpx;
			display: flex;
			// justify-tent: ;
			flex-direction: column;
			align-items: center;
			position: relative;
		}

		.pop-check {
			position: absolute;
			right: 0;
			top: -5rpx;
		}
	}

	.bottom {
		background-color: white;
		height: 100rpx;
		position: fixed;
		bottom: 0;
		z-index: 1000;
		width: 100vw;
		box-sizing: border-box;
		border-radius: 30rpx 30rpx 0 0;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.bottom-btn {
			width: 200rpx;
		}

	}
</style>