<template>
	<view class="container">
		<u-popup v-model="detailShow" mode="center" width="650" :closeable="true" border-radius="10">
			<view style="padding: 60rpx 5rpx 10rpx;">
				<u-cell-group >
					<u-cell-item :icon-style="iconStyle"  :border-top="false" :arrow="false"  icon="account-fill"  title="访客姓名:" :value="detailData.guestName"></u-cell-item>
					<u-cell-item :icon-style="iconStyle"  :arrow="false"  icon="bookmark-fill"  title="身份证号:" :value="detailData.guestIdcard"></u-cell-item>
					<u-cell-item :icon-style="iconStyle"  :arrow="false"  icon="phone-fill"  title="手机号:" :value="detailData.guestTel"></u-cell-item>
					<u-cell-item :icon-style="iconStyle"  :arrow="false"  icon="home-fill"  title="访问园区:" :label="detailData.agencyName"></u-cell-item>
					<u-cell-item :icon-style="iconStyle"  :arrow="false"  icon="map-fill"  title="访问企业:" v-if="detailData.enterpriseName" :label="detailData.enterpriseName"></u-cell-item>
					<u-cell-item :icon-style="iconStyle"  :arrow="false"  icon="clock-fill"  title="访问时间:" :value="detailData.interviewTime"></u-cell-item>
					<u-cell-item :icon-style="iconStyle"  :border-bottom="false" :arrow="false"  icon="error-circle-fill"  title="访问缘由:" :value="detailData.guestReason"></u-cell-item>
				</u-cell-group>
			</view>
		</u-popup>
		
		<u-modal cancel-text="重置" cancel-color="#000000" @cancel="filterReset" :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="params.guestName" /></u-form-item>
					<u-form-item :border-bottom="false" label="手机号"><u-input v-model="params.guestTel" type="number"  /></u-form-item>
				</u-form>
			</view>
		</u-modal>
		
		<u-modal   :show-cancel-button="true" confirm-color="#5064eb"  @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" >
					<view class="flex text-center">
						<view class="cu-item flex-sub" :class="index==current?'base-color text-xl text-bold ':'text-lg'" v-for="(item,index) in tabs" :key="index" @tap="tabChange(index)" >
							<text>{{item.name}}</text>
						</view>
					</view>
				</scroll-view>
				<view v-if="$cache.get('loginType')" class="flex justify-center align-center padding-right-20" style="flex: 10%;"  @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 @showDetail="showDetail" @fail="fail" ref="mescrollItem" :params="params" :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 {
				iconStyle:{
					color:'#5064eb'
				},
				
				//详情
				detailShow:false,
				detailData:{},
				
				//筛选
				filterCount:0,
				filterShow:false,
				params:{},
				
				//审核意见
				failItem:{},
				opinionShow:false,
				opinion:'',
				
				current: 0,
				swiperCurrent:0,
				tabs: [
					{
						name: '全部',
						value:-1
					},
					{
						name: '待审核',
						value:0,
					},
					{
						name: '已通过',
						value:1
					},
					{
						name:'未通过',
						value:2
					}
				],
			}
		},
		onShow(){
			
		},
		onLoad(options) {
		},
		methods:{
			/**显示详情
			 * @param {Object} item
			 */
			showDetail(item){
				this.detailShow=true
				this.detailData=item
			},
			/**
			 * 审核不通过时显示弹出框
			 * @param {Object} item
			 */
			fail(item){
				this.failItem=item
				this.opinionShow=true
			},
			opinionConfirm(){
				let params={
					id:this.failItem.id,
					checkState:2,
					opinion:this.opinion
				}
				this.$api.guest.audit(params).then(res=>{
					if (res.success==true) {
						this.$u.toast('操作成功')
						this.send()
						this.refreshMescroll()
					}else{
						this.$u.toast('操作失败')
					}
				})
			},
			async send(){
				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:this.failItem.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": this.opinion || '审核不通过'
						  },
						}
				}
				let resp=await this.$api.wxApi.subscribe(subscribeData)
				console.log(resp);
			},
			/**
			 * 显示筛选弹窗
			 */
			show(){
				this.filterShow=true
			},
			/**
			 * 筛选
			 */
			filterConfirm(){
				this.$nextTick(() => {
					this.refreshMescroll()
				})
				this.getFilterCount()
				
			},
			/**
			 * 计算筛选的数量
			 */
			getFilterCount(){
				let n=0
				if (!this.$isEmpty(this.params.guestName)) {
					n++
				}
				if (!this.$isEmpty(this.params.guestTel)) {
					n++
				}
				this.filterCount=n
			},
			/**
			 * 重置筛选项
			 */
			filterReset(){
				this.filterCount=0
				this.params={}
				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-xl{
		font-size: 34rpx;
	}
	.container {
	  height: calc(100vh);
	  background-color: #F6F6F6;
	  padding: 86rpx 0rpx 0rpx;
	 .tabs {
	    position: fixed;
	    top: -2rpx;
		left: 0;
		width: 100%;
		background-color: #FFFFFF;
		box-sizing: border-box;
		z-index: 3;
	  }
}
</style>