<template>
	<!-- 病虫害百科 -->
	<view>
		<ui-search placeholder="请输入害虫名字" @confirm="searchPestList"></ui-search>
		<ui-tabs :list="tabs" :active="params.code" @clickTab="clickPestTab"></ui-tabs>
		<!-- 病虫害百科列表 -->
		<view class="page-panel pest-panel">
			<block v-for="(item,index) in pestList" :key="index">
				<view class="pest-item" @click="openPestsInfo(item)">
					<image class="pic" mode="aspectFill" :src="item.img_urls"></image>
					<view class="row-center p-10">
						<text class="text">{{item.name}}</text>
					</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	import {
		loadPestList
	} from '@/api/pest.js'
	export default {
		data() {
			return {
				// 病虫害百科列表
				pestList: [],

				// 搜索条件列表
				params: {
					code: 2,
					page: 1,
					page_size: 10,
					pest_name: ''
				},
				total: 0, // 设备总数

				// tabs列表
				tabs: [{
					text: '虫害百科',
					value: 2
				}, {
					text: '病害百科',
					value: 1
				}]
			};
		},
		onLoad() {
			this.getPestList();
		},
		// 触底请求
		onReachBottom(e) {
			if (this.pestList.length >= this.total) {
				return;
			}
			this.params.page += 1;
			this.getPestList();
		},
		methods: {

			clickPestTab(value) {
				this.params.code = value;
				this.refreshPestList();
			},
			// 刷新虫害百科列表
			refreshPestList() {
				this.params.page = 1;
				this.pestList = [];
				this.getPestList();
			},
			// 获取虫害or病害 列表
			async getPestList() {
				let {
					disease_nums,
					pest_nums,
					data
				} = await loadPestList(this.params);
				this.pestList = [...this.pestList, ...(data ?? [])];
				this.total = this.params.code == 2 ? pest_nums : disease_nums;
			},
			/**
			 * 搜索病虫害
			 * @param {String} val 搜索内容
			 */
			searchPestList(val) {
				this.params.pest_name = val;
				this.refreshPestList();
			},
			// 打开病虫害
			openPestsInfo(item){
				uni.navigateTo({
					url:`details?params=${JSON.stringify(item)}`
				})
			}
		}
	}
</script>

<style lang="scss">
	// 病虫害百科面板
	.pest-panel {
		display: flex;
		flex-wrap: wrap;
		padding: 24rpx;
	}

	// 病虫害百科列表项
	.pest-item {
		width: 336rpx;

		margin-right: 28rpx;
		margin-bottom: 24rpx;

		border-radius: 12rpx;
		border: 1rpx solid #F1F1F1;

		&:nth-child(2n) {
			margin-right: 0;
		}

		.pic {
			display: block;
			width: 336rpx;
			height: 255rpx;

			border-radius: 4rpx;
		}

		.text {
			font-size: 20rpx;
			color: #666666;
		}

		.tips {
			width: 24rpx;
			height: 24rpx;
			background: #07F546;
			border-radius: 100%;
		}

	}
</style>
