<template>
	<div class="system-edit-role-container">
		<el-dialog title="" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="state.formData" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="标签" prop="name">
							<el-input v-model="formData.tag" :placeholder="formData.tag" clearable />
						</el-form-item>
					</el-col>
					<!-- 富文本编辑器 -->
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="产品详情" prop="content">
							<Editor v-model="formData.content" height="400px" :placeholder="formData.content" />
						</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" :loading="loading">
						{{ formData.id === 0 ? '新 增' : '修 改' }}
					</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { ref, reactive, toRefs, nextTick } from 'vue';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
import Editor from '/@/components/editor/index.vue';
import { getscriptAdd, updatescript } from '/@/api/customerService/script';
import { Session } from '/@/utils/storage';
// console.log(Session.get('userInfo').userNickname, 'user');

// 定义类型接口
interface MenuDataTree {
	id: number;
	pid: number;
	title: string;
	children?: MenuDataTree[];
}

interface DialogRow {
	id: number;
	tag: string;
	creator: string;
	content: string;
}

// 定义事件
const emit = defineEmits<{
	(e: 'success'): void;
}>();

// 响应式数据
const state = reactive({
	loading: false,
	isShowDialog: false,
	formData: {
		id: 0,
		tag: '',
		content: '',
		creator: '',
	},
	menuData: [] as MenuDataTree[],
	menuExpand: false,
	menuNodeAll: false,
	menuCheckStrictly: false,
});

// 表单验证规则
const rules: FormRules = {
	tag: [{ required: true, message: '标签名称不能为空', trigger: 'blur' }],
	content: [{ required: true, message: '产品详情不能为空', trigger: 'blur' }],
};

// 模板引用
const formRef = ref<FormInstance>();
const menuRef = ref();

// 解构状态数据
const { loading, isShowDialog, formData, menuData, menuExpand, menuNodeAll, menuCheckStrictly } = toRefs(state);

// 菜单配置
const menuProps = {
	children: 'children',
	label: 'title',
};

/**
 * 打开对话框
 * @param row - 可选的编辑数据
 */
const openDialog = (row?: DialogRow) => {
	resetForm();
	console.log(row, 'row');
	if (row) {
		state.formData = row;
	}

	state.isShowDialog = true;
};

/**
 * 关闭对话框
 */
const closeDialog = () => {
	state.isShowDialog = false;
};

/**
 * 取消操作
 */
const onCancel = () => {
	closeDialog();
};

/**
 * 提交表单
 */
const onSubmit = async () => {
	if (!formRef.value) return;

	try {
		// 表单验证
		const valid = await formRef.value.validate();
		if (!valid) return;

		state.loading = true;

		if (state.formData.id === 0) {
			state.formData.creator = Session.get('userInfo').userNickname;

			// 新增模式
			await getscriptAdd(state.formData);
			console.log(state.formData);
			ElMessage.success('添加成功');
		} else {
			state.formData.modifier = Session.get('userInfo').userNickname;
			// 编辑模式
			await updatescript(state.formData);
			ElMessage.success('修改成功');
		}

		// 关闭对话框并刷新列表
		closeDialog();
		emit('success');
	} catch (error) {
		console.error('提交失败:', error);
		ElMessage.error('操作失败');
	} finally {
		state.loading = false;
	}
};

/**
 * 重置表单
 */
const resetForm = () => {
	state.menuCheckStrictly = false;
	state.menuExpand = false;
	state.menuNodeAll = false;
	state.formData = {
		id: 0,
		tag: '',
		content: '',
		creator: '',
	};

	// 重置表单验证状态
	nextTick(() => {
		formRef.value?.clearValidate();
	});
};

// 暴露方法给父组件使用
defineExpose({
	openDialog,
	closeDialog,
});
</script>

<style scoped lang="scss">
.system-edit-role-container {
	:deep(.el-dialog__body) {
		padding: 20px;
	}

	.mb20 {
		margin-bottom: 20px;
	}

	.dialog-footer {
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}
}

.tree-border {
	margin-top: 5px;
	border: 1px solid #e5e6e7;
	border-radius: 4px;
}

.menu-data-tree {
	border: var(--el-input-border, var(--el-border-base));
	border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
	padding: 5px;
}
</style>
