<template>
	<view class="container">
		<view class="tabs">
			<scroll-view scroll-x class="bg-white nav">
				<view class="flex text-center">
					<view class="cu-item flex-sub" :class="index==current?'text-base cur text-xl text-bold ':'text-lg'"
						v-for="(item,index) in tabs" :key="index" @tap="tabChange(index)">
						{{item.name}}
					</view>
				</view>
			</scroll-view>
		</view>
		<view style="height: 100%;">
			<swiper style="height: 100%;" :current="current" @change="swiperChange" @animationfinish="animationfinish">
				<swiper-item>
					<scroll-view scroll-y style="height: 100%;">
						<card1 ></card1>
					</scroll-view>
				</swiper-item>
				<swiper-item>
					<scroll-view scroll-y style="height: 100%;">
						<card2 @login="showLogin"></card2>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
		
		<login ref="login" @signIn="signIn"></login>
	</view>
</template>
<script>
	import card1 from "./comps/card1.vue"
	import card2 from "./comps/card2.vue"
	import login from "@/components/login.vue"
	export default {
		components:{
			card1,card2,login
		},
		data() {
			return {
				current: 0,
				swiperCurrent: 0,
				tabs: [{
						name: '参与活动',
						value: 0
					},
					{
						name: '代理推广',
						value: 1
					}
				],
			}
		},
		onLoad(options) {
			this.current=options.current || 0
		},
		methods: {
			showLogin(){
				this.$refs.login.show()
			},
			showPhoneModal(){
				this.$refs.login.showPhoneModal()
			},
			signIn(resp){
				this.judgeAgenterType()
				if (!this.vuex_phone) {
					this.showPhoneModal()
				}
			},
			judgeAgenterType(){
				let params={
					userId:this.vuex_userId,
					enabled:1
				}
				this.$api.agenter.list(params).then(res=>{
					if (!this.$isEmpty(res.data.records)) {
						let item=res.data.records[0]
						let type=this.$isEmpty(item.agenterTypeId)?1:2
						this.$u.vuex('vuex_agenter_type',type)
					}
				})
			},
			showModal(){
				if (this.current==1 && this.$isEmpty(this.vuex_userId)) {
					this.showLogin()
					return
				}
				if (this.current==1 && this.$isEmpty(this.vuex_phone)) {
					this.showPhoneModal()
				}
			},
			tabChange(index) {
				this.current = index
				this.showModal()
			},
			swiperChange(e) {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.current = e.detail.current
				this.showModal()
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.swiperCurrent = current;
				this.current = current;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.text-xl {
		font-size: 34rpx;
	}
	
	.text-base{
		color: #EF9944;
	}

	.container {
		height: calc(100vh);
		background-color: #F6F6F6;
		padding: 78rpx 0rpx 0rpx;

		.tabs {
			position: fixed;
			top: -16rpx;
			left: 0;
			width: 100%;
			background-color: #FFFFFF;
			box-sizing: border-box;
			z-index: 3;
		}
	}
</style>