<template>
	<view>
		<view class="swiper-container">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"
				indicatorActiveColor="#FFAA48" :indicatorStyle="{bottom: '150rpx'}" indicatorInactiveColor="#fff"
				style="height: 412rpx;">
				<swiper-item class="w-100 h-100" v-for="item in state.banner" :key="item.id">
					<image class="w-100 h-100" :src="item.image" mode=""></image>
				</swiper-item>
			</swiper>
		</view>
		<view class="container">
			<view class="container-list">
				<view class="mb28 col3 font32">合伙人工作台</view>
				<!--  申请加入     v-if="userInfo.userHh != 1" -->
				<view class="container-list-but font28" v-if="userInfo.userHh != 1">
					<view class="explain">
						<view class="explain-text">
							<text>{{ configList?.partner_work_statement?.paramValueText }}</text>
							<!-- <view class="font24 add-but col-white join" style="background-color: #999;color: #fff;"
								@click="">暂未开放 ></view> -->
							<view class="font24 add-but col-white join" @click="handleJoin(1)">申请加入 ></view>
						</view>
						<image class="explain-img" 
                        style="background-color: transparent;"
                        :src="configList?.partner_work_statement?.paramValueImage"
							mode="aspectFit"></image>
					</view>
				</view>
				<!-- 申请加入后       -->
				<view class="flex flex-between font24 flex-wrap" v-if="userInfo.userHh==1">
					<view class="icon-list" @click="handleGoto(item)" v-for="item in iconState.list1" :key="item.id">
						<image class="mb20" :src="item.image" 
                        style="width: 68rpx;height: 68rpx;"
							mode="aspectFit"></image>
						<view>{{item.name}}</view>
					</view>
					<view class="icon-list"></view>
				</view>
			</view>
			<view class="container-list">
				<view class="mb28 col3 font32">伴宠师工作台</view>
				<!--  申请之前       -->
				<view class="container-list-but" style="background: #FFECE5" v-if="userInfo.userBcs != 1">
					<view class="explain">
						<view class="explain-text">
							<text>{{ configList?.pet_teacher_statement?.paramValueText }}</text>
							<view class="font24 add-but col-white join" @click="handleJoin(2)"
								style="background: #FF7935">申请加入 ></view>
						</view>
						<image class="explain-img" :src="configList?.pet_teacher_statement?.paramValueImage"
							mode="aspectFit"></image>
					</view>
				</view>
				<!--  申请之后      -->
				<view class="flex-rowl flex-wrap" v-if="userInfo.userBcs == 1">
					<view class="icon-list" v-for="item in iconState.list2" :key="item.id" @click="handleGoto(item)">
						<image class="mb20" :src="item.image" 
                        style="width: 68rpx;height: 68rpx;"
							mode="aspectFit"></image>
						<view>{{ item.name }}</view>
					</view>

					<!-- <view class="icon-list"@click="handleGoto1()">
						<up-image class="mb20" :show-loading="true"  width="68rpx"
							height="68rpx"></up-image>
						<view>123</view>
					</view> -->
				</view>
			</view>
		</view>
		<ModalCom :open="isoOpen" @close="modalClose" />
		<configPopup ref="configPopupRef" />
		
		<!-- 客服组件 -->
		<CustomerService />
	</view>

</template>

