<style lang="scss">
	.container{
		background:#F6F6F6;
		min-height: 100%;
		padding:30rpx 0;

		.card{
			min-height:1000rpx;
		}
	}
</style>
<template>
	<view class="container">
		<u-form class="card inline" labelWidth="auto" :model="formData" :rules="rules" ref="form">
			<d-form
				v-for="(v,k) in fields"
				:key="k"
				v-bind="v"
				:field="k"
				:required="!!rules[k]"
				@input="formData[k]=$event"
				@select="fields[k].value=$event.value.map(v=>v.value);formData[k]=$event.value.slice(-1)[0].value"/>
		</u-form>

		<button class="cu-btn round bg-green shadow submit" @click="submit">保存</button>
	</view>
</template>
<script>
import request from '@/common/request'
export default {
	data(){
		return{
			fields:{
				goodsName:{label:"物品名称"},
				massUnits:{label:"物品单位",type:"select",columns:[],value:[]},
				goodsType:{label:"物品类型",type:"select",columns:[],value:[]},
				supplier:{label:"供应商"},
				detailedType:{label:"详情类型",type:"select",columns:[],value:[]},
				remarks:{label:"备注",type:"textarea"},
			},
			formData:{
				goodsName:"",
				massUnits:"",
				goodsType:"",
				supplier:"",
				detailedType:"",
				remark:"",
			},
			rules:{
				goodsName:{required:true, message:"物品名称必填！", trigger:"blur"},
				massUnits:{required:true, message:"物品单位必选！", trigger:"blur"},
				goodsType:{required:true, message:"物品类型必选！", trigger:"blur"},
				supplier:{required:true, message:"供应商必填！", trigger:"blur"},
				detailedType:{required:true, message:"详情类型必选！", trigger:"blur"},
			},
			eventChannel:null,
		}
	},
	onLoad(options){
		this.eventChannel=this.getOpenerEventChannel()
		this.init()
	},
	methods:{
		async init(){
			var res=await request("/api/goods/units",{
				method:"post",
				params:{code:'mass_units'}
			})
			if(res.statu){
				this.fields.massUnits.columns=[res.data.map(v=>({label:v.text,value:v.value}))]
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取物品单位下拉选项失败！",
					showCancel:false,
				})
			}

			var res=await request("/api/goods/units",{
				method:"post",
				params:{code:'goods_type'}
			})
			if(res.statu){
				this.fields.goodsType.columns=[res.data.map(v=>({label:v.text,value:v.value}))]
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取物品类型下拉选项失败！",
					showCancel:false,
				})
			}
			
			var res=await request("/api/goods/categoryValue",{
				method:"post",
				params:{code:'B04'}
			})
			if(res.statu){
				this.fields.detailedType.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("/goods/goods/add",{
						method:"post",
						body: this.formData
					})
					if(res.statu){
						this.eventChannel.emit("update")
						uni.navigateBack()
					}else{
						uni.showModal({
							title:"提示",
							content:res.msg||"保存失败！",
							showCancel:false,
						})
					}
				}
			})
		}
	}
}
</script>