<template>
	<view class="container-main">
		<view class="type-tree">
			<view class="tree-item" v-for="(item,index) in treeList" :key="index"
				:class="currenIndex == index ? 'activeTree' : '' " @click="clickTree(index)">
				{{item.name}}
			</view>
		</view>
		<view class="data-rows">
			<u-sticky>
				<view class="header-search">
					<u-icon name="search" size="35" color="#9DA2AB"></u-icon>
					<u-input v-model="keyword" placeholder="请输入关键字"
						:custom-style="{marginLeft:'20rpx',fontSize:'30rpx'}" :height="40" :clearable="false" />
					<view class="search-submit" @click="searchData()">搜索</view>
				</view>
			</u-sticky>
			<scroll-view scroll-y class="scrollView" @scrolltolower="onreachBottom">
				<view class="list-item" v-for="item in list" :key="item.id" @click="getInfo(item)">
					<view class="item-data">
						<view class="data-title">{{ item.title}}</view>
						<view class="data-subtitle">{{item.department}}</view>
					</view>
					<view class="item-icon">
						<u-icon name="arrow-right" size="35" color="#DCE3EC"></u-icon>
					</view>
				</view>
				<u-loadmore :status="status" :load-text="loadText" margin-top="20" />
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		getEssayList,
		getinfo
	} from "@/api/shianliaoning/essay.js";
	export default {
		data() {
			return {
				keyword: '',
				status: 'nomore',
				loadText: {
					loading: '努力加载中',
					nomore: '已经到底了~'
				},
				list: [],
				total: 0,
				queryParams: {
					type: 2,
					pageNum: 1,
					pageSize: 10,
				},
				currenIndex: null,
				treeList: [{
						name: '连锁经营政策指南',
						litiletype: 8
					},
					{
						name: '中央厨房政策指南',
						litiletype: 9
					},
					{
						name: '企业变更政策指南',
						litiletype: 10
					},
					{
						name: '企业扶持政策指南',
						litiletype: 11
					},
					{
						name: '知识产权申请指南',
						litiletype: 12
					},
					{
						name: '食品安全责任险申请指南',
						litiletype: 13
					},
					{
						name: '生产经营许可申请指南',
						litiletype: 14
					}
				],

			};
		},
		onLoad() {
			//初始化
			this.clickTree(0)
		},
		methods: {
			//详情
			getInfo(item) {
				uni.navigateTo({
					url: `/sub-public/oldman-teach/info?id=${item.id}`,
				});
			},
			//关键字搜索
			searchData() {
				this.reset()
				this.queryParams.litiletype = this.treeList[this.currenIndex].litiletype
				this.queryParams.title = this.keyword
				this.getList()
			},

			// 触底加载
			onreachBottom() {

				if (this.queryParams.pageNum < this.total) {
					if (this.status == 'loading') return
					this.queryParams.pageNum = this.queryParams.pageNum + 1;
					this.getList()
				}
			},
			// 左侧分类
			clickTree(index) {
				if (this.currenIndex == index) return
				this.keyword = ''
				this.reset()
				this.queryParams.litiletype = this.treeList[index].litiletype
				this.getList()
				this.currenIndex = index
			},

			// 获取城市/县区列表
			getList() {
				this.status = 'loading'
				getEssayList(this.queryParams).then((res) => {
					this.filerList(res)
				});
			},
			filerList(res) {
				this.list = [...this.list, ...res.rows];
				this.total = Math.ceil(res.total / this.queryParams.pageSize) || 1;
				console.log(this.total, '可以多少页')
				this.status = 'nomore'
			},
			// 重置
			reset() {
				this.list = []
				this.total = 0
				this.queryParams = {
					type: 2,
					pageNum: 1,
					pageSize: 10,
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	page {
		height: 100%;
	}

	.container-main {
		height: 100%;
		display: flex;
		align-items: center;

		&>view {
			height: 100%;
		}

		.type-tree {
			background: #F5F7FA;

			.tree-item {
				width: 180rpx;
				box-sizing: border-box;
				padding: 20rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #34373B;
			}

			.activeTree {
				background: #fff;
				color: #3976F1;
			}
		}

		.data-rows {
			box-sizing: border-box;
			padding: 20rpx;
			flex: 1;
			background: #fff;

			.header-search {
				box-sizing: border-box;
				padding: 20rpx;
				border-radius: 16rpx;
				border: 1px solid #DCE3EC;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.search-submit {
					font-size: 32rpx;
					font-weight: 500;
					color: #3976F1;
				}
			}

			.scrollView {
				height: 94.5%;

				.list-item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					min-height: 160rpx;
					border-bottom: 1px solid #DCE3EC;
					box-sizing: border-box;
					padding: 20rpx 0;

					.item-data {
						flex: 1;

						.data-title {
							font-size: 34rpx;
							font-weight: normal;
							color: #34373B;
						}

						.data-subtitle {
							width: 500rpx;
							margin-top: 20rpx;
							font-size: 32rpx;
							font-weight: 400;
							color: #B0B4BB;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}
					}

					.item-icon {
						width: 40rpx;
					}
				}
			}
		}

	}
</style>