<template>
	<view>
		<view class="filter">
			<uni-search-bar class="search" radius="30" placeholder="请输入方案名称" clearButton="auto" cancelButton="none" v-model="keyword" />
		</view>
		
		<ul class="tab">
			<li v-for="(tab, i) in classifications" :class="{active: curTab === tab.id}" @click="tabChange(tab)">{{ tab.classificationName }}</li>
		</ul>
		
		<view v-if="list.length" class="list">
			<view v-for="(item, i) in list" class="item" @click="toDetail(i + 2)">
				<view class="c-name">{{ item.title }}</view>
				<view class="content">
					<view class="info">
						<view class="line">
							<view class="name">产品：</view>
							<view class="val ell-wrap">
								<view :class="{ell: !item.toggle}" v-html="item.product"></view>
								<view v-if="item.product.length > 14" class="toggle" @click.stop="toggle(item)">{{ item.toggle ? '收起' : '展开' }}</view>
							</view>
						</view>
						<view class="line">
							<view class="name">更新日期：</view>
							<view class="val">{{ item.updateTime }}</view>
						</view>
						<view class="line">
							<view class="name">适用专业：</view>
							<view class="val">{{ item.applicableMajor }}</view>
						</view>
					</view>
					<view class="detail" @click.stop="toEmail(i)">下载</view>
				</view>
			</view>
		</view>
		<empty v-else></empty>
	</view>
</template>

<script>
	import { schemeList, queryClassificationByType } from '@/apis/modules/article.js'
	export default {
		data() {
			return {
				curTab: '',
				classifications: [
					{
						id: '',
						classificationName: '不限'
					}
				],
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				searchTimer: null,
				articleNameSort: 'desc',
				keyword: '',
				list: [],
				page: 1,
				pageSize: 10,
			}
		},
		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()
			this.getClassification()
		},
		methods: {
			getList() {
				const data = {
					keyWord: this.keyword,
					pageNum: this.page,
					pageSize: this.pageSize,
					querySource: 4,
					classificationId: this.curTab
				}
				uni.showLoading({
					title: '加载中'
				})
				schemeList(data).then(({ data }) => {
					uni.hideLoading()
					// 未加载完所有数据，并且不是筛选，则拼接list，否则直接赋值
					const list = data.records
					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 => {
					uni.hideLoading()
				})
			},
			initList() {
				this.page = 1
				this.reachBottom = 0
				this.getList()
			},
			// 获取所属分类
			getClassification() {
				queryClassificationByType(3).then(({ data }) => {
					this.classifications.push(...data)
				}).catch(e => {})
			},
			// tab切换
			tabChange(tab) {
				this.curTab = tab.id
				this.initList()
			},
			// 跳转详情
			toDetail(id) {
				this.$util.openFile(id)
			},
			// 发送邮箱
			toEmail(id) {
				this.$util.to(`../send/send?id=${id}`)
			},
			// 展开
			toggle(item) {
				item.toggle = !item.toggle
			}
		}
	}
</script>

<style scoped lang="scss">
.list {
	background-color: #fff;
	.item {
		padding: 20rpx 40rpx;
		border-bottom: 1px solid #f1f1f1;
	}
	.c-name {
		font-size: 30rpx;
		color: #333;
	}
	.line {
		display: flex;
		padding: 10rpx 0;
	}
	.content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx;
	}
	.name {
		width: 140rpx;
		margin-right: 10rpx;
		white-space: nowrap;
		font-size: 28rpx;
		color: #999;
	}
	.val {
		max-width: 88%;
		font-size: 28rpx;
		color: #333;
	}
	.ell {
		height: 38rpx;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}
	.toggle {
		margin-top: 10rpx;
		white-space: nowrap;
		font-size: 24rpx;
		color: #0e92ef;
	}
	.detail {
		margin-left: 20rpx;
		font-size: 26rpx;
		color: #1f83ff;
		white-space: nowrap;
	}
}
</style>
