<template>
	<!-- 病虫害识别技术 -->
	<view>
		<view class="page-panel">
			<!-- 识别列表 -->
			<view class="media-item">
				<view class="media-hd">
					<image src="@/static/img/insect.png" mode="aspectFill"></image>
				</view>
				<view class="media-bd">
					<view class="title">虫害识别技术</view>
					<view class=" flex-1 text">
						模型和算法训练对虫害种类进行识别
					</view>
					<button class="btn" @click="startDiscern('insect')">我要识别</button>
				</view>
			</view>
			<view class="media-item">
				<view class="media-hd">
					<image src="@/static/img/plant.png" mode="aspectFill"></image>
				</view>
				<view class="media-bd">
					<view class="title">病害识别技术</view>
					<view class=" flex-1 text">
						通过模型和算法训练对病害进行识别
					</view>
					<button class="btn" @click="startDiscern('plant')">我要识别</button>
				</view>
			</view>
		</view>

		<view class="btn-box" v-if="token">
			<button class="btn" @click="openExpert">专家指导</button>
		</view>
	</view>
</template>

<script>
	import {
		handlePlantDiscern,
		handleInsectDiscern
	} from '@/api/pest.js'
	export default {
		data() {
			return {
				token: ''
			};
		},
		async onLoad(options) {
			await this.$onLaunched;
			this.token = options.token;
		},
		methods: {
			openExpert(e) {
				uni.navigateTo({
					url: `expert?token=${this.token}`
				})
			},
			/**
			 * 开始识别
			 * @param {String} type 识别类型  insect：虫害，plant：病害
			 */
			async startDiscern(type) {
				let file = await this.uploadImageSync();
				if (!file) {
					return;
				}
				let result; // 识别结果
				this.$api.loading('识别中...');
				// 识别虫害
				if (type == 'insect') {
					result = await handleInsectDiscern(file)
				}
				// 识别病害
				if (type == 'plant') {
					result = await handlePlantDiscern(file)
				}
				this.$api.hide();
				if (!result) {
					return;
				}
				this.openDiscernResult(result);
			},
			// 打开识别结果
			openDiscernResult(item) {
				uni.navigateTo({
					url: `discern-result?params=${JSON.stringify(item)}`
				})
			},
			uploadImageSync() {
				return new Promise((resolve, reject) => {
					uni.chooseImage({
						count: 1, // 默认9
						sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
						success(res) {
							resolve(res.tempFiles[0]);
						},
						fail(e) {
							resolve(null);
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.media-item {
		position: relative;
		display: flex;
		padding-top: 24rpx;
		padding-bottom: 24rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #F5F5F5;

		&:first-child {
			padding-top: 0;
		}

		&:last-child {
			padding-bottom: 0;
			border: none;
		}
	}

	.media-hd {
		overflow: hidden;
		width: 336rpx;
		height: 255rpx;
		margin-right: 32rpx;
		border-radius: 6rpx;
	}

	.media-bd {
		display: flex;
		flex: 1;
		flex-direction: column;
		align-items: flex-start;

		.title {
			margin-bottom: 5rpx;
			font-size: 32rpx;
			color: #333333;
			line-height: 45rpx;
		}

		.text {

			display: -webkit-box;
			-webkit-line-clamp: 4;
			-webkit-box-orient: vertical;
			overflow: hidden;
			text-overflow: ellipsis;
			font-size: 24rpx;
			color: #999999;
			line-height: 34rpx;
		}

		.btn {
			height: 60rpx;
			margin-top: 16rpx;
			margin-right: 0;
			font-size: 24rpx;
		}
	}

	.btn-box {
		padding: 180rpx 24rpx 100rpx;
		overflow: hidden;
	}
</style>
