<template>
	<MeScroll  :up="up" :down="down" @up="upFn" :fixed="false" @down="downFn" @init="initMeScroll">
		<card @showDetail="showDetail" @pass="pass" @fail="fail" :list="list" ></card>
	</MeScroll>
</template>
<script>
	import MeScroll from '@/comps/mescroll-body/mescroll-uni.vue'
	import card from './card.vue'
	var app=getApp()
	export default {
		components:{
			MeScroll,card
		},
		props: {
			params:Object,
			type: Number,
			i: Number,
			item:Object
		},
		data() {
			return {
				//审核不通过时展示
				modelShow:false,
				//审核不通过的原因
				opinion:'',
				dataDetail:{},
				
				memberId:'',
				isInit: false, // 是否初始化
				list: [], // 列表数据
				mescroll: null, // mescroll 对象
				// 上拉配置参数
				up: {
					noMoreSize: 3, 
					auto: false,
					page: {
						page: 0,
						size: 10
					}
				},
				// 下拉配置参数
				down: {
					use: true, 
					auto: false
				}
			}
		},
		created() {
			this.memberId=getApp().globalData.member.id
		},
		watch:{
			type(val) {
				if(!this.isInit && val === this.i) {
					this.mescroll.resetUpScroll()
				}
			}
		},
		mounted() {
			if(!this.isInit && this.i === 0) {
				this.mescroll.resetUpScroll()
			}
		},
		methods: {
			/**
			 * 获取待审核的数量
			 */
			fetchAuthRecordNum(){
				let that=this
				let memberId=app.globalData.member.id
				if (!this.$isEmpty(memberId)) {
					let params={
						member_id:memberId,
						size:99,
						auditStatus:0
					}
					// let operation='guestRecord/getListByMemberId'
					this.$http.getGuestRecordsByMemberId(params).then(res=>{
						let length=res.data.list.length
						that.$u.vuex('vuex_auth_audit_count',length)
						
					})
				}
			},
			/**
			 * 显示详情
			 * @param {Object} item
			 */
			showDetail(item){
				this.$emit('showDetail',item)
			},
			/**
			 * 通过审核
			 */
			pass(item){
				let that=this
				let {rootOrgId,orgPosition,orgId,...params}=item
				params.auditStatus=1
				let operation="guestRecord/updateGuestRecord"
				uni.showModal({
					title:"提示",
					content:"确定审核通过该访客信息吗？",
					showCancel:true,
					success: (res) => {
						if (res.confirm) {
							getApp().globalData.postRequest(params,operation,function(res){
								that.fetchAuthRecordNum()
								that.mescroll.resetUpScroll()
							})
						}
					}
				})
			},
			/**
			 * 审核不通过
			 * @param {Object} item
			 */
			fail(item){
				let that=this
				let {rootOrgId,orgPosition,orgId,...params}=item
				params.auditStatus=2
				let operation="guestRecord/updateGuestRecord"
				uni.showModal({
					title:"提示",
					content:"确定审核不通过该访客信息吗？",
					showCancel:true,
					success: (res) => {
						if (res.confirm) {
							getApp().globalData.postRequest(params,operation,function(res){
								that.fetchAuthRecordNum()
								that.mescroll.resetUpScroll()
							})
						}
					}
				})
			},
			/**
			 * @param {Object} mescroll 初始化组件
			 */
			initMeScroll(mescroll) {
				this.mescroll = mescroll
			},
			/**
			 * @param {Object} mescroll 上拉回调
			 */
			upFn(mescroll) {
				try{
					let that=this
					let params={
						member_id:that.memberId,
						current:mescroll.num,
						size:mescroll.size,
					}
					if (this.item.value!=-1) {
						params.auditStatus=this.item.value
					}
					if (!this.$isEmpty(this.params.id)) {
						params.id=this.params.id
					}
					if (!this.$isEmpty(this.params.guestName)) {
						params.guestName=this.params.guestName
					}
					if (!this.$isEmpty(this.params.guestTel)) {
						params.guestTel=this.params.guestTel
					}
					// let operation='guestRecord/getListByMemberId'
					this.$http.getGuestRecordsByMemberId(params).then(res=>{
						let data=res.data.list
						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()
					uni.showToast({
						title:"刷新成功",
						icon:"none",
						position:"top"
					})
				},1500)
			},
		}
	}
</script>

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