<template>
	<MeScroll  :up="up" :down="down" @up="upFn" :fixed="false" @down="downFn" @init="initMeScroll">
		<card @delItem="delItem" @showDetail="showDetail" @pass="pass" @fail="fail" :loginType="loginType" :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: {
			params:Object,
			type: Number,
			i: Number,
			item:Object
		},
		data() {
			return {
				//审核不通过时展示
				modelShow:false,
				//审核不通过的原因
				opinion:'',
				dataDetail:{},
				
				loginType:'',
				//访客openId
				openId:'',
				//园区id
				agencyId:'',
				//企业id
				enterpriseId:'',
				
				isInit: false, // 是否初始化
				list: [], // 列表数据
				mescroll: null, // mescroll 对象
				// 上拉配置参数
				up: {
					noMoreSize: 3, 
					auto: false,
					page: {
						page: 0,
						size: 10
					}
				},
				// 下拉配置参数
				down: {
					use: true, 
					auto: false
				}
			}
		},
		created() {
			this.loginType=this.$cache.get('loginType')
			if (this.loginType==this.$loginType.AGENCY) {
				//园区管理员
				this.agencyId=this.$cache.get('agencyId')
			}else if (this.loginType==this.$loginType.ENTERPRISE) {
				//企业
				this.enterpriseId=this.$cache.get('enterpriseId')
			}else{
				//访客
				this.openId=this.$cache.get('openId')
			}
		},
		watch:{
			type(val) {
				if(!this.isInit && val === this.i) {
					this.mescroll.resetUpScroll()
				}
			}
		},
		mounted() {
			if(!this.isInit && this.i === 0) {
				this.mescroll.resetUpScroll()
			}
		},
		methods: {
			delItem(item){
				this.$dialog.showModal('确定要删除此记录？').then(res=>{
					this.$api.guest.remove(item.id).then(res=>{
						if (res.success==true) {
							this.mescroll.resetUpScroll()
						}
					})
				})
			},
			/**
			 * 显示详情
			 * @param {Object} item
			 */
			showDetail(item){
				this.$emit('showDetail',item)
			},
			/**
			 * 通过审核
			 */
			pass(item){
				let that=this
				let params={
					id:item.id,
					checkState:1
				}
				this.$showModel('确定要审核通过此申请记录吗？').then(()=>{
					this.$api.guest.audit(params).then(res=>{
						if (res.success==true) {
							this.$u.toast('操作成功')
							this.send(item.openId)
							this.mescroll.resetUpScroll()
						}else{
							this.$u.toast('操作失败')
						}
					})
				})
			},
			async send(openId){
				let tokenData={
					grantType:this.$api.wxData.subscribe_grant_type,
					appId:this.$api.wxData.appId,
					secret:this.$api.wxData.secret
				}
				let res=await this.$api.wxApi.getAccessToken(tokenData)
				let token=JSON.parse(res.data).access_token
				let subscribeData={
					accessToken:token,
					touser:openId,
					lang:"zh_CN",
					page:'/pages/guest/records/records',
					miniprogramState:this.$miniprogramState,
				    templateId: this.$staffTmplIds[0],
					  "data": {
						  "name1": {
							  "value": this.$cache.get('loginAccount')
						  },
						  "phrase5":{
							  "value": "开门申请"
						  },
						  "phrase2": {
							  "value": "审核通过"
						  },
						  "thing3": {
							  "value": '已阅'
						  },
						}
				}
				let resp=await this.$api.wxApi.subscribe(subscribeData)
			},
			/**
			 * 审核不通过
			 * @param {Object} item
			 */
			fail(item){
				this.$emit('fail',item)
			},
			/**
			 * @param {Object} mescroll 初始化组件
			 */
			initMeScroll(mescroll) {
				this.mescroll = mescroll
			},
			/**
			 * @param {Object} mescroll 上拉回调
			 */
			async upFn(mescroll) {
				try{
					let that=this
					let params={
						current:mescroll.num,
						size:mescroll.size,
					}
					//审核状态
					if (this.item.value!=-1) {
						params.checkState=this.item.value
					}
					if (!this.$isEmpty(this.params.guestName)) {
						params.guestName=this.params.guestName
					}
					if (!this.$isEmpty(this.params.guestTel)) {
						params.guestTel=this.params.guestTel
					}
					
					if (this.loginType==this.$loginType.AGENCY) {
						//园区管理员
						params.agencyId=this.agencyId
					}else if (this.loginType==this.$loginType.ENTERPRISE) {
						//企业
						params.enterpriseId=this.enterpriseId
					}else{
						//访客
						params.openId=this.openId
					}
					let res={}
					if(this.$isEmpty(params.openId)){
						//不是访客
						res=await this.$api.guest.page(params)
					}else{
						//是访客，访客调的是不需要token的接口
						//先去除token
						this.$cache.remove('tokenObj')
						this.$cache.remove('token')
						res=await this.$api.guest.page(params)
					}
					let data=res.data.records
					let total=res.data.total
					mescroll.endBySize(data.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>
