<template>
	<view class="course_box">
		<nav-logo title="选购中心"></nav-logo>
		<view class="body_course">
			<view class="top">
				<u-tabs :list="courseLists" height="100" :current="current" @change="change" :scrollable="false"
					font-size="32" active-color="#333"></u-tabs>
				<view class="search_box">
					<u-search class="u_search" :show-action="false" @clear="search"
						:placeholder="`请输入${current == 4 ? '商品':courseLists[current].name}名称`"
						v-model="formData.goodsName" @search="search"></u-search>
					<view class="btns" @click="openFilterBox">
						<text>筛选</text>
						<img src="@/static/modIcon/screen.png" alt="">
					</view>
				</view>
			</view>
			<view class="content">
				<scroll-view scroll-y="true" class="scroll-Y" @scrolltolower="lower" :scroll-top="scrollTop"
					@scroll="scroll" scroll-with-animation>
					<view class="noGoods" v-if="goodsList.length == 0">
						<img src="https://file.xyyxt.net/web/static/img/no-content.png" alt="">
						<view class="">
							暂无数据
						</view>
					</view>
					<view class="content_box" v-for="(item,index) in goodsList" :key="index" @click="tobuy(item)">
						<view class="title">
							{{item.goodsName}}
						</view>
						<view class="f_x">
							<view class="img_goods">
								<img :src="$method.splitImgHost(item.coverUrl)" alt="">
							</view>
							<view class="content_goods">
								<view class="top_bus">
									<text>{{item.educationName}}</text>
									<text>{{item.aliasName}}</text>
								</view>
								<view class="price">
									<text class="price1" v-if="!item.specTemplateId ||
					(!item.maxPrice && !item.minPrice)"><text v-if="item.standPrice"
											style="font-size: 28rpx;font-weight: 400;">￥</text><text
											v-if="item.standPrice">{{item.standPrice | formatPrice}}</text><text
											v-else>免费</text></text>
									<text class="price1" v-else><text
											style="font-size: 28rpx;font-weight: 400;">￥</text>{{item.minPrice | formatPrice}}
										<template v-if="item.minPrice != item.maxPrice">
											<text>-</text>
											<text
												style="font-size: 28rpx;font-weight: 400;">￥</text>{{ item.maxPrice | formatPrice }}
										</template></text>
									<text class="price2"
										v-if="item.linePrice">原价：￥{{item.linePrice | formatPrice}}</text>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<!-- tabbar -->
		<myTabbar :backTopBtn="backTopBtn"></myTabbar>
		<filter-show-status ref="filterShowStatus" @backFunc="backFunc"></filter-show-status>
	</view>
</template>
<script>
	import filterShowStatus from "./filterStatus.vue"
	import {
		mapGetters
	} from "vuex";
	export default {
		components: {
			filterShowStatus
		},
		data() {
			return {
				backTopBtn: false,
				scrollTop: 0,
				courseLists: [{
						name: "课程",
						type: 1
					},
					{
						name: "直播",
						type: 6
					},
					{
						name: "题库",
						type: 2
					},
					{
						name: "资料",
						type: 8
					},
					// {
					// 	name: "会员商城",
					// 	aliaName: "商品",
					// 	type: 7
					// },
				],
				goodsList: [],
				formData: {
					goodsName: "",
					pageSize: 8,
					pageNum: 1,
					goodsStatus: 1,
					showStatus: 1
				},
				total: 0,
				oldScrollTop: 0,
				activeData: {
					educationId: "",
					projectId: "",
					businessId: "",
					subjectId: "",
					sortType: 1
				},
			};
		},
		onPullDownRefresh() {
			// this.initList();
			// setTimeout(function() {
			// 	uni.stopPullDownRefresh();
			// }, 500);
		},
		onLoad(option) {
			console.log(option, '------option------')
			const activeTypeData = {
				// 一建继续教育
				'1': {
					educationId: "2",
					projectId: "2",
					businessId: "2",
					subjectId: "",
					sortType: 1
				},
				// 二建继续教育
				'2': {
					educationId: "2",
					projectId: "2",
					businessId: "3",
					subjectId: "",
					sortType: 1
				},
				// 职称继续教育 
				'3': {
					educationId: "2",
					projectId: "111",
					businessId: "242",
					subjectId: "",
					sortType: 1
				},
				// 安管人员考前培训
				'4': {
					educationId: "3",
					projectId: "24",
					businessId: "65",
					subjectId: "",
					sortType: 1
				}
			};
			// 判断是否携带筛选条件businessKey
			if (activeTypeData.hasOwnProperty(option.businessKey)) {
				this.activeData = activeTypeData[option.businessKey];
			}

			if (option.current) {
				this.$store.state.current = Number(option.current)
			}
			uni.hideTabBar();
			this.initList()
			uni.$on('backTop', res => {
				//视图会发生重新渲染
				this.scrollTop = this.oldScrollTop
				//当视图渲染结束 重新设置为0
				this.$nextTick(() => {
					this.scrollTop = 0
				});
			})
		},
		onUnload() {
			uni.$off('backTop')
		},
		watch: {
			"current"(newVal, oldVal) {
				this.formData.pageNum = 1
				this.formData.goodsName = ""
				this.activeData = {
					educationId: "",
					projectId: "",
					businessId: "",
					subjectId: "",
					sortType: 1
				}
				this.goodsList = []
				this.initList()
			}
		},
		computed: {
			...mapGetters(["current", "hideBuyState"])
		},
		methods: {
			backFunc(item) {
				console.log('asd', item)
				this.activeData = item
				this.search()
			},
			scroll(e) {
				//记录scroll  位置
				this.oldScrollTop = e.detail.scrollTop
				if (e.detail.scrollTop > 100) {
					this.backTopBtn = true
				} else {
					this.backTopBtn = false
				}
			},
			openFilterBox() {
				this.$refs.filterShowStatus.openBoxs(this.activeData)
			},
			search() {
				this.formData.pageNum = 1
				this.goodsList = []
				this.initList()
			},
			//触底
			lower() {
				if (this.goodsList.length >= this.total) {
					uni.showToast({
						title: "已经到底啦~",
						icon: "none"
					})
					return
				}
				this.formData.pageNum++
				this.initList()
			},
			change(e) {
				this.$store.state.current = e
			},
			initList() {
				if (!this.hideBuyState) {
					this.$api.goodsList({
						...this.formData,
						...this.activeData,
						educationTypeId: this.activeData.educationId,
						goodsName: this.formData.goodsName,
						goodsType: this.courseLists[this.$store.state.current].type
					}).then((res) => {
						this.goodsList = this.goodsList.concat(res.data.rows)
						this.total = res.data.total
					});
				}
			},

			tobuy(item) {
				if (item.goodsType == 2) {
					uni.navigateTo({
						url: "/pages2/bank/detail?id=" + item.goodsId,
					});
				} else {
					// 视频、直播 /pages5/liveDetail/index
					uni.navigateTo({
						url: "/pages3/course/detail?id=" +
							item.goodsId +
							"&goodsType=" +
							item.goodsType,
					});
				}
			},
		},
	};
