<template>
	<scroll-view scroll-y class="content" @scrolltolower='onBottom'>
		<topbar />
		<view class="back">
			<view class="sticky">
				<van-search class='top-search' shape="round" background="#fff" placeholder="请输入搜索关键词" @search="onSearch" @change='onSearchChange' />
				<view class="grid">
					<view class="grid-item" :class="{border:listType==0}" @click="listType=0">
						<van-icon name="/static/imgs/jx.png" size="30px" />
						<text class="text">精选</text>
					</view>
					<view class="grid-item" :class="{border:listType==item.id}" @click="listType=item.id" v-for="(item,index) in typeList"
					 :key='index'>
						<van-icon :name="item.fileUrl || '/static/imgs/wxtb.png'" size="30px" />
						<text class="text">{{item.productName}}</text>
					</view>
					<view class="grid-item" :class="{border:listType==5}" @click="moreShow=!moreShow,typeMore()">
						<van-icon v-if='moreShow' name="/static/imgs/sq.png" size="30px" />
						<van-icon v-else name="/static/imgs/gd.png" size="30px" />
						<text class="text">{{moreShow?'收起':'更多'}}</text>
					</view>
				</view>
			</view>

			<loadSke :loading='loading' :list='applist'>
				<view class='list-item' v-for="(item, index) in applist" :key="index" @click="$utils.clickJumpType(item)">
					<image class="avatar" mode="aspectFit" :src="item.appletLogoFileUrl || '/static/imgs/shmr.png'" />
					<view class="top">
						<van-icon class="icon" v-if="item.collectionStatus=='0'" @tap.stop='addSc(index)' name="star-o" color='#999999' />
						<van-icon class="icon" v-else @tap.stop='delSc(index)' name="star" color='#dfdf00' />
						<van-icon class="icon" name="ellipsis" size='20' color='#999999' @tap.stop='showShare=true' />
					</view>
					<view class="item-right">
						<view class="bottom">
							<text class="title">{{item.corporateName}}</text>
							<text class="type">{{itemType(item)}}</text>
							<view class="right" v-if="false">
								<view class="flex">
									<van-icon class="icon" name="/static/imgs/评论.png" size='15' />
									<text>54</text>
								</view>
								<view class="flex">
									<van-icon class="icon" name="/static/imgs/好评率.png" size='15' />
									<text>99%</text>
								</view>
							</view>
						</view>
						<view class="center" v-if="item.appletIntroduce">
							<text class="details">{{item.appletIntroduce || `Appid: ${item.appletAddress}`}}</text>
						</view>

					</view>
				</view>
				<loading v-if="bottomLoading" class="bottom-loading" />
				<view class="ad-box list-item">
					<ad unit-id="adunit-d8c1548cc9663765"></ad>
				</view>
				<view class="ad-box list-item">
					<ad-custom unit-id="adunit-66504f2e91ee7be4"></ad-custom>
				</view>
			</loadSke>



		</view>



		<van-share-sheet :show="showShare" :options="options" @select="onSelect" @close='shareClose' />
	</scroll-view>

</template>

