<template>
	<view class="">
		<u-toast ref="uToast" />
		<MeScroll :up="up" :down="down" @up="upFn" :fixed="false" @down="downFn" @init="initMeScroll">
			<!-- 审核通过 -->
			<user-card @deleteUserById="deleteUserById" v-if="i==0" :list="userList"></user-card>
			<apply-card v-if="i==1" :list="applyList"></apply-card>
			<audit-card v-if="i==2" :list="auditList"></audit-card>
		</MeScroll>
	</view>
</template>
<script>
	import MeScroll from '@/comps/mescroll-body/mescroll-uni.vue'
	import userCard from './user-card.vue'
	import applyCard from './apply-card.vue'
	import auditCard from './audit-card.vue'
	var app = getApp()
	export default {
		name: 'item',
		components: {
			MeScroll,
			userCard,
			applyCard,
			auditCard
		},
		props: {
			//房间号
			roomId: String,

			i: Number,
			current: Number,
			item: Object,

		},
		data() {
			return {
				userList: [], //审核通过的数据列表
				applyList: [], //申请记录的数据列表
				auditList: [], //住户审核的数据列表

				isInit: false, // 是否初始化
				list: [], // 列表数据
				mescroll: null, // mescroll 对象
				// 上拉配置参数
				up: {
					noMoreSize: 3,
					auto: false,
					page: {
						page: 0,
						size: 10
					}
				},
				// 下拉配置参数
				down: {
					use: false,
					auto: false
				}
			}
		},
		watch: {
			current(val) {
				if (!this.isInit && val === this.i) {
					this.mescroll.resetUpScroll()
				}
			}
		},
		mounted() {
			if (!this.isInit && this.i === 0) {
				this.mescroll.resetUpScroll()
			}
		},
		created() {

		},
		methods: {
			/**
			 * 删除用户
			 */
			deleteUserById(id) {
				let that = this;
				app.globalData.twoFailHint("确认删除该住户吗？", function() {
					let params = {
						member_id: app.globalData.member.id,
						user_id: id
					};
					// let operation = 'user/deleteUserById';
					that.$http.deleteUserById(params).then(res =>{
						if (res.data.result_code == 1) {
							that.$u.toast(res.data.result_msg)
							that.mescroll.resetUpScroll()
						} else {
							app.globalData.oneFailHint(res.data.result_msg);
						}
						
					});
				});
			},
			/**
			 * @param {Object} mescroll 初始化组件
			 */
			initMeScroll(mescroll) {
				this.mescroll = mescroll
			},
			/**
			 * @param {Object} mescroll 上拉回调
			 */
			upFn(mescroll) {
				let that = this
				if (this.i == 0) {
					//审核通过的用户列表
					this.getUserListByMemberId(mescroll)
				} else if (this.i == 1) {
					//申请记录列表
					this.getApplyListByMemberId(mescroll)
				} else if (this.i == 2) {
					//审核通过列表
					this.getAuditListByMemberId(mescroll)
				}
			},
			/**
			 * 下拉回调
			 * */
			downFn(mescroll) {
				setTimeout(() => {
					this.$u.toast('刷新成功')
					this.mescroll.resetUpScroll()
				}, 1500)
			},
			/**
			 * 通过memberid获取审核通过标签页下的所有成员
			 * @param {Object} mescroll
			 */
			getUserListByMemberId(mescroll) {
				let that = this;
				let params = {
					"pageNum": mescroll.num,
					"pageSize": mescroll.size,
					"member_id": getApp().globalData.member.id
				};
				if (!this.$isEmpty(this.roomId)) {
					params.roomId = this.roomId
				}
				// let operation = 'user/getAllUserByMemberId';
				try {
					that.$http.getAllUserByMemberId(params).then(res => {
						if (res.data.result_code == 1) {
							let data = res.data.list
							mescroll.endByPage(data.length, res.data.pages);
							if (mescroll.num == 1) that.userList = [];
							that.userList = that.userList.concat(data); //追加新数据
						} else {
							that.$u.toast(res.data.msg)
							mescroll.endErr();
						}

					});
				} catch (e) {
					mescroll.endErr();
				}
			},
			/**
			 * 获取申请列表
			 */
			getApplyListByMemberId(mescroll) {
				let that = this;
				let params = {
					"pageNum": mescroll.num,
					"pageSize": mescroll.size,
					"member_id": getApp().globalData.member.id
				};
				if (!this.$isEmpty(this.roomId)) {
					params.roomId = this.roomId
				}
				// let operation = 'applyUser/getAllApplyUserByMemberId';
				try {
					that.$http.getAllApplyUserByMemberId(params).then(res => {
						if (res.data.result_code == 1) {
							let data = res.data.list
							mescroll.endByPage(data.length, res.data.pages);
							if (mescroll.num == 1) that.applyList = [];
							that.applyList = that.applyList.concat(data); //追加新数据
						} else {
							that.$u.toast(res.data.msg)
							mescroll.endErr();
						}

					})
				} catch (e) {
					mescroll.endErr();
				}
			},
			/**
			 * 获取 住户审核列表
			 * @param {Object} mescroll
			 */
			getAuditListByMemberId(mescroll) {
				let room_ids = []
				this.vuex_own_room_list.forEach(item => {
					room_ids.push(item.id)
				})
				if (this.$isEmpty(room_ids)) {
					//就算没有自己的房屋，也要传room_ids,不然后台报错
					room_ids = ['-1']
				}

				let that = this;
				let params = {
					"pageNum": mescroll.num.toString(),
					"pageSize": mescroll.size.toString(),
					"check_states": 0, //审核状态
					"room_ids": room_ids
				};
				// let operation = 'applyUser/getAllApplyUserByRoomIds'
				try {
					that.$http.getAllApplyUserByRoomIds(params).then(res =>{
						if (res.data.result_code == 1) {
							let data = res.data.list
							mescroll.endByPage(data.length, res.data.pages);
							if (mescroll.num == 1) that.auditList = [];
							that.auditList = that.auditList.concat(data); //追加新数据
						} else {
							that.$u.toast(res.data.msg)
							mescroll.endErr();
						}
						
					})
				} catch (e) {
					mescroll.endErr();
				}
			},

		}
	}
</script>

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