<template>
<el-dialog title="添加配置" v-model="visible" size="50%" destroy-on-close @closed="$emit('closed')">
	<el-main>
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="100px">
			<el-form-item label="配置标题" prop="title">
				<el-input v-model="form.title" placeholder="请输入配置标题" clearable />
			</el-form-item>
			<el-form-item label="配置名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入配置名称" clearable />
			</el-form-item>
			<el-form-item label="所属分组" prop="group">
				<sc-select v-model="form.group" dic="setting_group" placeholder="请选择所属分组" clearable filterable style="width: 70%;" />
			</el-form-item>
			<el-form-item label="配置值" prop="values">
				<el-input v-model="form.values" placeholder="请输入配置值" clearable />
			</el-form-item>
			<el-form-item label="配置类型" prop="type">
				<el-select v-model="form.type" placeholder="请选择配置类型" clearable filterable>
					<el-option v-for="item in config_type" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="配置说明" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入配置说明" clearable />
			</el-form-item>
			<el-form-item label="配置排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="0" />
			</el-form-item>
			<el-form-item label="配置状态" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
			</el-form-item>
		</el-form>
	</el-main>
	<template #footer>
		<el-button type="primary" @click="onSubmit" :loading="isSaveing">确人提交</el-button>
		<el-button @click="visible = false">取消</el-button>
	</template>
</el-dialog>
</template>

<script>
export default{
	data(){
		return {
			visible: false,
			isSaveing: false,
			config_type: [
				{label: '字符串', value: 'string'},
				{label: '数字', value: 'number'},
				{label: '布尔', value: 'boolean'},
				{label: '数组', value: 'array'},
				{label: '日期', value: 'date'},
				{label: '日期时间', value: 'datetime'},
				{label: '时间', value: 'time'}
			],
			config_status: [
				{label: '启用', value: 1},
				{label: '禁用', value: 0}
			],
			form: {status: 1, sort: 0, remark: '', type: 'string'},
			rules: {
				title: [{required: true, message: '请输入配置标题', trigger: 'blur'}],
				name: [{required: true, message: '请输入配置名称', trigger: 'blur'}],
				group: [{required: true, message: '请选择所属分组', trigger: 'blur'}],
				values: [{required: true, message: '请输入配置值', trigger: 'blur'}],
				type: [{required: true, message: '请选择配置类型', trigger: 'blur'}]
			}
		}
	},
	mounted(){
	},
	methods: {
		//显示
		open(){
			this.visible = true;
			return this
		},
		async onSubmit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if(valid){
					this.isSaveing = true;
					let res = await this.$API.system.setting.add.post(this.form)
					if(res.code == 1){
						this.$message.success(res.message);
						this.visible = false;
						this.$emit('success')
					}else{
						this.$message.error(res.message);
					}
					this.isSaveing = false;
				}
			})
		},
		setData(data) {
			Object.assign(this.form, data)
			this.form.status = data.status || 1
		}
	}
}
</script>
