<template>
	<MeScroll  :up="up" :down="down" @up="upFn" :fixed="false" @down="downFn" @init="initMeScroll">
		<u-toast ref="uToast"/>
		<card @pass="pass" @fail="fail" :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: {
			//审核意见
			opinion:String,
			//真实姓名
			realName:String,
			//联系方式
			phone:String,
			
			
			type: Number,
			i: Number,
			fireType: Number,
			item:Object
		},
		data() {
			return {
				//审核不通过时展示
				modelShow:false,
				//审核不通过的原因
				dataDetail:{},
				
				//所属公司
				enterpriseId:'',
				//登录类型
				loginType:'',
				//园区id
				agencyId:'',
				
				isInit: false, // 是否初始化
				list: [], // 列表数据
				mescroll: null, // mescroll 对象
				// 上拉配置参数
				up: {
					noMoreSize: 2, 
					auto: false,
					page: {
						page: 0,
						size: 10
					}
				},
				// 下拉配置参数
				down: {
					use: false, 
					auto: false
				}
			}
		},
		created() {
			this.enterpriseId= this.$cache.get('enterpriseId')
			this.loginType=this.$cache.get('loginType')
			this.agencyId=this.$cache.get('agencyId')
		},
		watch:{
			type(val) {
				if(!this.isInit && val === this.i) {
					this.mescroll.resetUpScroll()
				}
			}
		},
		mounted() {
			if(!this.isInit && this.i === 0) {
				this.mescroll.resetUpScroll()
			}
		},
		methods: {
			/**
			 * 通过审核
			 */
			pass(data){
				let that=this
				let item=this.$u.deepClone(data)
				this.$showModel("确定审核通过该员工信息吗？").then(res=>{
					item.examine=1
					item.auditTime=this.$createDateTime()
					this.$api.enterprisestaff.submit(item).then(res=>{
						if (res.success==true) {
							this.$showToast(res.msg)
							that.send(item)
							that.mescroll.resetUpScroll()
						}
					})
				})
			},
			/**
			 * 显示审核不通过的意见框
			 * @param {Object} item
			 */
			fail(item){
				this.$emit('showOpinion',item)
			},
			async send(item){
				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:item.openId,
					lang:"zh_CN",
					page:'/pages/login/login',
					miniprogramState:this.$miniprogramState,
				    templateId: this.$staffTmplIds[0],
						"data": {
						  "name1": {
							  "value": this.$cache.get('loginAccount')
						  },
						  "phrase5":{
							  "value": "员工认证"
						  },
						  "phrase2": {
							  "value": "审核已通过"
						  },
						  "thing3": {
							  "value": '已审核'
						  },
						}
				}
				this.$api.wxApi.subscribe(subscribeData).then(res=>{
					console.log(res);
				}).catch(err=>{
					console.error(err);
				})
			},
			/**
			 * @param {Object} mescroll 初始化组件
			 */
			initMeScroll(mescroll) {
				this.mescroll = mescroll
			},
			/**
			 * @param {Object} mescroll 上拉回调
			 */
			upFn(mescroll) {
				let that=this
				let obj={
					realName:this.realName,
					phone:this.phone,
					current:mescroll.num,
					size:mescroll.size
				}
				if (this.loginType==this.$loginType.ENTERPRISE) {
					//企业登录
					obj.enterpriseId=this.enterpriseId
					
				}else if(this.loginType==this.$loginType.AGENCY){
					//园区管理员登陆
					obj.agencyId=this.agencyId
				}
				
				
				if (this.item.value!=-1) {
					//不是查询全部就传examine
					obj.examine=this.item.value
				}
				try{
					this.$api.enterprisestaff.page(obj).then(res=>{
						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.$showToast('刷新成功')
					this.mescroll.resetUpScroll()
				},1500)
			},
		}
	}
</script>

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