<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" errorType="toast" :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.map(v=>v.value);formData[k]=fields[k].value[0]"
				@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:[]},
				harvestTime:{label:"采收时间",type:"calendar"},
				status:{label:"采收状态",type:"select",columns:[],value:[]},
				warehouseId:{label:"仓库",type:"select",columns:[],value:[]},
			},
			formData:{
				harvestNum:"",
				harvestBatch:"",
				principal:"",
				harvestTime:"",
				status:"",
				warehouseId:"",
				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"},
				warehouseId:{required:true, message:"仓库必选！"},
			},
			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,
				})
			}
			//仓库 下拉选
			var res=await request("/api/plantHarvest/getWarehouse",{
				params:{
					plantBaseId:this.$store.state.baseInfo.id,
					type:2,//1 农资，2 农产品
				}
			})
			if(res.statu){
				this.fields.warehouseId.columns=[res.data.map(v=>({label:v.warehouseName,value:v.id}))]
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取仓库选项失败！",
					showCancel:false,
				})
			}
			// 采收状态
			var res=await request("/common/api/dict/queryDictItemByDictCode",{
				method:"post",
				params:{dictCode:"harvest_status"}
			})
			if(res.statu){
				this.fields.status.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/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>