<template>
	<view class="carbox">
		<view class="flex1 cartop">
			<box-list :datainfo="list" type="mycar" @getDetail="getDetail"></box-list>
			<view class="cartips flexc mt16 mb16" @click="getChargeFn">
				<view class="tit flex1">当前有车辆<text class="co025">充电中…</text></view>
				<image :src="rimg"></image>
			</view>
		</view>
		<view class="plr12 flex0">
			<view class="rhbtn" @click="getAddFn">添加车辆</view>
		</view>
	<loading></loading>	
	</view>
</template>

<script>
	import config from '@/config'
	const baseUrl = config.baseUrl
	import boxList from "@/mine/components/box/list.vue"
	import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
	import {carList} from "@/api/work/car.js"
	import {getDictionaryFn} from "@/api/system/user.js"
	export default{
		components:{boxList},
		data(){
			return{
				rimg: require('@/mine/static/house/rimg.png'),
				list:[],
				pageSize: 10,
				pageNum: 1,
				reachflag: true,
				wtdt:'',
			}
		},
		onUnload() {
			uni.$off('refcarList')
		},
		onLoad: function() {
			uni.$on('refcarList',(res)=>{
				this.getDataFn()
			})
			this.init()
			this.getDataFn()
		},
		// 上拉触底加载更多触发事件
		onReachBottom() {
			if (this.reachflag) {
				this.pageNum++
				this.getDataFn()
			}
		},
		methods:{
			checkPermi, checkRole,
			init(){
				// 车型
				getDictionaryFn('cartype').then(res=>{
					if(res.code==200){
						this.sfList = res.data.map(v => {
							return {
								dictLabel: v.dictLabel,
								dictValue: v.dictValue
							}
						})
					}
				})
			},
			getAddFn(){
				this.$tab.navigateTo("/mine/pages/car/addcar")
			},
			getDetail(id){
				this.$tab.navigateTo("/mine/pages/car/addcar?id="+id)
			},
			getChargeFn(){
				this.$tab.navigateTo("/mine/pages/car/charge")
			},
			getDataFn(){
				var params={
					pageSize:this.pageSize,
					pageNum: this.pageNum,
				}
				carList(params).then(res=>{
					if(res.code==200){
						if (res.rows.length < this.pageSize) {
							this.reachflag = false
							this.wtdt = '到底了~';
						} else {
							var num = parseInt(res.rows.length) + parseInt(this.pageSize) * parseInt(this.pageNum -	1)
							if (num < res.total) {
								this.reachflag = true
								this.wtdt = ''
							} else {
								this.reachflag = false
								this.wtdt = '到底了~';
							}
						}
						var newArr=JSON.parse(JSON.stringify(res.rows))
						newArr.forEach(ite=>{
							ite.right=0;
						})
						if (this.pageNum == 1) {
							this.list = newArr;
						} else {
							this.list = this.list.concat(newArr)
						}
					}else{
						this.$toast(res.msg)
					}
				})
				
			},
		}
	}
</script>
<style>
	page{background: #F3F3F0;}
</style>
<style lang="scss" scoped>
.carbox{display: flex;flex-direction: column;min-height: 100vh;padding: 22rpx 18rpx 100rpx;
	.cartop{overflow: auto;}
	.cartips{background: #FFFFFF;border-radius: 20rpx;height: 98rpx;padding: 0 24rpx;
		image{width: 16rpx;height: 28rpx;flex: 0 0 auto;margin-left: 20rpx;}
		.tit{font-weight: bold;font-size: 26rpx;color: #272727;}
	}
}
</style>