<template>
	<view>
		<view class="status_bar"></view>
		<view class="" style="position: relative;top: 40px;">
			<view style="position: fixed;z-index: 100;">
				<uni-nav-bar @clickLeft="clickLeft" left-icon="back" title="病虫害识别"></uni-nav-bar>
			</view>
			<view class="image_box">
				<image :src="path" mode="" class="image" @click="examine(path)"></image>
			</view>
			<view class="recognition" v-if="datasTF">
				<p class="recognition_title">{{name}}</p>
				<view :class="tishi?'recognition_img':'recognition_img_two'">
					<image :src="path2" mode="" @error="error" @click="examine(path2)"></image>
				</view>
				<button class="recognition_details" v-if="tishi" @click="examines">查看详情</button>
				<button class="recognition_details" v-if="!tishi" @click="show=!show">再拍一张</button>
			</view>
			<view class="datas" v-else>
				<p class="datas_title">{{name}}</p>
				<view v-for="(item,index) in preventionArr" :key="index" class="prevention">
					<view class="prevention_title">
						<u-icon name="play-right-fill" size="24" color="#55A92D"></u-icon>
						<p>{{regexptitle[index]}}:</p>
					</view>
					<p class="prevention_con">{{item}}</p>
				</view>
				<button class="recognition_details" v-if="tishi" @click="examines">查看图片</button>
			</view>
			<u-modal v-model="show" :mask-close-able="true" title="" :show-cancel-button="true" confirm-text="拍病害" cancel-text="拍虫害"
			 content="拍照识别病虫害" @confirm="confirm" @cancel="cancel"></u-modal>
		</view>
		<kps-image-cutter @ok="onok" @cancel="oncancle" :url="imgdata" :fixed="false" :blob="true"></kps-image-cutter>
		<view class="loading" v-if="loadTF">
			<u-loading  mode="flower" size="100" :show="true" ></u-loading>
		</view>
	</view>
</template>

<script>
	import kpsImageCutter from "@/components/ksp-image-cutter/ksp-image-cutter.vue";
	export default {
		components: {
			kpsImageCutter
		},
		data() {
			return {
				path: "",
				path2: "",
				tishi: true,
				name: "",
				prevention: "",
				datasTF: true,
				show: false,
				imgdata: "",
				preventionArr: [],
				regexptitle: [],
				loadTF:false
			}
		},
		methods: {
			clickLeft() {
				uni.switchTab({
					url: "../index/index"
				})
			},
			examines() {
				this.datasTF = !this.datasTF
			},
			confirm() {
				this.flag = 1
				var that = this
				// console.log(1)
				uni.chooseImage({
					count: 1, //默认9
					// sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera', 'album'], //从相册选择
					success: (res) => {
						that.imgdata = res.tempFilePaths[0]
						that.path = res.tempFilePaths[0]
					},
				});
			},
			cancel() {
				var that = this
				this.flag = 2
				uni.chooseImage({
					count: 1, //默认9
					// sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera', 'album'], //从相册选择
					success: (res) => {
						that.imgdata = res.tempFilePaths[0]
						that.path = res.tempFilePaths[0]
					}
				});
			},
			onok(ev) {
				this.loadTF = !this.loadTF
				if (this.flag == 2) {
					// pest.pests.insect_discern 虫害
					uni.uploadFile({
						// url: 'http://dev.hnyfwlw.com/api/api_gateway?method=base.bases.base_photo', //仅为示例，非真实的接口地址
						url: 'https://wx.hnyfwlw.com/api/api_gateway?method=pest.pests.insect_discern', //仅为示例，非真实的接口地址
						filePath: ev.path,
						name: 'img_file',
						formData: {
							'user': 'test'
						},
						success: (uploadFileRes) => {
							console.log(JSON.parse(uploadFileRes.data))
							this.loadTF = !this.loadTF
							this.optionverify(JSON.parse(uploadFileRes.data).data)
						}
					});
				} else if (this.flag == 1) {
					//pest.pests.insect_discern病害识别
					uni.uploadFile({
						// url: 'http://dev.hnyfwlw.com/api/api_gateway?method=base.bases.base_photo', //仅为示例，非真实的接口地址
						url: 'https://wx.hnyfwlw.com/api/api_gateway?method=pest.pests.plant_discern', //仅为示例，非真实的接口地址
						filePath: ev.path,
						name: 'img_file',
						formData: {
							'user': 'test'
						},
						success: (uploadFileRes) => {
							console.log(JSON.parse(uploadFileRes.data))
							this.loadTF = !this.loadTF
							this.optionverify(JSON.parse(uploadFileRes.data).data)
						}
					});
				}
				this.imgdata = "";
			},
			oncancle() {
				// url设置为空，隐藏控件
				this.imgdata = ''
			},
			error() {
				this.path2 = this.$imageURL+"/bigdata_app/image/e1cd85dc59139760f43ddbac15136f2.png"
			},
			optionverify(data) {
				console.log(data)
				if (data == null) {
					this.name = "识别失败，请换张图片"
					this.tishi = false
					this.path2 = this.$imageURL+"/bigdata_app/image/10ca93e17420371a82826073c8425c0.png"
				} else {
					if(data.img_urls==""){
						this.path2 = this.$imageURL+"/bigdata_app/image/e1cd85dc59139760f43ddbac15136f2.png"
					}else{
						console.log(data.img_urls)
						this.path2 = data.img_urls
					}
					this.name = data.name
					this.tishi = true
					this.prevention = data.prevention
					var regex2 = /\[(.+?)\]/g; // [] 中括号
					var str = this.prevention
					var arr = str.match(regex2)
					var arrindex = []
					for (var i = 0; i < arr.length; i++) {
						arrindex.push(str.indexOf(arr[i]))
					}
					for (var i = 0; i < arr.length; i++) {
						if(str.slice(arrindex[i] + arr[i].length, arrindex[i + 1]) == " null" || str.slice(arrindex[i] + arr[i].length, arrindex[i + 1])== "" ||str.slice(arrindex[i] + arr[i].length, arrindex[i + 1]) == "null"){
							this.preventionArr.push("暂无信息")
						}else{
							this.preventionArr.push(str.slice(arrindex[i] + arr[i].length, arrindex[i + 1]))
						}
					}
					
					for (var i = 0; i < arr.length; i++) {
						arr[i] = arr[i].slice(1, arr[i].length - 1)
					}
					this.regexptitle = arr
				}
			},
			examine(url) {
				var imgarr =[]
				imgarr.push(url)
				console.log(imgarr)
				uni.previewImage({
					urls: imgarr
				});
			},
		},
		onLoad(option) {
			console.log(option)
			this.path = option.path
			this.optionverify(JSON.parse(option.datas).data)
		}
	}