<script setup>
	import ModalCom from "./components/modal.vue"
	import {
		onShow
	} from "@dcloudio/uni-app"
	import {
		banner,
		joinHh,
		joiBcs,
		indexConfig,
		indexConfigIco,
		getbaseInfo
	} from "../../api/home.js"
	import {
		computed,
		onMounted,
		reactive,
		ref
	} from "vue";
	import tab from "../../plugins/tab";
	import {
		getLoginStatus
	} from "../../utils/useMixin";
	import {
		useStore
	} from "vuex"
	import configPopup from '@/components/configPopup.vue'
	import {
		answeBaseIsFinish,
		answeTrainIsFinish,
		getAnswerBuildData,
	} from '@/api/examination'
	import { insertUser, udpateUser, getUserOne } from '@/api/userTeacher'
	import utilsExam from '@/utils/exam.js'
	import {
		code
	} from "../../uni_modules/uview-plus/libs/function/test";
	const configPopupRef = ref(null)

	const store = useStore();
	const state = reactive({
		banner: []
	})
	onMounted(() => {
		getBanner()
		getpz()
	})
	const userInfo = computed(() => {
		return store.state.user.userInfo
	})
	const configList = computed(() => {
		return store.getters.configList
	})

	// 获取轮播图
	const getBanner = async () => {
		const res = await banner()
		if (res.code === 200) {
			state.banner = [...res.data]
		}
	}
	// 申请加入合伙人
	const num = ref(0)
	const isoOpen = ref(false)
	const handleJoin = (type) => {
		num.value = Number(type)
		const info = uni.getStorageSync("baseInfo")
		if (!info) {
			uni.navigateTo({
				url: "/pages/login/index"
			})
			return
		}
		const baseInfo = JSON.parse(info)
		if (type === 1) {
			// 加入合伙人工作台
			joinHh({
				appUserId: baseInfo.userId
			}).then(res => {
				isoOpen.value = true
				store.dispatch("getUserInfo");
			}).catch(() => {})
		} else {
			// 加入伴宠师工作台
			handleBc()
			// joiBcs({
			// 	appUserId: baseInfo.userId
			// }).then(res => {
			// 	isoOpen.value = true
			// 	changeBaseInfo(baseInfo.userId)
			// })
		}
	}
	// 更新基本信息
	const changeBaseInfo = (userId) => {
		getbaseInfo(userId).then(res => {

		})
	}

	const modalClose = () => {
		isoOpen.value = false
		if (num.value === 1) {
			show.value = false
		} else {
			show.value = false
		}

	}

	// 获取工作台图标
	const iconState = reactive({
		list1: [],
		list2: [],
	})
	const getpz = () => {
		indexConfig().then(res => {
			// 合伙人工作台菜单
			iconState.list1 = res?.rows?.slice(0, 4) || [];
			// 伴宠师工作台
			iconState.list2 = res?.rows?.slice(4) || [];
		})
	}

	const show = ref(true)
	const hhShow = ref(true)
	const handleGoto = (item) => {

		if (item.name == '平台手册') {
			// pet_platform_introduction
			// return configPopupRef.value.open('pet_platform_introduction')
			
			uni.navigateTo({
				url: '/pages/userManage/mine/platformManual'
			})
			return
		}

		if (item.url) {
			uni.navigateTo({
				url: item.url
			})
		}
	}
	const handleGoto1 = () => {
		uni.navigateTo({
			url: '/otherPages/authentication/examination/train'
		})
	}
	// 加入合伙人
	const handleAdd = (type) => {
		if (!getLoginStatus()) return
		switch (type) {
			case 1:
				break
			case 2:
				tab.navigateTo("/otherPages/workbenchManage/feedingStaff/index")
				break
		}
	}

	const handleOpen = () => {
		show.value = false
	}
	const handleBc = async () => {

		
		if (!userInfo.value || !userInfo.value.userId || userInfo.value.userId == 'undefined') {
			uni.navigateTo({
				url: "/otherPages/authentication/list/index"
			})
			return
		}

		uni.showLoading({
			title: '加载中...'
		})

		try {
			
			const data = await getAnswerBuildData(userInfo.value.userId)
			// const data = await getUserOne(userInfo.value.userId)
			
			if(!data || !data.id){
				
				uni.navigateTo({
					url: "/otherPages/authentication/list/index"
				})
				
				return
			}
		
			if ([1, 2].includes(data.status)) { // status: 0-审核中 1-通过 2-不通过
				uni.navigateTo({
					url: `/otherPages/authentication/examination/trainCompleted/index?status=${data.status}`
				})
				return
			}
			
			
			let code1 = await answeBaseIsFinish({
				userId: userInfo.value.userId
			})
			let code2 = await answeTrainIsFinish({
				userId: userInfo.value.userId
			})
			
			
			if (code1 && code2) {
				uni.navigateTo({
					url: `/otherPages/authentication/examination/trainCompleted/index?status=0`
				})
			} else if(code1){
				
				//判断基本答题是否正确
				if(!utilsExam.isSuccessPrecision(data.answerList.filter(item => item.type === '基本'))){
					uni.navigateTo({
						url: "/otherPages/authentication/examination/errorDetail"//
					})
					return
				}
				
				uni.navigateTo({
					url: "/otherPages/authentication/examination/baseCompleted"//train
				})
			}else{
				uni.navigateTo({
					url: "/otherPages/authentication/examination/start"//base
				})
			}
			
			
		} catch (e) {
			console.log(e);
		
			uni.navigateTo({
				url: "/otherPages/authentication/list/index"
			})
		} finally {
			uni.hideLoading()
		}
	}
</script>

<style scoped lang="scss">
	@import "index";
</style>