<template>
<el-dialog :title="titleMap[mode]" v-model="visible" size="50%" destroy-on-close @closed="$emit('closed')">
	<el-main>
		<scForm v-model="form" :config="config" :apiObj="apiObj" @onSuccess="onSuccess"></scForm>
	</el-main>
</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data(){
		return {
			mode: 'add',
			titleMap: { add: '添加', edit: '编辑', show: '查看' },
			form: {status: 1},
			config: {
				labelWidth: '120px',
				formItems: [
					{ name: 'title', label: '客户端名称', type: 'string', options: {} },
					{ name: 'app_id', label: '客户端APPID', type: 'string', options: {}, disabled: true },
					{ name: 'secret', label: '客户端密匙', type: 'string', options: {}, disabled: true },
					{ name: 'status', label: '状态', type: 'boolean', options: {} }
				]
			},
			rules: {
				title: [{ required: true, message: '请输入模型标题' }]
			},
			apiObj: this.$API.system.client.add,
			visible: false,
			isSaveing: false
		}
	},
	mounted(){
	},
	methods:{
		open(mode){
			this.mode = mode
			this.visible = true
			this.isSaveing = false
			return this;
		},
		setData(data){
			if(this.mode == 'edit'){
				this.form.id = data.id
				this.apiObj = this.$API.system.client.edit
			}else{
				this.apiObj = this.$API.system.client.add
			}
			this.isSaveing = false
			this.config.formItems.map(item => {
				this.form[item.name] = data[item.name] ? data[item.name] : ''
			})
		},
		onSuccess(res){
			if(res.code == 1){
				this.$emit('success', res)
				this.visible = false
				this.$message.success("操作成功！")
			}else{
				this.$message.error(res.message)
			}
		},
	}
}
</script>

<style scoped>
</style>
