<template>
	<view class="bg-f9 warehouse-add">
		<header-bar :title="submitType === 'add' ? '添加物料' : '修改物料'"></header-bar>
		<view class="example">
			<!-- 基础用法，不包含校验规则 -->
			<uni-forms label-position="top" :modelValue="form" ref="form" :rules="rules">
				<uni-forms-item label="物料分类" required name="classifyId">
					<uni-data-picker v-model="form.classifyId" :map="{text:'label',value:'id'}" :localdata="treeData"
						popup-title="请选择物料" @change="treeChange">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="物料类型" required name="type">
					<uni-data-select v-model="form.type" :localdata="types"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="物料编码">
					<uni-easyinput v-model="form.code" placeholder="请输入,不输入则系统自动生成" />
				</uni-forms-item>
				<uni-forms-item label="单位" required name="unit">
					<uni-easyinput v-model="form.unit" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="物料名称" required name="name">
					<uni-easyinput v-model="form.name" placeholder="请输入" /> 
				</uni-forms-item>

				<uni-forms-item label="物料说明" name="introduce">
					<uni-easyinput type="textarea" v-model="form.introduce" placeholder="请输入自我介绍" />
				</uni-forms-item>
			</uni-forms>
			<button class="submit" @click="submitForm">提交</button>
		</view>
	</view>
</template>

<script>
	import headerBar from '../../../components/header-bar/index.vue'
	import Vue from 'vue'
	export default {
		components: {
			headerBar
		},
		data() {
			return {
				form: {
					name: '',
					type: null,
					classifyId: null,
					code: null,
					unit: null,
					introduce: null,
				},
				types:[{
					text: '原料',
					value: '1'
				}, {
					text: '辅料',
					value: '2'
				}, {
					text: '配件',
					value: '3'
				}, {
					text: '包材',
					value: '4'
				}, {
					text: '其他',
					value: '5'
				}],
				rules: {
					// 对name字段进行必填验证
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入产品名称',
						}, ]
					},
					// 对email字段进行必填验证
					type: {
						rules: [{
							required: true,
							errorMessage: '请选择产品类型',
						}, ]
					},
					unit: {
						rules: [{
							required: true,
							errorMessage: '请输入产品单位',
						}, ]
					},
					classifyId: {
						rules: [{
							required: true,
							errorMessage: '请选择产品分类',
						}, ]
					},
					
					
				},
				submitType: 'add',
				treeData: [],
			}
		},
		onLoad(e) {
			if (e.id) {
				this.form.id = e.id
				this.form.remarks = e.remarks
				this.form.type = e.type
				this.form.name = e.name
				this.submitType = 'edit'
			}
			this.getTree()
		},
		methods: {
			treeChange() {},
			getTree() {
				const v = this
				v.$post('/api/basics//classify/tree', {
					type: 2
				}).then(res => {
					if (res.code === 200) {
						v.treeData = res.data
					}

				})
			},
			submitForm() {
				const v = this
				this.$refs.form.validate().then(res => {
					uni.showLoading({
						title: '加载中'
					});
					v.$post('/api/basics/material/' + this.submitType, v.form).then(res => {
						if (res.code === 200) {
							uni.showToast({
								title: this.submitType == 'add' ? '添加成功' : '修改成功',
								duration: 1000,
								icon: 'success'
							})
						}
						uni.hideLoading()
						let pages = getCurrentPages(); // 获取页面栈
						let prePage = pages[pages.length - 2]; //获取上一页
						prePage.$vm.needRefresh = true; // 需要刷新
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
		},
	}
</script>

<style lang="less">
	.warehouse-add {}
</style>
