<template>
	<uni-popup ref="popupRef" :animation="false" :is-mask-click="false"
	 mask-background-color="rgba(255, 255, 255, 0.6);" :is-shadow="false">
		<view class="ezy-catalogue-dialog">
			<uni-icons @click="handleClose" class="catalogue-close-btn"></uni-icons>
			<!-- 目录区域 -->
			<uni-collapse v-model="activeCollapse" class="ezy-catalogue-collapse" accordion>
				<uni-collapse-item title-border="none" :border="false" :name="index+''" v-for="(item,index) in list" 
				class="collapse-item-box" :class="{ 'collapse-active': activeCollapse === index+''}">
					<template v-slot:title>
							<view @click.stop="handleSelectZhang(item,index)" class="collapse-title">
								<view class="collapse-num-box">{{index+1}}</view>
								<!-- 章名 -->
								<text class="collapse-name">{{item.zhangName}}</text>
								<!-- 锁 -->
								<template v-if="AuthCode !== 'VIP'">
									<view class="collapse-lock" v-if="index!=0"></view>
								</template>
							</view>
					</template>
					<view class="collapse-content-box">
						<view v-for="(jie,cindex) in item.jieList" class="collapse-content-item">
							<text class="jie-index">{{`${index+1}.${cindex+1}`}}</text>
							<text class="text">{{jie.jieName}}</text>
						</view>
					</view>
				</uni-collapse-item>
			</uni-collapse>
			<tip-small-dialog ref="confirmDialogRef" @confirm-btn="handleConfirmPay" :content="Message"></tip-small-dialog>
			<tip-middle-dialog ref="youkeDialogRef" @confirm-btn="ykConfirm" :content="MESSAGE_VISITER_TO_LOGIN"></tip-middle-dialog>
		</view>
	</uni-popup>
</template>

<script setup>
	import {
		useCatalogue
	} from './useCatalogue';
	import {
		ref
	} from "vue";
	import {
		toast
	} from "@/utils/common.js"
	import {getUserIdentity} from "@/utils/common.js"
	import {MESSAGE_VISITER_TO_LOGIN,MESSAGE_BEFORE_PAY} from "@/utils/constant.js"
    import tipSmallDialog from '@/components/dialog/tipSmallDialog.vue'
	import tipMiddleDialog from '@/components/dialog/tipMiddleDialog.vue';
	import {
	  onShow
	} from '@dcloudio/uni-app';
	const growthType = ref(null);
	const AuthCode = ref(null);
	onShow(() => AuthCode.value = getUserIdentity()); // 用户身份

	const $emit = defineEmits(['change-zhang'])

	const { getCatalogue} = useCatalogue();

	const popupRef = ref(null); // 索引
	const confirmDialogRef = ref(null);
	const activeCollapse = ref('');
	const Message = MESSAGE_BEFORE_PAY;
	const youkeDialogRef = ref(null);

	const props = defineProps({
		list: {
			type: Array,
		}
	})

	function ykConfirm() {
		uni.redirectTo({
			url: '/pages/login/index'
		});
	}

	/**
	 * @summary 展示弹窗 暴露函数
	 */
	async function showPopup() {
		handleShow();
	}


	/**
	 * @summary 展示目录弹窗
	 */
	function handleShow() {
		popupRef.value.open('center');
	}

	/**
	 * @summary 关闭目录弹窗
	 */
	function handleClose() {
		popupRef.value.close();
	}
	/**
	 * @summary 选中
	 * @param({zhangId:string}) data
	 */
	function handleSelectZhang(data,index) {
		if(index !=0 && AuthCode.value == 'Visitor'){
			// 游客
			youkeDialogRef.value.handleShow();
			return;
		}

		if(index !=0 && AuthCode.value == 'Not-Vip'){
			// 非VIP
			popupRef.value.close();
			confirmDialogRef.value.handleShow();
			return;
		}
		$emit('change-zhang', Object.assign({}, data));
		handleClose();
	}
	function handleConfirmPay() {
		uni.redirectTo({ url: '/pages/pay/svip' })
	}
	defineExpose({
		showPopup
	})
</script>