</script>

<style lang="scss">
	.loading{
		position: absolute;
		top: 0;
		left: 0;
		z-index: 100;
		width:100%;
		height: 100vh;
		background-color: rgba(0,0,0,0.5);
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.image_box {
		position: absolute;
		top: 44px;
		width: 100%;
		height: 400rpx;

		.image {
			width: 100%;
			height: 400rpx;
		}
	}

	.recognition {
		position: absolute;
		top: 254px;
		width: 100%;
		height: 800rpx;

		.recognition_title {
			width: 100%;
			text-align: center;
			font-size: 36rpx;
			font-weight: 700;
			margin: 60rpx 0;
		}

		.recognition_img {
			border: 2rpx dashed #06B535;
			width: 450rpx;
			height: 450rpx;
			border-radius: 50%;
			margin: 0 auto;
			text-align: center;
			line-height: 450rpx;

			image {
				margin-top: 50rpx;
				width: 340rpx;
				height: 340rpx;
			}
		}

		.recognition_img_two {
			width: 450rpx;
			height: 450rpx;
			border-radius: 50%;
			margin: 0 auto;
			text-align: center;
			line-height: 450rpx;

			image {
				margin-top: 10rpx;
				width: 340rpx;
				height: 340rpx; 
			}
		}
		.recognition_details {
			width: 400rpx;
			height: 80rpx;
			border-radius: 40rpx;
			margin: 30rpx auto;
			font-size: 30rpx;
			background-color: #67B25F;
			color: #FFFFFF;
		}
	}

	.datas {
		position: absolute;
		top: 284px;
		width: 95%;
		left: 2.5%;

		.datas_title {
			font-size: 32rpx;
			font-weight: 700;
			margin-left: 30rpx;
		}

		.recognition_details {
			width: 400rpx;
			height: 80rpx;
			border-radius: 40rpx;
			margin: 30rpx auto;
			font-size: 30rpx;
			background-color: #67B25F;
			color: #FFFFFF;
		}
	}

	.prevention {
		font-size: 28rpx;
		color: #919191;
		width: 95%;
		margin: 20rpx auto;

		.prevention_title {
			display: flex;

			p {
				margin-left: 20rpx;
				color: #000000;
				font-size: 28rpx;
				font-weight: 700;
			}
		}

		.prevention_con {
			padding-left: 6%;
		}
	}
</style>
