<style lang="scss">
	.container{
		min-height: 100%;
		padding:30rpx;
	}
</style>
<template>
	<view class="container">
		<d-navbar text="农产品入库" isBack style="--bg:#10C176;--c:#fff;">
			<button slot="right" class="cu-btn round" @click="submit">提交</button>
		</d-navbar>

		<u-form class="inline" errorType="toast" :model="formData" ref="form" :rules="rules" 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]"
				@input="formData[k]=$event"
				@select="fields[k].value=$event.map(v=>v.value);formData[k]=fields[k].value.slice(-1)[0]"
				@imgs="fields[k].value=$event"/>
		</u-form>
	</view>
</template>
<script>
import request from '@/common/request'
export default {
	data(){
		return{
			fields:{
				inOutType:{label:"入库类型",type:"select",columns:[],value:[]},
				varietyName:{label:"名称"},
				number:{label:"入库数量",type:"digit",suffix:"kg"},
				detailType:{label:"作物类型",type:"select",columns:[[]],value:[]},
				operateTime:{label:"入库时间",type:"calendar"},
				warehouseId:{label:"仓库",type:"select",columns:[],value:[]},
				pic:{label:"单据、凭证图片",type:"upImg",value:""},
				remark:{label:"备注",type:"textarea"},
			},
			formData:{
				enterpriseId: this.$store.getters['userInfo'].companyId,
				varietyName:"",
				inOutType:"",
				number:"",
				detailType:"",
				operateTime:"",
				pic:[],
				warehouseId:"",
				remark:"",

				substanceType:2,
			},
			rules:{
				inOutType:{required:true,message:"入库类型必选！"},
				varietyName:{required:true,message:"名称必填！"},
				number:{required:true,type:'number',message:"入库数量必填！"},
				detailType:{required:true,message:"作物类型必选！"},
				operateTime:{required:true,message:"入库时间必选"},
				warehouseId:{required:true,message:"仓库必选！"},
			},
			eventChannel:null,
		}
	},
	onLoad(){
		this.eventChannel=this.getOpenerEventChannel()
		this.init()
	},
	methods:{
		async init(){
			//入库类型
			var res=await request("/common/api/dict/queryDictItemByDictCode",{
				method:"post",
				params:{dictCode:"product_in_type"}
			})
			if(res.statu){
				this.fields.inOutType.columns=[res.data]
			}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/categoryValue",{
				method:"post",
				params:{code:"B06"}
			})
			if(res.statu){
				this.fields.detailType.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/Inventory/addInventory",{
						method:"post",
						body: {
							...this.formData,
							pic: this.fields.pic.value
						}
					})
					if(res.statu){
						uni.navigateBack()
						this.eventChannel.emit("update")
					}else{
						uni.showModal({
							title:"提示",
							content:res.msg||"保存失败！",
							showCancel:false,
						})
					}
				}
			})
		},
	},
}
</script>