<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		width="700"
		destroy-on-close
		:close-on-click-modal="false"
		@closed="$emit('closed')"
	>
		<el-main>
			<el-form
				ref="dialogForm"
				:model="form"
				:rules="rules"
				label-width="120px"
			>
				<el-form-item label="字段名称" prop="title">
					<el-input
						v-model="form.title"
						placeholder="请输入字段名称"
					/>
				</el-form-item>
				<el-form-item label="字段标识" prop="name">
					<el-input
						v-model="form.name"
						placeholder="请输入字段标识"
					/>
				</el-form-item>
				<el-form-item label="字段类型" prop="type">
					<sc-select
						v-model="form.type"
						placeholder="请选择字段类型"
						dic="model_field_type"
						clearable
						style="width: 240px"
						:disabled="mode == 'edit'"
					/>
				</el-form-item>
				<el-form-item label="字段长度" prop="length">
					<el-input
						type="number"
						v-model="form.length"
						placeholder="请输入字段长度"
					/>
				</el-form-item>
				<el-form-item label="字段默认值" prop="default_value">
					<el-input
						v-model="form.default_value"
						placeholder="请输入字段默认值"
					/>
				</el-form-item>
				<el-form-item label="在字段后" prop="after">
					<sc-select
						v-model="form.after"
						placeholder="请选择字段类型"
						:apiObj="$API.member.field.field"
						:params="fieldParams"
						clearable
						style="width: 240px"
					/>
				</el-form-item>
				<el-form-item label="字段描述" prop="remark">
					<el-input
						type="textarea"
						v-model="form.remark"
						placeholder="请输入字段描述"
					></el-input>
				</el-form-item>
				<el-form-item label="字段配置" prop="setting">
					<el-checkbox
						v-model="form.required"
						label="是否必填"
						:true-value="1"
						:false-value="0"
						border
					/>
					<el-checkbox
						v-model="form.search_show"
						label="是否可搜索"
						:true-value="1"
						:false-value="0"
						border
					/>
					<el-checkbox
						v-model="form.list_show"
						label="是否列表显示"
						:true-value="1"
						:false-value="0"
						border
					/>
				</el-form-item>
				<el-form-item label="表单排序" prop="sort">
					<el-input
						type="number"
						v-model="form.sort"
						placeholder="请输入排序"
					></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-switch
						v-model="form.status"
						:active-value="1"
						:inactive-value="0"
					></el-switch>
				</el-form-item>
			</el-form>
		</el-main>
		<template #footer>
			<el-button type="primary" @click="onSubmit">确认提交</el-button>
		</template>
	</el-dialog>
</template>

<script>
import scIconSelect from "@/components/scIconSelect";

export default {
	emits: ["success", "closed"],
	components: {
		scIconSelect,
	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			form: {},
			rules: {
				title: [{ required: true, message: "请输入标题" }],
				name: [
					{ required: true, message: "模型必须" },
					{
						validator: (rule, value, callback) => {
							if (!/^[a-zA-Z_]+$/.test(value)) {
								callback(new Error("模型标识只能是字母"));
							} else {
								callback();
							}
						},
					},
				],
				type: [{ required: true, message: "请选择字段类型" }],
			},
			modelList: [],
			fieldParams: { model_id: 0, name: "" },
			visible: false,
			isSaveing: false,
			cache: {},
		};
	},
	mounted() {},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		onSubmit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let res;
					if (this.form.id) {
						res = await this.$API.member.field.edit.post(this.form);
					} else {
						res = await this.$API.member.field.add.post(this.form);
					}

					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit("success", res.data);
						this.visible = false;
						this.$message.success("操作成功");
					} else {
						this.$message.error(res.message);
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			this.loading = true;
			this.fieldParams.model_id = data.model_id;
			if (this.mode == "edit") {
				this.fieldParams.name = data.name;
			}
			Object.assign(this.form, data);
			if (this.mode) {
				this.form.sort = 0;
				this.form.status = 1;
			}
			console.log(this.fieldParams);
		},
	},
};
</script>
