<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" errorType="toast" 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]"
				: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]"/>
		</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:['1'],disabled:true},
				supplier:{label:"供应商",btnIcon:"plant-sousuo", btnFun:()=>{
					uni.navigateTo({
						url:`/pages/purchase/search?data=${this.formData.supplier}`,
						events:{update:e=>this.formData.supplier=e||this.formData.supplier}
					})
				}},
				detailedType:{label:"详情类型",type:"select",columns:[],value:[]},
				remarks:{label:"备注",type:"textarea"},
			},
			formData:{
				goodsName:"",
				massUnits:"",
				goodsType:"1",
				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("/common/api/dict/queryDictItemByDictCode",{
				method:"post",
				params:{dictCode:'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("/common/api/dict/queryDictItemByDictCode",{
				method:"post",
				params:{dictCode:'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("/common/api/dict/categoryValue",{
				method:"post",
				body:{code:'B07'}
			})
			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>