<template>
	<view class="pagesCons">
		<view class="utabs">
			<u-tabs-swiper ref="uTabs" bar-width="160" :list="tabList" name="dispName" :current="current" @change="tabsChange" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			<view class="message">我的邀请码：{{yqcode}} <copyText :text="yqcode" label="邀请码"></copyText></view>
		</view>
		<swiper class="check-list" :current="swiperCurrent" @transition="transition" @change="swiperChange" @animationfinish="animationfinish">
			<swiper-item class="swiper-item" v-for="(tabs, index) in tabList" :key="index">
				<scroll-view scroll-y class="scroll-view" @scrolltolower="onreachBottom">
					<view  
					class="check-order-list" 
					v-if="index==current"
					v-for="(item,sindex) in list" 
					:key="item.id+'-'+sindex"
					>
						<view class="check-row">
							<view>{{item.applyTime}}</view>
							<view>
								<text>{{item.auditStatusDictValue}}</text>
							</view>
						</view>
						<view class="check-col">
							<view class="flex align_item" @click="toDetail(item)">
								<view class="imgs">
									<u-image :src="item.storeImage" :width="135" height="135" :border-radius="10"></u-image>
								</view>
								<view class="info">
									<view class="btext">{{item.contactPhone}} {{item.contactName}}</view>
									<view>{{item.storeName}}</view>
									<view class="addr">{{item.addrProvinceName +'-'+ item.addrCityName +'-'+ item.addrDistrictName +'-'+ item.addrDetail}}</view>
								</view>
							</view>
							<view style="width: 100%;text-align: right;" v-if="item.auditStatus == 'WAIT'">
								<u-button size="mini" hover-class="none" @click="handleAudit(item,0)" shape="circle">审核不通过</u-button>
								<u-button size="mini" type="success" @click="handleAudit(item,1)" :custom-style="customStyle" hover-class="none" shape="circle">审核通过</u-button>
							</view>
						</view>
					 </view>
					 <u-empty :src="`/static/${$config('themePath')}/def_no_data@3x.png`" icon-size="240" :text="noDataText" img-width="120" v-if="list.length==0 && status!='loading'" mode="list"></u-empty>
					 <view v-if="index==current" style="padding: 20upx;">
						 <u-loadmore :load-text="$config('loadText')" v-if="total>pageSize || status=='loading'" :status="status" />
					 </view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import { xprhStoreApplyQueryPage, xprhStoreApplyAudit } from '@/api/approveStore'
	import { clzConfirm } from '@/libs/tools';
	import copyText from '@/pages/common/copyText.vue'
	export default {
		components: {copyText},
		data() {
			return {
				status: 'loadmore',
				noDataText: '暂无数据',
				tabList: [],
				current: 0,
				swiperCurrent: 0,
				pageNo: 1,
				pageSize: 10,
				list: [],
				total: 0,
				action:'swiperChange', // 操作类型，上划分页，或左右滑动
				customStyle: {
					background: this.$config('primaryColor'),
					marginLeft: '20rpx'
				},
				yqcode: ''
			}
		},
		onLoad(options) {
			const _this = this
			_this.tabList = this.$store.state.vuex_storeAuthState
			// 邀请码
			_this.yqcode = this.$store.state.vuex_userData.extInfo.dealer.dealerInviteCode
			// 获取数据
			_this.getRow(1)
			uni.$on("updateAuthState",function(){
				_this.getRow(1)
			})
		},
		onUnload() {
			uni.$off("updateAuthState")
		},
		methods:{
			// tabs通知swiper切换
			tabsChange(index) {
				this.swiperCurrent = index
			},
			swiperChange(event){
				this.action = 'swiperChange'
				this.status = 'loading'
			},
			// swiper-item左右移动，通知tabs的滑块跟随移动
			transition(e) {
				let dx = e.detail.dx
				this.$refs.uTabs.setDx(dx)
			},
			// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
			// swiper滑动结束，分别设置tabs和swiper的状态
			animationfinish(e) {
				let current = e.detail.current
				let isCurtab = this.current == current
				if(this.status!="nomore"){
					let loadData = this.action == 'swiperChange' ? !isCurtab : isCurtab
					if(loadData){
						this.$refs.uTabs.setFinishCurrent(current)
						this.swiperCurrent = current
						this.current = current
						this.resetPage()
					}
				}
			},
			// scroll-view到底部加载更多
			onreachBottom() {
				this.action = 'onreachBottom'
				if(this.list.length < this.total){
					this.resetPage()
				}else{
					this.status = "nomore"
				}
			},
			// 查询列表
			getRow (pageNo) {
				if (pageNo) {
					this.pageNo = pageNo
				}
				let params = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					auditStatus: this.tabList[this.current].code
				}
				this.status = "loading"
				xprhStoreApplyQueryPage(params).then(res => {
					if (res.status == 200) {
						if(this.pageNo>1){
							this.list = this.list.concat(res.data.list || [])
						}else{
							this.list = res.data.list || []
						}
						this.total = Number(res.data.count) || 0
						if(this.current == 0){
							this.tabList[0].count = this.total
						}
					} else {
						this.list = []
						this.total = 0
						this.noDataText = res.message
					}
					this.status = "loadmore"
				})
			},
			resetPage(){
				this.status = 'loading';
				// 上划分页
				if(this.action == 'onreachBottom'){
					this.pageNo += 1
					this.getRow()
				}
				// 左右切换tab
				if(this.action == 'swiperChange'){
					this.list = []
					this.getRow(1)
				}
			},
			// 详情
			toDetail(row){
				uni.navigateTo({
					url: "/pages/approveStore/detail?sn="+row.applySn
				})
			},
			// 审核
			handleAudit (row, type) {
			  // 审核通过
			  if (type == 1) {
				uni.navigateTo({
					url: "/pages/approveStore/authPass?sn="+row.applySn
				})
			  } else {
			    const _this = this
			    clzConfirm({
			      title: '提示',
			      content: '确认审核不通过吗？',
				  success: function(res) {
				  	if (res.confirm || res.index == 0) {
						xprhStoreApplyAudit({
						  applySn: row.applySn,
						  auditStatus: 'REFUSE'
						}).then(res => {
						  if (res.status == 200) {
						     _this.getRow(1)
						  }
						})
				  	}
				  }
			    })
			  }
			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}
	.pagesCons{
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		.text-right {
			text-align: right;
		}
		.utabs{
			.message{
				padding: 0.5rem 1rem;
				background: #feffb9;
				margin-top: 0.5rem;
			}
		}
		.check-list{
			flex: 1;
			.swiper-item{
				width: 100%;
				height: 100%;
				overflow: hidden;
				.scroll-view {
					width: 100%;
					height: 100%;
					overflow: auto;
				}
			}
		}
		 
		// 列表样式
		.check-order-list{
			background: #ffffff;
			margin: 25upx;
			border-radius: 25upx;
			box-shadow: 1px 1px 3px #EEEEEE;
			.check-row{
				 display: flex;
				 justify-content: space-between;
				 >view{
					 &:first-child{
						 color: #999;
						 font-size: 24rpx;
						 width: 80%;
					 }
					 &:last-child{
						 font-size: 24rpx;
						 margin-top: 5rpx;
					 }
				 }
				 border-bottom: 2rpx solid #eee;
				 padding: 20rpx;
			}
			.check-col{
				padding: 20rpx;
				> view{
					padding: 3rpx 0;
					font-size: 26rpx;
				}
				.info{
					flex-grow: 1;
					padding: 0 10rpx 0 20rpx;
				}
				.btext{
					font-weight: bold;
					margin-bottom: 10rpx;
				}
				.addr{
					color: #999;
					margin-top: 6rpx;
					word-break: break-all;
				}
			}
		}
	}
</style>
