<template>
	<view class="ezy-shop-page">
		<!-- 返回区域 -->
		<view class="icon-title-navBar-box">
			<view class="nav-bar-icon" @click="handleBack"></view>
			<text class="nav-bar-title">商城</text>
		</view>
		<view class="ezy-page-body">
			<!-- 头部区域 -->

			<view class="ezy-xueke-tab-box">
				<!-- 动画按钮 -->
				<ezyActiveVue class="ezy-btn-active tab-item" v-for="item in data.list" :key="item.key"
					:class="{active: item.key == data.chanpinActiveSelect}" @aclick="handleSelectChanpin(item)">
					{{ item.label }}
				</ezyActiveVue>
			</view>
			<!-- 数学列表 -->
			<shuxueListVue v-if="data.chanpinActiveSelect == 'shuxue'" :list="data.shuxueList"></shuxueListVue>
			<!-- 英语列表 -->
			<yingyuListVue v-if="data.chanpinActiveSelect == 'yingyu'" :list="data.yingyuList"></yingyuListVue>
			<!-- 语文列表 -->
			<yuwenListVue v-if="data.chanpinActiveSelect == 'yuwen'" :list="data.yuwenList"></yuwenListVue>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from "vue";
	import shuxueListVue from "./components/shuxueList.vue";
	import yingyuListVue from "./components/yingyuList.vue";
	import yuwenListVue from "./components/yuwenList.vue";
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app"
	import * as shopHttp from "@/api/shop.js"
	import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
	import {
		getChanpinTongyongIndex
	} from "@/api/chanpinShuxue";
	import {
		getFullTimer
	} from "@/utils/common";

	let currentTabIndex = ref(0)
	const data = reactive({
		list: [{
				key: 'shuxue',
				label: '数学'
			},
			{
				key: 'yingyu',
				label: '英语'
			},
			{
				key: 'yuwen',
				label: '语文'
			}
		],
		chanpinActiveSelect: 'shuxue',

		shuxueList: [],
		yingyuList: [],
		yuwenList: [],
	})


	function getFormatData(data) {
		const shuxue = data.shuxue || {};
		const yingyu = data.yingyu || {};
		const yuwen = data.yuwen || {};

		return {
			shuxue: Object.entries(shuxue).map(([chanpin, value]) => {
				return {
					chanpin,
					value
				};
			}),
			yingyu: Object.entries(yingyu).map(([chanpin, value]) => {
				return {
					chanpin,
					value
				};
			}),
			yuwen: Object.entries(yuwen).map(([chanpin, value]) => {
				return {
					chanpin,
					value
				};
			}),
		}
	}

	function getPageData() {
		shopHttp.getAppShopIndex().then(res => {
			const result = getFormatData(res.data);
      // 追加排序，以产品id 小在前
			data.shuxueList = result.shuxue.sort((a,b) => a.value.chanpinId - b.value.chanpinId);
			data.yingyuList = result.yingyu.sort((a,b) => a.value.chanpinId - b.value.chanpinId);
			data.yuwenList = result.yuwen.sort((a,b) => a.value.chanpinId - b.value.chanpinId);
		})
	}

	function handleSelectChanpin(item) {
		data.chanpinActiveSelect = item.key
	}

	onShow(() => {
		currentTabIndex.value = 0;
		data.chanpinActiveSelect = 'shuxue';

		getPageData();
	})

	onLoad(() => {

	})


  function handleBack() {
    uni.navigateBack()
  }
</script>