<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" :close-on-click-modal="false" v-model="state.dialog.isShowDialog"
			width="500px">
			<el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="24" class="mb20">
						<el-form-item label="字典编码">
							<el-input disabled v-model="state.ruleForm.type" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="执行状态">
							<el-switch v-model="state.ruleForm.enable" inline-prompt active-text="启"
								inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="24" class="mb20">
						<el-form-item label="字典标签">
							<el-input v-model="state.ruleForm.label" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="24" class="mb20">
						<el-form-item label="字典值">
							<el-input v-model="state.ruleForm.value" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="24" class="mb20">
						<el-form-item label="字典排序">
							<el-input v-model="state.ruleForm.sort" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="DicManageDialog">
import { reactive, ref } from 'vue';
import { dicApi } from "/@/api/dic";
import { ElNotification } from "element-plus";

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dicDialogFormRef = ref();
const state = reactive<any>({
	ruleForm: {
		type: '', // 字典编码
		label: '', // 字典标签
		value: '', // 字典值
		sort: '', // 排序
		id: "",
		enable: false,
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: any) => {
	state.dialog.type = type
	if (type === 'edit') {
		state.ruleForm = Object.assign({}, row); //强类型赋值
		state.dialog.title = '修改字典';
		state.dialog.submitTxt = '修 改';
		if (row.enable == 1) {
			state.ruleForm.enable = true
		} else {
			state.ruleForm.enable = false
		}
	} else {
		state.dialog.title = '新增字典';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {} //清空表单
		state.ruleForm.type = row;
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	let params = {
		type: state.ruleForm.type,
		label: state.ruleForm.label,
		value: state.ruleForm.value,
		sort: state.ruleForm.sort,
		enable: state.ruleForm.enable == true ? '1' : '0',
	}
	if (state.dialog.type === 'add') {//新增接口
		const item = await dicApi().addItemApi({
			...params
		})
		if (item.code != 200) {
			ElNotification({
				title: "提示",
				message: item.msg,
				type: "error",
				duration: 3000,
			});
		} else {
			ElNotification({
				title: "提示",
				message: item.msg,
				type: "success",
				duration: 3000,
			});
		}
	} else { ///修改接口
		const item = await dicApi().putItemApi({
			id: state.ruleForm.id,
			...params
		})
		if (item.code != 200) {
			ElNotification({
				title: "提示",
				message: item.msg,
				type: "error",
				duration: 3000,
			});
		} else {
			ElNotification({
				title: "提示",
				message: item.msg,
				type: "success",
				duration: 3000,
			});
		}
	}
	closeDialog();
	emit('refresh');
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
