<template>
	<view id="warp">
		<view class="car_list" v-for="(item,index) in carYardList" :key="index">
			<view class="wid name">
				<view class="name_title">
					<view class="lef_nam">{{item.contactPerson}}</view>
					<view class="dd">{{item.contactPhone}}</view>
				</view>
				<view class="clear">
					<uni-icons type="closeempty" color="#bababa" size="20" @tap="clearCarYard(item.id)"></uni-icons>
				</view>
			</view>
			<view class="wid address">
				<view class="lef_nam">车场位置:</view>
				<view class="ddd">{{item.address}}</view>
			</view>
			<view class="wid footer">
				<radio-group @change="radioChange(item.id)">
					<label>
						<radio color="#1B64F8FF" :value="item.isDefault" :checked="item.isDefault"/>
						<text :class="item.isDefault?'text_avtive':''">{{item.isDefault?'已设默认':'默认'}}</text>
					</label>
				</radio-group>
				<view class="btn" @tap="changeCarYard(item.id)">编辑</view>
			</view>
		</view>
		<view class="bottomTitle" v-if="showNone && carYardList.length > 5">
			<text>没有更多数据了 ~</text>
		</view>
		<view v-if="showNoCar" class="blank">
			<image src="/static/images/common/none.png"></image>
			<text>暂无车场 ~~ </text>
		</view>
		<view class="zw"></view>
		<view class="add">
			<view class="add_btn" @tap="addCarYard">添加车场位置</view>
		</view>
	</view>
</template>

<script>
	const {urlList,https} = require('@/static/api');
	export default {
		data() {
			return {
				carYardList:[],
				showNone:false,
				showNoCar:false,
				isLoading:false, //是否在加载中
				page:1,
				pageSize:10,
			};
		},
		onLoad() {
			uni.showLoading({
				title:'加载中...'
			})
		},
		onShow() {
			this.page = 1;
			this.getCarYardList(1);
		},
		onReachBottom() {
			console.log('上拉加载')
			if(!this.isLoading){
				this.isLoading = true;
				this.page+=1;
				this.getCarYardList(this.page);
			}
		},
		methods:{
			// 设置默认车场
			radioChange(id){
				https(urlList.setDefaultCarYard+'/'+id,'PUT','','正在设置...').then(data => {
					uni.showToast({
						title:'设置成功',
						icon:'none',
						duration:1500
					});
					this.page = 1;
					this.getCarYardList(1);
				}).catch(err => {
					uni.showToast({
						title:err,
						icon:'none',
						duration:1500
					})
				})
			},
			// 删除
			clearCarYard(id){
				const that = this;
				uni.showModal({
					content:'是否确认删除此车场?',
					success: function (res) {
						if (res.confirm) {
							https(urlList.clearCarYard+'/'+id,'DELETE','','正在删除...').then(data => {
								uni.showToast({
									title:'删除成功',
									icon:'none',
									duration:1500
								})
								that.page = 1;
								that.getCarYardList(1);
							}).catch(err => {
								uni.showToast({
									title:err,
									icon:'none',
									duration:1500
								})
							})
						}
					}
				})
			},
			// 修改
			changeCarYard(id){
				uni.navigateTo({
					url:'/pages/carYardAdd/carYardAdd?id='+ id
				})
			},
			// 新增
			addCarYard(){
				uni.navigateTo({
					url:'/pages/carYardAdd/carYardAdd'
				})
			},
			// 获取车场列表
			getCarYardList(page){
				const _that = this;
				_that.showNoCar = false;
				https(urlList.getCarYardList+'/'+page+'/'+this.pageSize,'GET','','').then(data => {
					// console.log('chec',data)
					let list = data.data.items;
					if(list.length !== 0){
						if(page == 1){
							_that.carYardList = list;
						}else{
							_that.carYardList = _that.carYardList.concat(list);
						}
					}else{
						if(page > 1){
							_that.page-=1;
							_that.showNone = true;
						}else{
							_that.carYardList = [];
							_that.showNoCar = true;
						}
					};
					uni.hideLoading();
					_that.isLoading = false;
				}).catch(err => {
					uni.showToast({
						title:err,
						icon:'none',
						duration:1500
					})
					_that.isLoading = false;
					if(_that.page > 1){
						_that.page-=1;
					}
					uni.hideLoading()
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1;
			this.isLoading = true;
			this.getCarYardList(1);
		},
	}
</script>
<style>
	page {
		background-color: #F3F4F6;
	}
</style>
<style lang="scss" scoped>
#warp{
	width: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
	.add{
		width: 95%;
		display: block;
		position: fixed;
		bottom:48rpx;
		.add_btn{
			width: 100%;
			height: 90rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color:white;
			font-size: 35rpx;
			background-color:#1B64F8FF;
			border-radius: 10rpx;
		}
	}
	.zw{
		width: 100%;
		height: 130rpx;
	}
	.lef_nam{
		width: 150rpx;
		white-space:nowrap;
		overflow:hidden;
		text-overflow:ellipsis;
		font-weight: bold;
		font-size: 29rpx;
	}
	.ddd{
		width: calc(100% - 150rpx);
		height: 90rpx;
		line-height: 45rpx;
		color: #353535;
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
	}
	.car_list{
		width: 95%;
		padding: 20rpx 0rpx 20rpx 0rpx;
		background-color: white;
		display: flex;
		flex-direction: column;
		align-items: center;
		border-radius: 10rpx;
		margin-top: 20rpx;
		font-size: 27rpx;
		.wid{
			width: 95%;
		}
		.name{
			// margin-top: 10rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			.name_title{
				display: flex;
				flex-direction: row;
				align-items: center;
			}
			.clear{
				display: flex;
				justify-content: flex-end;
			}
		}
		.address{
			margin-top: 20rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
		}
		.footer{
			margin-top: 15rpx;
			padding-top: 20rpx;
			border-top: 1rpx solid #f5f4f8;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			.text_avtive{
				color: #1B64F8FF;
			}
			.btn{
				padding: 8rpx 25rpx;
				background-color: #1B64F8FF;
				color: white;
				font-size: 25rpx;
				border-radius: 30rpx;
			}
		}
	}
	.bottomTitle{
		width: 100%;
		height: 70rpx;
		color: #666666;
		font-size: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.blank{
		height: 100%;
		width:100%;
		font-size: 24rpx;
		color: #999999FF;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.blank image{
		width: 300rpx;
		height: 240rpx;
		margin-bottom: 40rpx;
	}
}
</style>
