<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" :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]&&rules[k].required"
				:value="/^select$/.test(v.type)?fields[k].value:formData[k]"
				@select="fields[k].value=$event.map(v=>v.value);formData[k]=fields[k].value.slice(-1)[0]"
				@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"},
				estimateOutput:{label:"预估产量(kg)",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('yyyyMMddhhmmss')}`},
				principal:{label:"负责人",disabled:/^base_admin|^company_admin/.test(this.$store.getters.userInfo.roleCode)},
				principalId:{label:"负责人身份证"},
				productIntroduce:{label:"产品介绍",type:"textarea"},
			},
			formData:{
				varietyName:"",
				plantType:"",
				plantArea:"",
				estimateOutput:"",
				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"},
				estimateOutput: {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:false, validator(rule,val,call){
					if(!val)call();
					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()
		this.eventChannel.on("copy",e=>{
			this.formData=e
			this.fields.images.value=e.images?.join?.(',')||''
			this.fields.plantType.value=e.plantType
			this.formData.plantType=e.plantType[1]
		})
		await this.init()
		
		options.plotId && (this.formData.plotId=options.plotId)
		if(options.id){
			this.getDetail(options.id)
		}else if(/^base_admin|^company_admin/.test(this.$store.getters.userInfo.roleCode)){
			this.formData.principal=this.$store.getters.userInfo.realname
		}
	},
	methods:{
		async init(){
			var res=await request("/common/api/dict/categoryValue",{
				method:"post",
				body:{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>