<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="1000" :close-on-press-escape="false" :close-on-click-modal="false" destroy-on-close @closed="$emit('closed')" class="xdrawer">
		<el-main style="padding:0 20px 0px 20px">
			<x-form ref="formref" :config="config" v-model="info" :loading="loading"> </x-form>
		</el-main>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit">{{ config.submitname || '保存' }}</el-button>
			<el-button @click="visible=false">取消</el-button>
		</template>
	</el-drawer> 
</template>
<style>
	.xdrawer /deep/ .el-drawer__footer {
		text-align: left;
		border-top: 1px solid var(--el-border-color-light);
		padding: 10px 18px;
	}
</style>
<script>

	import api from './api'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				loading: false,
				visible: false,
				mode: "add",
				titleMap: {
					add: '新增公寓',
					edit: '编辑公寓'
				},
				info:{},
				id:0,
				token:null,
				config:{},
			}
		},
		mounted() {

		},
		methods: {
			submit(){
				this.$refs.formref.validate( async (valid, obj) => {
					if (valid) {

						var res = await api.post('submit', {info:this.info,id:this.id,token:this.token})
						if (res.code==200) {
							this.close();
							this.$emit('success', this.info, this.mode);
							this.$message.success("操作成功")
							return ;
						}
						this.$alert(res.message, "提示", {type: 'error'})
					}else{
						this.$refs.formref.scrollToField(Object.keys(obj)[0])
						return false
					}
				})
			},
			close(){
				this.visible = false;
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			setConfig(){
				var that = this;
				api.get('config', {}).then((res) => {
					that.config = res.data;	
				});
				return this;
			},
			//表单注入数据
			setData(data){
				this.loading = true
				const params = {
					id: data.id,
					uniqid: data.uniqid,
				}
				this.id = data.id;
				setTimeout(async ()=>{
					var res = await api.get('info', params)
					if (res.code==200) {
						this.loading = false
						this.info = res.data.info;
						this.token = res.data.token;
						return ;
					}
					this.$message.warning(res.message)
					this.visible = false;
				},400)

			},
			
		}
	}
</script>