</script>


<style scoped lang="scss">
	.noGoods {
		height: 80%;
		display: flex;
		flex-direction: column;
		align-items: center;

		&>img {
			margin-top: 50rpx;
			width: 500rpx;
			height: 500rpx;
		}

		&>view {
			font-size: 34rpx;
			margin-top: 30rpx;
			color: #666;
		}
	}

	.course_box {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: rgba(241, 244, 247, 1);

		.body_course {
			flex: 1;
			height: 1rpx;
			display: flex;
			flex-direction: column;

			.top {
				flex-shrink: 0;
				background-color: #fff;

				.search_box {
					display: flex;
					align-items: center;
					padding: 40rpx 30rpx;

					&>.u_search {
						flex: 1;
					}

					&>.btns {
						display: flex;
						align-items: center;
						margin-left: 30rpx;

						&>text {
							color: #333;
							font-size: 28rpx;
							font-weight: 600;
						}

						&>img {
							margin-left: 6rpx;
							width: 30rpx;
							height: 30rpx;
						}
					}
				}
			}

			.content {
				flex: 1;
				height: 1rpx;
				overflow: hidden;

				&>.scroll-Y {
					height: 100%;
				}
			}
		}
	}

	.content_box {
		background-color: #fff;
		border-radius: 8rpx;
		padding: 20rpx;
		margin: 20rpx;

		&>.title {
			padding-top: 10rpx;
			margin-bottom: 30rpx;
			color: #333;
			font-weight: 600;
			font-size: 28rpx;
		}

		&>.f_x {
			display: flex;

			&>.img_goods {
				width: 235rpx;
				height: 138rpx;
				overflow: hidden;
				border-radius: 8rpx;
				margin-right: 20rpx;
				flex-shrink: 0;

				&>img {
					width: 100%;
					height: 100%;
				}
			}

			&>.content_goods {
				flex: 1;
				width: 1rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				&>.top_bus {
					text-overflow: ellipsis;
					white-space: nowrap;
					overflow: hidden;

					&>text {
						background-color: #f5f7f9;
						border-radius: 4rpx;
						padding: 6rpx 10rpx;

						&:first-child {
							margin-right: 14rpx;
						}
					}
				}

				&>.price {
					display: flex;
					align-items: center;
					flex-wrap: wrap;
					justify-content: space-between;

					&>.price1 {
						font-size: 36rpx;
						font-weight: 700;
						color: red;
					}

					&>.price2 {
						text-decoration: line-through;
						color: #aaaaaa;
						font-size: 24rpx;
					}
				}
			}
		}
	}

	/deep/ .u-scroll-box {
		display: flex;
	}

	/deep/ .u-tab-bar {
		background-color: #2979ff !important;
	}

	/deep/ .u-content {
		border: 1rpx solid #2979ff !important;
	}
</style>