<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.value.map(v=>v.value);formData[k]=$event.value[0].value"
				@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:"仓库名称"},
					warehouseAddress:{label:"仓库位置"},
					warehouseVolume:{label:"仓库容量(㎡)", type:"digit",placeholder:"请输入仓库容量"},
					warehouseType:{label:"仓库类型",type:"select",columns:[[{label:"标签1",value:1},{label:"标签2",value:2}]],value:[]},
					contactPeople:{label:"基地管理员"},
					contactNumber:{label:"管理员联系方式"},
					warehousePic:{label:"仓库图片",type:"upImg",num:3},
				},
				formData:{
					warehouseName:"5号仓库",
					warehouseAddress:"昆明五华区",
					warehouseVolume:"32",
					warehouseType:"",
					contactPeople:"小明",
					contactNumber:"13752000000",
					warehousePic:[],
					baseId: this.$store.state.plantBaseInfoId,
				},
				rules:{
					warehouseName:{required:true, message:"仓库名称必填！", trigger:["blur","change"]},
					warehouseAddress:{required:true, message:"仓库位置必填！", trigger:["blur","change"]},
					warehouseVolume:{required:true, message:"仓库容量必填！", type:"number", 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()
			if(options.id){
				this.getDetail(options.id)
			}
		},
		methods:{
			async init(){
				var res=await request("/api/AddWarehouseApi/queryWarehouseType")
				if(res.statu){
					this.fields.warehouseType.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/WarehouseManageApi/addOrUpdate",{
							method:"post",
							body:{
								...this.formData,
								warehousePic: this.fields.warehousePic.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/WarehouseManageApi/queryById",{
					params:{id}
				})
				if(res.statu){
					this.formData=res.data
					this.fields.warehousePic.value=res.data.warehousePic
					this.formData.warehousePic=res.data.warehousePic?.split?.(",")?.filter?.(v=>v)||[]
					this.fields.warehouseType.value=[res.data.warehouseType]
				}else{
					uni.showModal({
						title:"提示",
						content:res.msg||"获取仓库详情失败！",
						showCancel:false,
					})
				}
			},
		},
	}
</script>
