<template>
	<view class="bg-f9 warehouse-add">
		<header-bar title="仓库维护"></header-bar>
		<view class="example">
			<!-- 基础用法，不包含校验规则 -->
			<uni-forms label-position="top" :modelValue="form" ref="form" :rules="rules">
				<uni-forms-item label="仓库名称" required name="name">
					<uni-easyinput v-model="form.name" placeholder="请输入姓名" />
				</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="仓库说明" name="remarks">
					<uni-easyinput type="textarea" v-model="form.remarks" 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,
					remarks: null,
				},
				types: [{
					text: '普通仓库',
					value: '1'
				}, {
					text: '成品仓',
					value: '2'
				}, {
					text: '原料仓',
					value: '3'
				}, {
					text: '辅料仓',
					value: '4'
				}, {
					text: '配件仓',
					value: '5'
				}, {
					text: '处置仓',
					value: '6'
				}],
				rules: {
					// 对name字段进行必填验证
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入仓库名称',
						}, ]
					},
					// 对email字段进行必填验证
					type: {
						rules: [{
							required: true,
							errorMessage: '请选择仓库类型',
						}, ]
					}
				},
				submitType:'add'
			}
		},
		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'
			}
			console.log(this.form)
		},
		methods: {
			submitForm() {
				const v = this
				this.$refs.form.validate().then(res => {
					uni.showLoading({
						title: '加载中'
					});
					v.$post('/api/basics/warehouse/' + 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>
