<style lang="scss">
	.container{
		min-height:100%;
		background:#f6f6f6;
		padding:30rpx 0;
	}
</style>
<template>
	<view class="container">
		<d-navbar text="采收信息" style="--bg:#10C176;--c:#fff;" isBack>
			<button slot="right" class="cu-btn round" @click="submit">提交</button>
		</d-navbar>

		<u-form class="card inline" :model="formData" :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[k].value=$event.value.map(v=>v.value);formData[k]=$event.value[0].value"
				@input="formData[k]=$event"/>
		</u-form>
	</view>
</template>
<script>
import request from '@/common/request'
export default {
	data(){
		return{
			fields:{
				harvestNum:{label:"采收数量",suffix:'kg',type:"digit"},
				harvestBatch:{label:"收获批次号",btnText:"生成", btnFun:()=>this.formData.harvestBatch=`CS${new Date().format('yyyyMMdd')}${new Date().getTime()}`},
				principal:{label:"负责人",type:"select",columns:[],value:[null]},
				harvestTime:{label:"采收时间",type:"calendar"},
				status:{label:"采收状态",type:"select",columns:[[{label:"本次采收完后续还会采收",value:"1"},{label:"已全部采收完",value:"2"}]],value:[null]},
			},
			formData:{
				harvestNum:"",
				harvestBatch:"",
				principal:"",
				harvestTime:"",
				status:"",
				plantSchedulId:"",//种植计划id
				plotId:"",//地块id
			},
			rules:{
				harvestNum: {required:true, type:"number", message:"采收数量必填!", trigger:"blur"},
				harvestBatch:{required:true, message:"收获批次号必填!", trigger:"blur"},
				principal:{required:true, message:"负责人必填!", trigger:"blur"},
				harvestTime:{required:true, message:"采收时间必填!", trigger:"blur"},
				status:{required:true, message:"采收状态必填!", trigger:"blur"},
			},
			eventChannel:null,
		}
	},
	onLoad(options){
		this.eventChannel=this.getOpenerEventChannel()
		this.formData.plantSchedulId=options.plantId
		this.formData.plotId=options.plotId
		this.init()
	},
	methods:{
		async init(){
			var res=await request("/api/plantHarvest/getUser")
			if(res.statu){
				this.fields.principal.columns=[res.data.map(v=>({label:v,value:v}))]
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取负责人选项失败!",
					showCancel:false,
				})
			}
		},
		submit(){
			this.$refs.form.validate().then(async valid=>{
				if(valid){
					var res=await request("/api/plantHarvest/add",{
						method:"post",
						body: this.formData
					})
					if(res.statu){
						uni.navigateBack()
						this.eventChannel.emit('toRecovery')
					}else{
						uni.showModal({
							title:"提示",
							content:res.msg||"保存失败失败!",
							showCancel:false,
						})
					}
				}
			})
		},
	},
}
</script>