<template>
	<div class="knowledge-standard-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="80px">
				<el-form-item label="标题" prop="title" :rules="[{ required: true, message: '输入标题，不超过30个字', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm.title" placeholder="输入标题，不超过30个字" clearable max-length="30"></el-input>
				</el-form-item>
				<el-form-item label="正文" prop="content" :rules="[{ required: true, message: '请输入正文内容', trigger: 'blur' }]">
					<Editor v-model:get-html="state.ruleForm.content" :disable="state.disable" placeholder="请输入正文内容" />
				</el-form-item>
				<el-form-item>
					<el-button class="default-button" @click="onPreview">预 览</el-button>
					<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="state.loading"> 发 布</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-dialog title="预览" v-model="state.isShowDialog" draggable>
			<div>
				<h3 style="text-align: center">{{ state.ruleForm.title }}</h3>
				<div v-html="state.ruleForm.content" class="mt20"></div>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.isShowDialog = false" class="default-button">关 闭</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="knowledgeStandard">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, ElMessageBox } from 'element-plus';
import { useRoute } from 'vue-router';
import { standardInfo, standardUpdate } from '/@/api/knowledge/standard';
import mittBus from '/@/utils/mitt';
import '@wangeditor/editor/dist/css/style.css'; // 引入 css

// 引入组件
const Editor = defineAsyncComponent(() => import('/@/components/Editor/index.vue'));

// 定义变量内容
const ruleFormRef = ref();
const state = reactive<any>({
	ruleForm: {
		content: '',
		title: '',
	},
	disable: false,
	loading: false,
	isShowDialog: false,
});

const route = useRoute();
// 新增
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid) => {
		if (valid) {
			ElMessageBox.confirm(`是否要发布？`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					standardUpdate(state.ruleForm).then(() => {
						ElMessage.success('发布成功');
						mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
					});
				})
				.catch(() => {});
		}
	});
};
// 预览
const onPreview = () => {
	state.isShowDialog = true;
};
// 查询规范详情
const standardInfoFn = async () => {
	try {
		state.loading = true;
		const res: any = await standardInfo();
		state.ruleForm = res.result ?? {};
		state.loading = false;
	} catch (error) {
		state.loading = false;
	}
};
onMounted(() => {
	standardInfoFn();
});
</script>

<style scoped lang="scss">

</style>
