<template>
	<view>
		<view class="filter">
			<uni-search-bar class="search" radius="30" placeholder="请输入产品名称" v-model="keyword" clearButton="auto" cancelButton="none" />
			<view :class="['sort', sort]" @click="switchSort"></view>
		</view>
		
		<ul class="tab">
			<li v-for="(tab, i) in tabs" :class="{active: curTab === tab.id}" @click="tabChange(tab)">{{ tab.name }}</li>
		</ul>
		
		<ul class="list">
			<li v-for="item in list">
				<view class="pro-name">
					<image v-if="item.miniProgramPictureAddress" class="icon" :src="item.miniProgramPictureAddress" mode="widthFix"></image>
					{{ item.productName }}
				</view>
				<view class="info">
					<view class="line">
						<text class="name">产品简介：</text>
						<view class="val ell-wrap">
							<view :class="{ell: !item.toggle}">{{ item.briefIntroduction }}</view>
							<view v-if="!item.toggle" class="toggle" @click="toggle(item)">展开</view>
						</view>
					</view>
					<view class="line">
						<text class="name">产品类型：</text>
						<text class="val">{{ tabs.find(e => e.id === item.productType).name }}</text>
					</view>
					<view class="line">
						<text class="name">市场单价：</text>
						<text class="val">{{ item.marketPrice }}元/年</text>
					</view>
					<view class="line">
						<text class="name">结算方式：</text>
						<text class="val">{{ item.settlementMethod ? '比例分成' : '结算单价' }}</text>
					</view>
					<view class="line">
						<text class="name">供应厂商：</text>
						<text class="val">{{ item.supplier }}</text>
					</view>
				</view>
			</li>
		</ul>
		<uni-load-more :status="status" />
	</view>
</template>

<script>
	import { AppletsDataProductList } from '@/apis/modules/product.js'
	import slFilter from '@/components/sl-filter/sl-filter.vue'
	export default {
		data() {
			return {
				curTab: '',
				tabs: [
					{
						name: '全部',
						id: ''
					},
					{
						name: '实训课程',
						id: 1
					},
					{
						name: '理论课程',
						id: 0
					},
					{
						name: '数据产品',
						id: 2
					}
				],
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				searchTimer: null,
				sort: '',
				keyword: '',
				list: [],
				page: 1,
				pageSize: 10
			}
		},
		components: {
			slFilter
		},
		watch: {
			keyword () {
				clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.initList()
				}, 500)
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.initList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1500)
		},
		// 上拉加载
		onReachBottom() {
			if (this.reachBottom >= 0) {
				this.reachBottom = 1
				this.status = 'loading'
				this.getList()
			}
		},
		onShow() {
			this.initList()
		},
		methods: {
			getList() {
				AppletsDataProductList({
					sort: this.sort,
					keywords: this.keyword,
					productType: this.curTab,
					pageNum: this.page,
					pageSize: this.pageSize
				}).then(({ data }) => {
					// 未加载完所有数据，并且不是筛选，则拼接list，否则直接赋值
					const list = data.records
					list.map(e => {
						e.toggle = e.briefIntroduction.length < 14 // 超过了14个字才需要显示展开按钮
					})
					this.list = this.reachBottom > 0 ? [...this.list, ...list] : list
					this.page++ // 每次获取了数据后page+1
					const noMore = this.list.length === data.total // 是否加载完所有数据
					this.status = noMore ? 'noMore' : 'more' // 加载完了则设置为noMore
					this.reachBottom = noMore ? -1 : 0 // 加载完了则设置为-1
				}).catch(e => {})
			},
			initList() {
				this.page = 1
				this.reachBottom = 0
				this.getList()
			},
			// 展开
			toggle(item) {
				item.toggle = !item.toggle
			},
			// 排序
			switchSort() {
				this.sort = this.sort === 'desc' ? 'asc' : 'desc'
				this.initList()
			},
			// tab切换
			tabChange(tab) {
				this.curTab = tab.id
				this.initList()
			}
		}
	}
</script>

<style scoped lang="scss">
.filter {
	display: flex;
	align-items: center;
	.search {
		flex: 1;
	}
	.sl-filter {
		width: 30%;
		margin-left: 10%;
	}
}
.list {
	li {
		padding: 0 24rpx;
		margin: 16rpx 24rpx;
		background-color: #fff;
		border-radius: 16rpx;
	}
	.pro-name {
		display: flex;
		align-items: center;
		padding: 18rpx 0;
		font-size: 30rpx;
		color: #333;
		border-bottom: 1px solid #E6E8ED;
		.icon {
			width: 52rpx;
			margin-right: 20rpx;
		}
	}
	.info {
		padding: 12rpx 0;
	}
	.line {
		display: flex;
		padding: 12rpx 0;
	}
	.name {
		margin-right: 10rpx;
		font-size: 28rpx;
		color: #999;
	}
	.val {
		max-width: 70%;
		font-size: 28rpx;
		color: #333;
	}
	.ell-wrap {
		display: inline-flex;
		align-items: center;
	}
	.ell {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}
	.toggle {
		margin-left: 10rpx;
		white-space: nowrap;
		font-size: 24rpx;
		color: #0e92ef;
	}
}
</style>
