<style lang="scss">
	.container{
		min-height: 100%;
		background:#f6f6f6;
		padding:30rpx 0;

		.card{
			margin-top:0;
			
		}
	}
</style>
<template>
	<view class="container">
		<u-form class="card inline" errorType="toast" :model="{...formData,images:fields.images.value}" :rules="rules" ref="form" labelWidth="auto">
			<d-form
				v-for="(v,k) in fields"
				:key="k"
				v-bind="v"
				:field="k"
				:required="!!rules[k]"
				:value="/^select$/.test(v.type)?fields[k].value:formData[k]"
				@select="fields.plantType.value=$event.value.map(v=>v.value);formData[k]=$event.value[1].value"
				@input="formData[k]=$event"
				@imgs="fields[k].value=$event"/>
		</u-form>
		<button class="cu-btn round bg-green shadow submit" @click="submit">确认提交</button>
	</view>
</template>
<script>
import request from '@/common/request'
export default {
	data(){
		return{
			fields:{
				varietyName:{label:"品种名称"},
				plantType:{label:"作物类型",type:"select",columns:[[]],value:[]},
				plantArea:{label:"种植面积(亩)",type:"digit"},
				startTime:{label:"种植开始日期",type:"calendar"},
				harvestTime:{label:"预估采收日期",type:"calendar"},
				images:{label:"产品图片",type:"upImg",num:3,value:''},
				plantStandard:{label:"种植标准",type:"radio",columns:[
					{label:"有机",value:'1'},
					{label:"绿色",value:'2'},
					{label:"无公害",value:'3'},
					{label:"普通",value:'4'},
				]},
				plantBatch:{label:"种植批次号",btnText:"生成",btnFun:()=>this.formData.plantBatch=`ZZ${new Date().format('yyyyMMdd')}${new Date().getTime()}`},
				principal:{label:"负责人"},
				principalId:{label:"负责人身份证"},
				productIntroduce:{label:"产品介绍",type:"textarea"},

			},
			formData:{
				varietyName:"",
				plantType:"",
				plantArea:"",
				startTime:"",
				harvestTime:"",
				images:[],
				plantStandard:"",
				plantBatch:"",
				principal:"",
				principalId:"",
				productIntroduce:"",
				plotId: "",  // 给我传下地块id
			},
			rules:{
				varietyName: {required:true, message:"品种名称必填!", trigger:"blur"},
				plantType: {required:true, message:"作物类型必选!", trigger:"blur"},
				plantArea: {required:true, type:"number", message:"种植面积必填!", trigger:"blur"},
				startTime: {required:true, message:"种植开始日期必选!", trigger:"blur"},
				harvestTime: {required:true, message:"预估采收日期必选!", trigger:"blur"},
				images: {required:true, message:"产品图片必选!", trigger:"blur"},
				plantStandard: {required:true, message:"种植标准必填!", trigger:"blur"},
				plantBatch: {required:true, message:"种植批次号必填!", trigger:"blur"},
				principal: {required:true, message:"负责人必填!", trigger:"blur"},
				principalId: {required:true, validator(rule,val,call){
					if(!val)call(new Error("负责人身份证必填!"));
					else if(!/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(val))call(new Error("请输入正确身份证格式!"));
					else call();
				}, trigger:"blur"},
			},
			eventChannel:null,
		}
	},
	async onLoad(options){
		this.eventChannel=this.getOpenerEventChannel()
		await this.init()
		
		options.plotId && (this.formData.plotId=options.plotId)
		options.id && this.getDetail(options.id)
	},
	methods:{
		async init(){
			var res=await request("/common/api/dict/categoryValue",{
				method:"post",
				params:{code:"B06"}
			})
			if(res.statu){
				this.fields.plantType.columns=[res.data]
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取作物类型下拉选项失败!",
					showCancel:false,
				})
			}
		},
		submit(){
			this.$refs.form.validate().then(async valid=>{
				if(valid){
					var res=await request("/api/plantPlan/add",{
						method:"post",
						body:{
							...this.formData,
							images:this.fields.images.value
						}
					})
					if(res.statu){
						this.eventChannel.emit("update")
						uni.navigateBack()
					}else{
						uni.showModal({
							title:"提示",
							content:res.msg||"保存失败!",
							showCancel:false,
						})
					}
				}
			})
		},
		async getDetail(id){
			var res=await request("/api/plantPlan/queryById",{
				params:{id}
			})
			if(res.statu){
				this.formData=res.data
				this.fields.images.value=res.data.images?.join?.(',')||''
				this.fields.plantType.value=res.data.plantType
				this.formData.plantType=res.data.plantType[1]
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取种植计划详情失败!",
					showCancel:false,
				})
			}
		},
	}
}
</script>