<template>
	<view class="container">
		<u-toast ref="uToast"/>
		<u-modal cancel-text="重置" cancel-color="#000000" @cancel="filterReset" confirm-color="#5064eb"  :show-cancel-button="true"  @confirm="filterConfirm" title="筛选" :mask-close-able="true" v-model="filterShow" >
			<view class="slot-content" style="margin: 20rpx;">
				<u-form label-width="150"  ref="uForm">
					<u-form-item label="员工姓名"><u-input v-model="realName" /></u-form-item>
					<u-form-item :border-bottom="false" label="手机号"><u-input  v-model="phone" type="number"  /></u-form-item>
				</u-form>
			</view>
		</u-modal>
		
		<u-modal   :show-cancel-button="true"  @confirm="opinionConfirm" title="审核意见" :mask-close-able="true" v-model="opinionShow" >
			<view class="slot-content" style="margin: 20rpx;">
				<u-form label-width="150"  ref="uForm">
					<u-form-item :border-bottom="false"><u-input height="150"  placeholder="请输入审核意见(选填)" v-model="opinion" /></u-form-item>
				</u-form>
			</view>
		</u-modal>
		
		<view class="tabs flex flex-direction">
			<view class="flex">
				<scroll-view scroll-x class="bg-white nav" style="width: 90%;">
					<view class="flex text-center">
						<view class="cu-item flex-sub" :class="index==current?'text-blue text-xl text-bold cur':'text-lg'" v-for="(item,index) in tabs" :key="index" @tap="tabChange(index)" >
							{{item.name}}
						</view>
					</view>
				</scroll-view>
				<view class="flex justify-center align-center"  @click="show">
					<text class="cuIcon-filter "  style="font-size: 40rpx;"></text>
					<u-badge size="mini" type="error" :count="filterCount"></u-badge>
				</view>
			</view>
		</view>
		<view style="height: 100%;">
			<swiper style="height: 100%;" :current="current"  @change="swiperChange"
				@animationfinish="animationfinish">
				<swiper-item  v-for="(item, index) in tabs" :key="index">
					<scroll-view scroll-y style="height: 100%;">
						<item @showOpinion="showOpinion" ref="mescrollItem" :opinion="opinion" :realName="realName" :phone="phone" :refresh="refresh" :i="index" :item="item" :type="current"></item>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>
<script>
	import item from "./comps/item.vue"
	export default {
		components: {
			item
		},
		data() {
			return {
				// 审核不通过的审核意见
				opinion:'',
				// 显示审核不通过对话框
				opinionShow:false,
				// 审核不通过的对象
				failItem:{},
				
				filterCount:0,
				filterShow:false,
				realName:'',
				phone:'',
				
				current: 0,
				swiperCurrent:0,
				tabs: [
					{
						name: '全部',
						value:-1
					},
					{
						name: '待审核',
						value:0
					},
					{
						name: '已通过',
						value:1
					},
					{
						name:'未通过',
						value:2
					}
				],
			}
		},
		onShow(){
			let pages = getCurrentPages();  //获取所有页面栈实例列表
			let currPage = pages[ pages.length - 1];  //当前页页面实例
			if (currPage.data.flag==true) {
				this.realName=''
				this.phone=''
				this.refreshMescroll()
			}
		},
		onLoad() {
			
		},
		methods:{
			showOpinion(item){
				this.opinionShow=true
				this.failItem=item
			},
			
			/**
			 * 确定审核不通过回调
			 */
			opinionConfirm(){
				this.submitFailAudit()
			},
			/**
			 * 审核不通过
			 * @param {Object}
			 */
			submitFailAudit(){
				let item=this.$u.deepClone(this.failItem)
				let that=this
				item.auditStatus=2
				item.opinion=that.opinion
				item.auditTime=this.$createDateTime()
				that.$api.enterprisestaff.submit(item).then(res=>{
					if (res.success==true) {
						this.$showToast(res.msg)
						that.send(item)
						that.$nextTick(() => {
							that.refreshMescroll()
						})
					}
				})
			},
			/**
			 * 发送信息
			 */
			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.opinion || '审核不通过'
					    },
					  }
				}
				this.$api.wxApi.subscribe(subscribeData).then(res=>{
					console.log(res);
				}).catch(err=>{
					console.error(err);
				})
			},
			
			/**
			 * 筛选显示
			 */
			show(){
				this.filterShow=true
			},
			/**
			 * 筛选
			 */
			filterConfirm(){
				this.refreshMescroll()
				let n=0
				if (!this.$isEmpty(this.realName)) {
					n++
				}
				if (!this.$isEmpty(this.phone)) {
					n++
				}
				this.filterCount=n
			},
			/**
			 * 重置筛选项
			 */
			filterReset(){
				this.filterCount=0
				this.realName=''
				this.phone=''
				this.$nextTick(() => {
					this.refreshMescroll()
				})
			},
			/**
			 * 刷新列表
			 */
			refreshMescroll(){
				let curMescroll = this.getMescroll(this.current)
				curMescroll && curMescroll.resetUpScroll()
			},
			/**
			 * 获取Mescroll对象
			 * @param {Object} i
			 */
			getMescroll(i){
				let mescrollItems = this.$refs.mescrollItem;
				if(mescrollItems){
					let item = mescrollItems[i]
					if(item) return item.mescroll
				}
				return null
			},
			
			tabChange(index) {
				this.current = index
			},
			swiperChange(e) {
			  uni.pageScrollTo({
			      scrollTop: 0,
			      duration: 0
			  });
			  this.current = e.detail.current
			},
			animationfinish({detail: { current }}) {
				this.swiperCurrent = current;
				this.current = current;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.text-blue{
		color: $base-color;
	}
	.text-xl{
		font-size: 34rpx;
	}
	.container {
	  height: calc(100vh);
	  background-color: #F6F6F6;
	  padding: 88rpx 0rpx 0rpx;
	 .tabs {
	    position: fixed;
	    top: 0rpx;
		left: 0;
		width: 100%;
		background-color: #FFFFFF;
		box-sizing: border-box;
		z-index: 3;
	  }
}
</style>