<template>
	<view class="add">
		<!-- 位置信息 -->
		<view class="main">
			<u--form labelPosition="left" labelAlign="center" :model="form" ref="form1">
				<u-form-item label="位置名称" prop="name" labelWidth="auto" borderBottom ref="item1">
					<u--input v-model="form.name" border="none" placeholder="请输入位置名称"></u--input>
				</u-form-item>
				
				<u-form-item label="位置详情" prop="details" labelWidth="auto" borderBottom ref="item1">
					<u--textarea v-model="form.details" placeholder="请输入位置详情" confirmType="done"></u--textarea>
				</u-form-item>
			</u--form>

		</view>

		<view class="submit" v-if="form.id">
			<u-button type="primary" text="修 改 位 置" @click="submit"></u-button>
		</view>
		<view class="submit" v-else>
			<u-button type="primary" @click="submit" text="添 加 位 置"></u-button>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				form: {
					id: null,
					name: null,
					details: null
				},
				
			}
		},

		onLoad: function(options) {
			if (options.form) {
				let data = JSON.parse(options.form);
				this.form = data
			}
			
		},
		methods: {
			
			submit(){
				if(this.form.id){
					// 修改
					this.updatePosition();
				}else{
					this.addPosition();
				}
			},
			// 添加位置信息
			async addPosition() {
				let flag = true;
				if(this.form.name==null || this.form.name=='' ){
					flag = false;
				}
				if(this.form.details == null || this.form.details==''){
					flag = false;
				}
				if(!flag) return uni.showToast({
					title: '请补充位置内容',
					icon: 'error'
				})
				const {
					data: res
				} = await this.$http('/app/addPosition', this.form)
				if (res.code == 0) {
					uni.showToast({
						title: '位置添加成功',
						icon: 'success'
					});
					this.reset();
					wx.navigateTo({
						url: 'positionList',
					})
				}else{
					// 添加失败
					uni.showToast({
						title: "位置添加失败",
						icon: 'error'
					})
				}
				
			},
			// 修改位置信息
			async updatePosition() {
				let flag = true;
				if(this.form.name==null || this.form.name=='' ){
					flag = false;
				}
				if(this.form.details == null || this.form.details==''){
					flag = false;
				}
				if(!flag) return uni.showToast({
					title: '请补充位置内容',
					icon: 'error'
				})
				const {
					data: res
				} = await this.$http('/app/updatePosition', this.form)
				if (res.code == 0) {
					uni.showToast({
						title: '位置修改成功',
						icon: 'success'
					});
				}else{
					// 修改失败
					uni.showToast({
						title: "位置修改失败！",
						icon: 'error'
					})
				}
				
			},
			reset() {
				this.form = {
					id: null,
					name: null,
					details: null,
				}
			},
		}
	}
</script>

<style lang="scss">
	.submit {
		margin: 1vh 2.667vw;
	}

	.add {
		background: #f6f6f6;
		padding: 2.667vw;
		height: 95vh;

		.main {
			background: #ffffff;
			margin-bottom: 2.667vw;
			padding: 2vw;
			border-radius: 8px;

			.u-form-item__body__right {
				padding-left: 20vw;
			}

			.u-button {
				border: none !important;
				padding: 0 !important;
				justify-content: flex-start;
			}
		}
	}
</style>
