<template>
	<!-- 病虫害百科 -->
	<view>
		<ui-sticky>
			<ui-search placeholder="请输入害虫名字" @confirm="searchPestList"></ui-search>
			<ui-tabs :list="tabs" :active="params.code" @clickTab="clickPestTab"></ui-tabs>
		</ui-sticky>
		<!-- 病虫害百科列表 -->
		<view class="page-panel pest-panel" v-if="pestList.length>0">
			<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?item.img_urls:'http://106.119.196.18:10004/static/img/noimage.689b20a.png'"></image>
					<view class="row-center p-10">
						<text class="text">{{item.name}}</text>
					</view>
				</view>
			</block>
		</view>
		<view class="page-panel" v-if="pestList.length==0">
			<ui-empty></ui-empty>
		</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
				}]
			};
		},
		async onLoad() {
			await this.$onLaunched;
			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() {
				const {
					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>
