<template>
	<view>
		<view class="status_bar"></view>
		<view class="" style="position: relative;top: 64px;">
			<view style="position: fixed;z-index: 100;">
				<uni-nav-bar @clickLeft="clickLeft" left-icon="back" title="手动添加"></uni-nav-bar>
			</view>
			<view class="imgform">
				<u-form :model="form" ref="uForm">
					<u-form-item label="害虫名称" :required="requireds" label-width="150rpx" prop="pest_name" class="form_item">
						<u-input v-model="form.pest_name" />
					</u-form-item>
					<u-form-item label="害虫数量" :required="requireds" label-width="150rpx" prop="pest_num" class="form_item">
						<u-input v-model="form.pest_num" type="number" />
					</u-form-item>
					<u-form-item label="作物种类" label-width="150rpx" class="form_item">
						<u-input v-model="form.crop_name" />
					</u-form-item>
					<u-form-item label="虫害阶段" label-width="150rpx" class="form_item">
						<u-input v-model="form.pest_case" />
					</u-form-item>
				</u-form>
				<view class="imgform_btn">
					<button @click="ensure">确定</button>
					<button @click="cancel">取消</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				requireds: true,
				form: {
					pest_name: '',
					pest_num: "",
					crop_name: '',
					pest_case: ""
				},
				rules: {
					pest_name: [{
						required: true,
						message: '请输入害虫名称',
						trigger: ['blur', 'change'],
					}],
					pest_num: [{
						required: true,
						message: '请输入害虫数量',
						trigger: ['blur', 'change'],
					}],
				},
				id: '',
				device_id: ""

			}
		},
		methods: {
			//describe: "{"pest_num":"1","crop_name":"水稻","pest_case":"1","pest_name":"七星瓢虫"}"
			// device_id: "819"
			// img_id: "2396"
			async species(id) { //统计
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.forecast_system.equip_photo_species',
					data: {
						ret: "add_pest",
						pest: this.form.name,
						img_id: this.id,
						device_id: this.device_id,
						describe: JSON.stringify(this.form)
					}
				})
			},
			clickLeft() {
				uni.navigateBack({
					delta: 1
				})
			},
			cancel() { //取消
				this.clickLeft()
			},
			ensure() {
				if (this.form.pest_name.length == 0) {
					uni.showToast({
						title: '请输入害虫名称',
						duration: 1000,
						image: ""
					});
				} else if (this.form.pest_num.length == 0) {
					uni.showToast({
						title: '请输入害虫数量',
						duration: 1000,
						image: ""
					});
				} else {
					this.species()
					this.clickLeft()
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(option) {
			// this.species(this.id)
			this.id = option.id
			this.device_id = option.device_id
		}
	}
</script>

<style lang="scss">
	.imgform {
		position: absolute;
		top: 54px;
		width: 90%;
		left: 5%;
		.form_item{
			width: 90%;
			margin: 0 auto;
			border-bottom: 2rpx solid #dedede;
		}
		.imgform_btn {
			margin-top: 40rpx;
			display: flex;

			button {
				width: 300rpx;
				font-size: 28rpx;
				height: 60rpx;
				line-height: 60rpx;
			}

			button:first-child {
				color: #FFFFFF;
				background-color: #14A478;
			}
		}
	}
</style>
