<template>
	<view class="carbox">
		<view class="flex1 cartop">
			<box-list :datainfo="list" type="mycar" :clcxList="clcxList" @getDetail="getDetail" @getDelFn="getDelFn"></box-list>
		</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 "@/work/components/car/list.vue"
	import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
	import {carList,carDel} from "@/api/work/car.js"
	import {getDictionaryFn} from "@/api/system/user.js"
	export default{
		components:{boxList},
		data(){
			return{
				rimg: require('@/people/static/people/rimg.png'),
				list:[],
				pageSize: 10,
				pageNum: 1,
				reachflag: true,
				wtdt:'',
				clcxList:[],
				houseId:'',
			}
		},
		onUnload() {
			uni.$off('carlist')
		},
		onLoad: function(e) {
			uni.$on('carlist',(res)=>{
				this.getrefreshData()
			})
			this.houseId=e.id
			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.clcxList = res.data.map(v => {
							return {
								dictLabel: v.dictLabel,
								dictValue: v.dictValue
							}
						})
					}
				})
			},
			getDelFn(data){
				var that=this;
				carDel(data).then(res=>{
					if(res.code==200){
						this.$toast("删除成功");
						setTimeout(function(){
							that.getrefreshData()
						},1500)
					}
				})
			},
			getrefreshData(){
				this.pageNum=1;
				this.list=[];
				this.reachflag=true;
				this.getDataFn()
			},
			getAddFn(){
				this.$tab.navigateTo("/work/pages/manage/addcar")
			},
			getDetail(id){
				this.$tab.navigateTo("/work/pages/manage/addcar?id="+id)
			},
			getChargeFn(){
				this.$tab.navigateTo("/mine/pages/car/charge")
			},
			getDataFn(){
				var params={
					pageSize:this.pageSize,
					pageNum: this.pageNum,
					houseId:this.houseId,
				}
				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>