<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')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<sc-form ref="formref" :config="config" v-model="info" :loading="loading"> </sc-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
				<el-button @click="visible=false">取消</el-button>
			</el-footer>
		</el-container>
	</el-drawer> 
</template>

<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) {
						console.log(this.info)

						var res = await api.post('submit', {info:this.info,id:this.id,token:this.token})
						if (res.code==200) {
							this.close();
							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) => {
					console.log(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>

<style>
</style>
