<style lang="scss" scoped>
	.container{
		background:#F6F6F6;
		padding:28rpx 0;
	}
</style>
<template>
	<view class="container">
		<u-form class="card inline" errorType="toast" :model="formData" ref="form" :rules="rules" labelWidth="auto">
			<d-form
				v-for="(v,k) in fields"
				:key="k"
				v-bind="v"
				:required="!!rules[k]"
				:field="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[0]"
				@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.js'
	export default{
		data(){
			return{
				fields:{
					warehouseName:{label:"仓库名称"},
					warehouseNo:{label:"仓库编号"},
					warehouseAddress:{label:"仓库位置",type:"textarea"},
					warehouseVolume:{label:"仓库容量(㎡)", type:"digit",placeholder:"请输入仓库容量"},
					goodsType:{label:"存储物品类型",type:"select",columns:[],value:[]},
					warehouseType:{label:"仓库类型",type:"select",columns:[],value:[]},
					// contactPeople:{label:"基地管理员", disabled:this.$store.getters['userInfo'].roleCode=="baseAdmin"},
					// contactNumber:{label:"管理员联系方式", disabled:this.$store.getters['userInfo'].roleCode=="baseAdmin"},
					warehousePic:{label:"仓库图片",type:"upImg",num:3},
				},
				formData:{
					warehouseName:"",
					warehouseNo:'',
					warehouseAddress:"",
					warehouseVolume:"",
					goodsType:'',
					warehouseType:"",
					// contactPeople:"",
					// contactNumber:"",
					warehousePic:[],
					baseId: this.$store.state.baseInfo.id,
				},
				rules:{
					warehouseName:{required:true, message:"仓库名称必填！", trigger:["blur","change"]},
					warehouseNo: {required:true, message:"种植编号必填！", trigger:"blur"},
					warehouseAddress:{required:true, message:"仓库位置必填！", trigger:["blur","change"]},
					warehouseVolume:{required:true, message:"仓库容量必填！", type:"number", trigger:["blur","change"]},
					goodsType:{required:true, message:"存储物品类型必填！", trigger:["blur","change"]},
					warehouseType:{required:true, message:"仓库类型必选！", trigger:["blur","change"]},
					// contactPeople:{required:true, message:"基地管理员必填！", trigger:["blur","change"]},
					// contactNumber:{required:true, validator(rule,val,call){
					// 	if(!val)call(new Error("管理员联系方式必填！"));
					// 	else if(!/^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(val))call(new Error("请输入正确联系方式格式！"))
					// 	else call();
					// }, trigger:["blur","change"]},
					warehousePic:{required:true, validator(rule,val,call){
						if(!this.fields.warehousePic)call(new Error("仓库图片必选！"))
						else call()
					}, trigger:["blur","change"]},
				},
				eventChannel:null,
			}
		},
		onLoad(options){
			this.eventChannel=this.getOpenerEventChannel()
			this.init()
			this.eventChannel.on("detail", e=>{
				if(e){
					this.formData=e
					this.fields.warehousePic.value=e.warehousePic.join(',')
					this.fields.warehouseType.value=[e.warehouseType]
					this.fields.goodsType.value=[e.goodsType]
				}else if(this.$store.getters['userInfo'].roleCode=="baseAdmin"){
					// this.formData.contactPeople=this.$store.getters['userInfo'].realname
					// this.formData.contactNumber=this.$store.getters['userInfo'].phone
				}
			})
		},
		methods:{
			async init(){
				//仓库类型
				var res=await request("/common/api/dict/queryDictItemByDictCode",{
					method:"post",
					params:{dictCode: "warehouse_type"}
				})
				if(res.statu){
					this.fields.warehouseType.columns=[res.data]
				}else{
					uni.showModal({
						title:"提示",
						content:res.msg||"获取仓库类型选项失败！",
						showCancel:false,
					})
				}
				//存储物品类型
				var res=await request("/common/api/dict/queryDictItemByDictCode",{
					method:"post",
					params:{dictCode: "goods_type"}
				})
				if(res.statu){
					this.fields.goodsType.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/WarehouseManageApi/addOrUpdate",{
							method:"post",
							body:{
								...this.formData,
								warehousePic: this.fields.warehousePic.value
							}
						})
						if(res.statu){
							this.eventChannel.emit("update")
							uni.navigateBack()
							//新增仓库，首页刷新
							if(!this.formData.id){
								this.$provise.emit("updateStock")
							}
						}else{
							uni.showModal({
								title:"提示",
								content:res.msg||"保存失败！",
								showCancel:false,
							})
						}
					}
				})
			},
		},
	}
</script>
