<template>
	<MeScroll  :up="up" :down="down" @up="upFn" :fixed="false" @down="downFn" @init="initMeScroll">
		<card  :list="list" ></card>
	</MeScroll>
</template>
<script>
	import MeScroll from '@/components/mescroll-body/mescroll-uni.vue'
	import card from './card.vue'
	var app=getApp()
	export default {
		components:{
			MeScroll,card
		},
		props: {
			refresh:Boolean,
			type: Number,
			i: Number,
			fireType: Number,
			item:Object
		},
		data() {
			return {
				isInit: false, // 是否初始化
				list: [], // 列表数据
				mescroll: null, // mescroll 对象
				// 上拉配置参数
				up: {
					noMoreSize: 2, 
					auto: false,
					page: {
						page: 0,
						size: 10
					}
				},
				// 下拉配置参数
				down: {
					use: true, 
					auto: false
				}
			}
		},
		watch:{
			refresh() {
				console.log("我要刷新了");
				this.mescroll.resetUpScroll()
			},
			type(val) {
				if(!this.isInit && val === this.i) {
					this.mescroll.resetUpScroll()
				}
			}
		},
		mounted() {
			if(!this.isInit && this.i === 0) {
				this.mescroll.resetUpScroll()
			}
		},
		methods: {
			/**
			 * @param {Object} mescroll 初始化组件
			 */
			initMeScroll(mescroll) {
				this.mescroll = mescroll
			},
			/**
			 * 企业通过 page接口查询，园区通过agencyPage接口查询
			 * 烟感和燃气通过设备前缀查询
			 * 消防栓通过设备类型查询
			 * @param {Object} mescroll 上拉回调
			 */
			async upFn(mescroll) {
				let that=this
				let loginType=this.$cache.get('loginType')
				let params={
					current:mescroll.num,
					size:mescroll.size,
				}
				if (loginType==this.$loginType.ENTERPRISE) {
					//公司统一信用代码
					// params.creditCode=this.$cache.get('creditCode')
					//公司id
					params.enterpriseId=this.$cache.get('enterpriseId')
				}else if (loginType==this.$loginType.AGENCY) {
					//园区tenantId
					// params.tenantId=this.$cache.get('agencyTenantId')
				}
				
				if (this.fireType==0) {
					//烟感报警
					params.prefix=this.$device_prefix.SMOKE
				}else if (this.fireType==1) {
					//燃气告警
					params.prefix=this.$device_prefix.GAS
				}else if (this.fireType==2) {
					//消防水压
					params.deviceType=this.$device_type.FIRE_HYDRANT
				}else if (this.fireType==3) {
					//电表记录
					params.deviceType=this.$device_type.ELECTRIC_METER
				}
				if (!this.$isEmpty(this.item.value)) {
					params.deviceStatus=this.item.value	//设备状态
				}
				try{
					let res=null;
					if (loginType==this.$loginType.ENTERPRISE) {
						//企业设备接口
						res=await this.$api.fireDevice.page(params)
					}else if (loginType==this.$loginType.AGENCY) {
						//园区设备接口
						res=await this.$api.fireDevice.agencyPage(params)
					}
					let data=res.data.records
					let length=data.length
					let total=res.data.total
					mescroll.endBySize(length, total);
					if(mescroll.num == 1) that.list = []; //如果是第一页需手动制空列表
					that.list=that.list.concat(data); //追加新数据
					
				}catch(e){
					mescroll.endErr();
				}
			},
			/**
			 * 下拉回调
			 * */
			downFn(mescroll) {
				setTimeout(()=>{
					this.mescroll.resetUpScroll()
				},1000)
			},
		}
	}
</script>

<style lang="scss" scoped>
	view{
		box-sizing: border-box;
	}
</style>
