<template>
	<view class="ezy-mall-page">
		<view class="icon-title-navBar-box">
			<view @click="goBack" class="nav-bar-icon"></view>
			<text class="nav-bar-title">鹅状元进阶营</text>
		</view>
		<view class="ezy-tab-border">

			<uni-segmented-control :current="dingdanData.current" :values="dingdanData.items" active-color="#3A7FE9"
				@clickItem="onChangeTab" class="ezy-tab-box" />
			<view>
				<view v-if="dingdanData.current === 0">
					<scroll-view scroll-y="true" refresher-enabled="true"
						:refresher-triggered="dingdanData.quanbu.loading" :refresher-threshold="50"
						refresher-background="transparent" @refresherrefresh="onRefresh" class="order-scroll-view">
						<!--数学-->
						<uni-list>
							<uni-list-item v-for="item in dingdanData.quanbu.list" class="mall-list-item">
								<template v-slot:body>
									<view class="mall-content-box">
										<!-- 需要换成从接口中取得 wgy -->
										<img src="/static/images/my/about-icon.png" class="mall-image"/>
										<view class="content-body-box">
											<view class="content-name">
												<view class="name-text">数学暑假进阶L1</view>
											</view>
											<view class="content-text">适用于L1和学前，介绍介绍，适用于L1和学前，介绍介绍适用于L1和学前，介绍介绍</view>
											<view class="content-row">
												<view class="content-yuanjia">原价：￥39.9</view>
												<view class="shop-car-box">
													<!-- 购物车上的对号需控制 wgy-->
													<icon class="car-change"></icon>
												</view>
											</view>
										</view>
									</view>
									<view class="mall-bottom-row">
										<!-- 组合课程显示课程包明细按钮 wgy -->
										<view class="kcb-btn" @click="productBtn">课程包明细<icon></icon></view>
										<view class="hdj-text">活动价：￥29.9</view>
									</view>
								</template>
							</uni-list-item>
							<uni-load-more :status="dingdanData.quanbu.state" @click="getMore(0)"
								:contentText="dingdanData.quanbu.contentText">
							</uni-load-more>
						</uni-list>
					</scroll-view>
				</view>
				<view v-if="dingdanData.current === 1">
					<view>英语</view>
				</view>
				<view v-if="dingdanData.current === 2">
					<view>语文</view>
				</view>
			</view>
		</view>
		<view class="footer-mall-pay-box">
			<view class="mall-left-box" @click="detailBtn">
				<!-- 需要换成从接口中取得 wgy -->
				<view class="price-icon-box">
					<text class="red-price fh-text">￥</text>
					<text class="red-price">1999.8</text>明细
					<icon :class="mxjtClass"></icon>
				</view>
				<view>购买即同意虚拟产品不支持退订</view>
			</view>
			<!-- 微信 -->
			<view class="pay-status-box" v-if="showPayWay" @click="switchPayWay"><icon class="wx-icon"></icon>微信</view>
			<!-- 支付宝 -->
			<view class="pay-status-box" v-if="!showPayWay" @click="switchPayWay"><icon class="zfb-icon"></icon>支付宝</view>
			<!-- 苹果 -->
			<view class="pay-status-box apple-status-box" v-if="false"><icon class="apple-icon"></icon>apple</view>
			<view class="pay-btn">立即支付</view>
		</view>
		
		<detail-dialog ref="mallDetailPopup" @payBtn="payBtn"></detail-dialog>
		<product-dialog ref="mallProductPopup" @payBtn="payBtn"></product-dialog>
	</view>
</template>