<script>
	import {
		debounce
	} from '@/utils'
	export default {
		data() {
			return {
				comname: 'xueche',
				active: 0,
				tabActive: 0,
				info: [{
					"id": 12,
					"pictureName": "山泉心品",
					"fileId": "77",
					"fileUrl": "http://image.jppt.com.cn/zzjs/2021-04-09/others/1617954376809.jpg",
					"jumpUrl": "{\"appId\":\"wxb47185e82704dda8\"}",
					"jumpUrlType": "goMiniApp",
					"status": 0
				}],
				current: 0,
				mode: 'round',
				applist: [],
				typeList: {},
				listType: 0,
				loading: true,
				showShare: false,
				moreShow: false,
				options: [{
					name: '微信',
					icon: 'wechat',
					openType: 'share'
				}],
				pageNum: 1,
				pageSize: 10,
				total: 1,
				customerName: '',
				bottomLoading: false
			}
		},
		mounted() {
			this.initAppList()
		},
		watch: {
			listType(val) {
				this.customerName = ''
				this.pageNum = 1
				this.loading = true
				this.$api.applist.getTypeAppList(val, {
					pageNum: this.pageNum,
					pageSize: this.pageSize
				}).then(res => {
					this.applist = res.rows
					this.total = res.total
					this.$nextTick(() => {
						this.loading = false
					})
				})
			}
		},
		methods: {
			itemType(item) {
				if (item.jumpUrlType == 'goMiniApp') {
					return '小程序'
				}
				if (item.jumpUrlType == 'goPage') {
					return '子页面'
				}
				if (item.jumpUrlType == 'goWebView') {
					return 'WEB'
				}
			},
			onSearchChange(e) {
				this.customerName = e.detail
			},
			onSearch(e) {
				this.pageNum = 1
				this.loading = true
				this.$api.applist.getTypeAppList(this.listType, {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					customerName: e.detail
				}).then(res => {
					this.applist = res.rows
					this.total = res.total
					this.$nextTick(() => {
						this.loading = false
					})
				})
			},
			initAppList() {
				this.$api.applist.typeList({
					pid: 0
				}).then(res => {
					this.typeList = res.rows.slice(0, 8)
				})
				this.$api.applist.getTypeAppList(0, {
					pageNum: 1,
					pageSize: 10
				}).then(res => {
					this.applist = res.rows
					this.total = res.total
					this.$nextTick(() => {
						this.loading = false
					})
				})
			},
			onBottom() {
				if(this.bottomLoading) return
				if (this.total - this.pageNum * this.pageSize <= 0) {
					return
				}
				this.bottomLoading=true
				this.pageNum++
				this.$api.applist.getTypeAppList(this.listType, {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					customerName: this.customerName
				}).then(res => {
					this.applist = this.applist.concat(res.rows)
					this.$nextTick(()=>{
						this.bottomLoading=false
					})
				})
			},
			typeMore() {
				if (this.moreShow) {
					typeList({
						pid: 0
					}).then(res => {
						this.typeList = res.rows
					})
				} else {
					this.typeList = this.typeList.slice(0, 8)
				}
			},
			addSc(index) {
				this.$api.applist.addFavorites(this.applist[index].id).then(res => {
					if (res.code == 200) {
						this.$set(this.applist[index], 'collectionStatus', '1')
						this.$utils.Toast('收藏成功!');
					}
				})
			},
			delSc(index) {
				this.$api.applist.delFavorites(this.applist[index].id).then(res => {
					if (res.code == 200) {
						this.$set(this.applist[index], 'collectionStatus', '0')
						this.$utils.Toast('取消收藏成功!');
					}
				})
			},
			shareClose() {
				this.showShare = false;
			},
			onSelect(event) {
				this.$utils.Toast(event.detail.name);
				this.onClose();
			},
			getAppList() {
				this.$api.applist.featuredAppList({
					pageNum: 1,
					pageSize: 10
				}).then(res => {
					console.log(res)
					this.applist = res.rows
					this.$nextTick(() => {
						this.loading = false
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bottom-loading {
		text-align: center;
		padding: 10rpx;
	}

	.ad-box {
		margin: 40rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: center !important;
		align-items: center;
	}

	.content {
		padding-bottom: 1rpx;
		height: 100%;
	}

	.swiper-box {
		width: 100vw;
		height: 56.25vw;

		.image {
			width: 100%;
			height: 100%;
		}
	}

	.sticky {
		position: sticky;
		top: 0rpx;
		left: 0;
		background-color: #fff;
		z-index: 9;
		margin-bottom: 20rpx;

		.grid {
			display: flex;
			flex-wrap: wrap;
			justify-content: flex-start;
			overflow: auto;

			.grid-item {
				flex-shrink: 0;
				width: 20%;
				height: 130rpx;
				display: flex;
				justify-content: center;
				flex-direction: column;
				align-items: center;
				text-align: center;
				font-size: 34rpx;
				padding: 10rpx;
				box-sizing: border-box;
				position: relative;

				.text {
					font-size: .8em;
					margin-top: 5rpx;
					color: #999999;
					font-weight: 400;
				}
			}

			.border::after {
				content: '';
				position: absolute;
				bottom: 5rpx;
				width: 30rpx;
				height: 5rpx;
				background-color: red;
			}
		}
	}

	.list-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #FFFFFF;
		padding: 20rpx;
		margin: 2rpx 0rpx;
		position: relative;

		.avatar {
			flex-shrink: 0;
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			border: 1px solid #1989FA;
			margin-right: 16rpx;
		}

		.top {
			display: flex;
			justify-content: flex-end;
			position: absolute;
			top: 10rpx;
			right: 0;

			.icon {
				margin-top: -20rpx;
				padding: 20rpx;
			}
		}

		.item-right {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			// height: 200rpx;



			.center {
				align-self: flex-start;

				.details {
					width: 436rpx;
					// height: 70rpx;
					font-size: 26rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #999999;
					display: -webkit-box;
					margin-bottom: 20rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					word-wrap: break-word;
					white-space: normal !important;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
			}


			.bottom {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				margin: 20rpx 0;

				.title {
					max-width: 300rpx;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
					word-break: break-all;
					font-family: PingFang SC;
					font-weight: 400;
				}

				.type {
					margin-left: 10rpx;
					padding: 3rpx 12rpx;
					height: 30rpx;
					border: 2rpx solid #999999;
					border-radius: 20rpx;
					font-size: 20rpx;
					color: #999999;
					line-height: 20rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}

				.right {
					width: 170rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 22rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #999999;
					line-height: 35rpx;

					.flex {
						display: flex;
						justify-content: center;

						.icon {
							margin-right: 5rpx;
						}
					}
				}
			}
		}
	}
</style>