<script setup>
	import {reactive,ref} from "vue";
	import {getOrderList,orderAdd} from "@/api/order";
	import {onLoad} from "@dcloudio/uni-app";
	import {toast,getUserIdentity} from "@/utils/common";
	import cacheManager from '@/utils/cacheManager.js';
	import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';
	import detailDialog from './detailDialog.vue'
	import productDialog from './productDialog.vue'
	const showPayWay = ref(true)
	const showDetail = ref(false)
	const mxjtClass = ref('mxjt-sq-icon')
	function switchPayWay(){
		showPayWay.value = !showPayWay.value
	}
	const mallDetailPopup = ref(null);
	const mallProductPopup = ref(null);
	function productBtn(){
		mallProductPopup.value.detailShow();
	}
	function detailBtn(){
		showDetail.value = !showDetail.value;
		if(showDetail.value){
			mxjtClass.value = 'mxjt-zk-icon';
			mallDetailPopup.value.detailShow();
		}else{
			mxjtClass.value = 'mxjt-sq-icon';
			mallDetailPopup.value.detailCloseBtn();
		}
		
	}
	
	function payBtn(){
		
	}
	
	const dingdanData = reactive({
		items: ['数学', '英语', '语文'],
		current: 0,
		quanbu: {
			page: 0,
			list: [],
			loading: false,
			state: 'more',
			contentText: {
				contentdown: '查看更多',
				contentrefresh: '加载中',
				contentnomore: '没有更多'
			}

		},
		daiFukuan: {
			page: 0,
			list: [],
			loading: false,
			state: 'more',
			contentText: {
				contentdown: '查看更多',
				contentrefresh: '加载中',
				contentnomore: '没有更多'
			}
		},
		yiFukuan: {
			page: 0,
			list: [],
			loading: false,
			state: 'more',
			contentText: {
				contentdown: '查看更多',
				contentrefresh: '加载中',
				contentnomore: '没有更多'
			}
		},
	})


	function handlePay(item) {
		uni.redirectTo({
			url: `/pages/pay/svip?cardId=${item.cardId}&formPage=my&orderId=${item.id}`
		})
	}

	function onChangeTab(e) {
		if (dingdanData.current !== e.currentIndex) {
			dingdanData.current = e.currentIndex;
			if (dingdanData.current == 0) {
				dingdanData.quanbu.page = 0
			} else if (dingdanData.current == 1) {
				dingdanData.daiFukuan.page = 0
			} else if (dingdanData.current == 2) {
				dingdanData.yiFukuan.page = 0
			}
			refreshData(dingdanData.current);
		}
	}

	function refreshData(code) {
		const opt = {
			page: 1,
			size: 10, // 固定查询10条
			status: code
		}

		if (code == 0) {
			dingdanData.quanbu.list = [];
			// 数学
			dingdanData.quanbu.state = 'loading';
			dingdanData.quanbu.page++;
			opt.page = dingdanData.quanbu.page;
		} else if (code == 1) {
			dingdanData.daiFukuan.list = [];
			// 英语
			dingdanData.daiFukuan.state = 'loading';
			dingdanData.daiFukuan.page++;
			opt.page = dingdanData.daiFukuan.page;
		} else if (code == 2) {
			dingdanData.yiFukuan.list = [];
			// 英语
			dingdanData.yiFukuan.state = 'loading';
			dingdanData.yiFukuan.page++;
			opt.page = dingdanData.yiFukuan.page;
		}
		getOrderList(opt).then(res => {
			if (code == 0) {
				dingdanData.quanbu.list = dingdanData.quanbu.list.concat(res.data.data);
				dingdanData.quanbu.loading = false;
			} else if (code == 1) {
				dingdanData.daiFukuan.list = dingdanData.daiFukuan.list.concat(res.data.data);
				dingdanData.daiFukuan.loading = false;
			} else if (code == 2) {
				dingdanData.yiFukuan.list = dingdanData.yiFukuan.list.concat(res.data.data);
				dingdanData.yiFukuan.loading = false;
			}
			if (code == 0) {
				if (res.data.total > dingdanData.quanbu.list.length) {
					// 数学
					dingdanData.quanbu.state = 'more';
					dingdanData.quanbu.loading = false;
				} else {
					// 数学
					dingdanData.quanbu.state = 'no-more';
					dingdanData.quanbu.loading = false;
				}
			} else if (code == 1) {
				if (res.data.total > dingdanData.daiFukuan.list.length) {
					// 英语
					dingdanData.daiFukuan.state = 'more';
					dingdanData.daiFukuan.loading = false;
				} else {
					// 英语
					dingdanData.daiFukuan.state = 'no-more';
					dingdanData.daiFukuan.loading = false;
				}
			} else if (code == 2) {
				if (res.data.total > dingdanData.yiFukuan.list.length) {
					// 英语
					dingdanData.yiFukuan.state = 'more';
					dingdanData.yiFukuan.loading = false;
				} else {
					// 英语
					dingdanData.yiFukuan.state = 'no-more';
					dingdanData.yiFukuan.loading = false;
				}
			}
		}).catch(err => {
			if (code == 0) {
				// 数学
				dingdanData.quanbu.state = 'more';
				dingdanData.quanbu.loading = false;
			} else if (code == 1) {
				// 英语
				dingdanData.daiFukuan.state = 'more';
				dingdanData.daiFukuan.loading = false;
			} else if (code == 2) {
				// 英语
				dingdanData.yiFukuan.state = 'more';
				dingdanData.yiFukuan.loading = false;
			}
		})
	}

	function onRefresh() {
		if (dingdanData.current == 0) {
			dingdanData.quanbu.page = 0;
			dingdanData.quanbu.list = [];
			dingdanData.quanbu.loading = true;
		} else if (dingdanData.current == 1) {
			dingdanData.daiFukuan.page = 0;
			dingdanData.daiFukuan.list = [];
			dingdanData.daiFukuan.loading = true;
		} else if (dingdanData.current == 2) {
			dingdanData.yiFukuan.page = 0;
			dingdanData.yiFukuan.list = [];
			dingdanData.yiFukuan.loading = true;
		}
		refreshData(dingdanData.current);
	}

	function getMore(code) {
		const opt = {
			page: 1,
			size: 10, // 固定查询10条
			status: code // 前台索引加1为学科cardId
		}

		if (code == 0) {
			if (dingdanData.quanbu.state == 'no-more') {
				return;
			}
			// 数学
			dingdanData.quanbu.state = 'loading';
			dingdanData.quanbu.page++;
			opt.page = dingdanData.quanbu.page;
		} else if (code == 1) {
			if (dingdanData.daiFukuan.state == 'no-more') {
				return;
			}
			dingdanData.daiFukuan.state = 'loading';
			dingdanData.daiFukuan.page++;
			opt.page = dingdanData.daiFukuan.page;
		} else if (code == 2) {
			if (dingdanData.yiFukuan.state == 'no-more') {
				return;
			}
			dingdanData.yiFukuan.state = 'loading';
			dingdanData.yiFukuan.page++;
			opt.page = dingdanData.yiFukuan.page;
		}
		getOrderList(opt).then(res => {
			if (code == 0) {
				dingdanData.quanbu.list = dingdanData.quanbu.list.concat(res.data.data);
				dingdanData.quanbu.loading = false;
			} else if (code == 1) {
				dingdanData.daiFukuan.list = dingdanData.daiFukuan.list.concat(res.data.data);
				dingdanData.daiFukuan.loading = false;
			} else if (code == 2) {
				dingdanData.yiFukuan.list = dingdanData.yiFukuan.list.concat(res.data.data);
				dingdanData.yiFukuan.loading = false;
			}
			if (code == 0) {
				if (res.data.total > dingdanData.quanbu.list.length) {
					// 数学
					dingdanData.quanbu.state = 'more';
					dingdanData.quanbu.state_text = '加载更多';
					dingdanData.quanbu.loading = false;
				} else {
					// 数学
					dingdanData.quanbu.state = 'no-more';
					dingdanData.quanbu.state_text = '没有更多啦';
					
					dingdanData.quanbu.loading = false;
				}
			} else if (code == 1) {
				if (res.data.total > dingdanData.daiFukuan.list.length) {
					// 英语
					dingdanData.daiFukuan.state = 'more';
					dingdanData.daiFukuan.state_text = '加载更多';
					
					dingdanData.daiFukuan.loading = false;
				} else {
					// 英语
					dingdanData.daiFukuan.state = 'no-more';
					dingdanData.daiFukuan.state_text = '没有更多啦';
					dingdanData.daiFukuan.loading = false;
				}
			} else if (code == 2) {
				if (res.data.total > dingdanData.yiFukuan.list.length) {
					// 英语
					dingdanData.yiFukuan.state = 'more';
					dingdanData.yiFukuan.state_text = '加载更多';
					dingdanData.yiFukuan.loading = false;
				} else {
					// 英语
					dingdanData.yiFukuan.state = 'no-more';
					dingdanData.yiFukuan.state_text = '没有更多啦';
				
					dingdanData.yiFukuan.loading = false;
				}
			}
		}).catch(err => {
			if (code == 0) {
				// 数学
				dingdanData.quanbu.state = 'more';
				dingdanData.quanbu.state_text = '加载更多';
				dingdanData.quanbu.loading = false;
			} else if (code == 1) {
				// 英语
				dingdanData.daiFukuan.state = 'more';
				dingdanData.daiFukuan.state_text = '加载更多';
				dingdanData.daiFukuan.loading = false;
			} else if (code == 2) {
				// 英语
				dingdanData.yiFukuan.state = 'more';
				dingdanData.yiFukuan.state_text = '加载更多';
				dingdanData.yiFukuan.loading = false;
			}
		})
	}

	
	function goBack() {
		uni.redirectTo({
			url: '/pages/my/index'
		})
	}

	onLoad(() => {
		getMore(dingdanData.current);
	})
</script>

<style>

</style>