<template>
	<el-dialog v-model="state.dialogVisible" width="70%" draggable title="编辑随手拍信息配置" @close="close">
		<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef" :disabled="loading" scroll-to-error>
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="类型" prop="bulletinObj" :rules="[{ required: true, message: '请选择类型', trigger: 'change' }]">
						<el-select
							v-model="state.ruleForm.bulletinObj"
							placeholder="请选择类型"
							value-key="dicDataValue"
							class="w100"
							@change="
								(e) => {
									state.ruleForm.bulletinTypeId = e.dicDataValue;
									state.ruleForm.bulletinTypeName = e.dicDataName;
								}
							"
						>
							<el-option v-for="item in bulletinTypeOptions" :value="item" :key="item.dicDataValue" :label="item.dicDataName" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="来源" prop="sourceMode" :rules="[{ required: true, message: '请选择来源', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.sourceMode" placeholder="请选择来源" value-key="dicDataValue" class="w100">
							<el-option v-for="item in bulletinSource" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<el-form-item label="标题" prop="title" :rules="[{ required: true, message: '请填写标题', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.title" placeholder="请填写标题" clearable show-word-limit maxlength="200"></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<el-form-item label="文档编码" prop="no" :rules="[{ required: false, message: '请填写文档编码', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.no" placeholder="请填写文档编码" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<el-form-item label="时间" prop="bulletinTime" :rules="[{ required: false, message: '请选择时间', trigger: 'change' }]">
						<el-date-picker
							v-model="state.ruleForm.bulletinTime"
							type="datetime"
							placeholder="请选择时间"
							value-format="YYYY-MM-DD[T]HH:mm:ss"
							class="w100"
							:disabled-date="disabledDate"
							popper-class="no-atTheMoment"
						/>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<el-form-item label="所属部门" prop="departmentId" :rules="[{ required: true, message: '请选择所属部门', trigger: 'change' }]">
						<el-cascader
							:options="orgsOptions"
							filterable
							:props="{ value: 'id', label: 'name', emitPath: false, checkStrictly: true }"
							placeholder="请选择所属部门"
							class="w100"
							v-model="state.ruleForm.departmentId"
							ref="orgRef"
							@change="changeOrg"
							clearable
						>
						</el-cascader>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<el-form-item label="来源部门" prop="sourceOrgName" :rules="[{ required: false, message: '请填写来源部门', trigger: 'change' }]">
						<el-input v-model="state.ruleForm.sourceOrgName" placeholder="请填写来源部门" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col>
					<el-form-item label="公告标识">
						<el-checkbox v-model="state.ruleForm.isOpen">是否公开</el-checkbox>
						<el-checkbox v-model="state.ruleForm.isBold">是否加粗</el-checkbox>
						<el-checkbox v-model="state.ruleForm.isOpenWebsite">网站公开</el-checkbox>
						<el-checkbox v-model="state.ruleForm.isWeibo">微博公开</el-checkbox>
						<el-checkbox v-model="state.ruleForm.isWeChat">微信公开</el-checkbox>
						<el-checkbox v-model="state.ruleForm.isTop">是否置顶</el-checkbox>
						<el-checkbox v-model="state.ruleForm.isPopup">是否飘窗</el-checkbox>
					</el-form-item>
				</el-col>
				<el-col>
					<el-form-item label="视频地址" prop="videoPath" :rules="[{ required: false, message: '请填写视频地址', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.videoPath" placeholder="请填写视频地址" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="内容" prop="content" :rules="[{ required: true, message: '请填写内容', trigger: 'blur' }]">
						<editor v-model:get-html="state.ruleForm.content" placeholder="请填写内容" height="450px" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx" name="noticeDetail">
import { reactive, ref, defineAsyncComponent } from 'vue';
import { throttle } from '@/utils/tools';
import { ElMessage, FormInstance } from 'element-plus';
import { disabledDate } from '@/utils/constants';
import { ElCheckbox } from 'element-plus';
import { editSnapshotBulletin, editSnapshotBulletinBase, getSnapshotBulletinDetail } from '@/api/snapshot/info';
// 引入组件
const Editor = defineAsyncComponent(() => import('@/components/Editor/index.vue')); // 富文本编辑器
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		bulletinTypeId: null, // 公告类型
		bulletinTypeName: null, // 公告类型名称
		title: null, // 标题
		no: null, // 文档编码
		bulletinTime: null, // 时间
		departmentId: null, // 所属部门ID
		departmentName: null, // 所属部门名称
		sourceOrgName: null, // 来源单位名称
		isOpen: false, // 公开
		isBold: false, // 加粗
		isOpenWebsite: false, // 网站公开
		isWeibo: false, // 微博公开
		isWeChat: false, // 微信公开
		isTop: false, // 置顶
		isPopup: false, // 弹窗
		content: null, // 内容
		videoPath: null, // 视频地址
		sourceMode: '1', // 来源方式 默认自建
	},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();

const bulletinTypeOptions = ref<EmptyArrayType>([]); // 公告类型
const orgsOptions = ref<EmptyArrayType>([]); // 来源单位
const bulletinSource = ref<EmptyArrayType>([]); // 来源
const openDialog = async (id: string) => {
	loading.value = true;
	state.dialogVisible = true;
	try {
		const [responseAnnounce, result] = await Promise.all([editSnapshotBulletinBase(), getSnapshotBulletinDetail(id)]);
		bulletinTypeOptions.value = responseAnnounce.result?.bulletinType ?? [];
		orgsOptions.value = responseAnnounce.result?.orgsOptions ?? [];
		bulletinSource.value = responseAnnounce.result?.bulletinSource ?? [];

		state.ruleForm = result.result ?? {};
		state.ruleForm.bulletinObj = {
			dicDataValue: result.result?.bulletinTypeId,
			dicDataName: result.result?.bulletinTypeName,
		};
		loading.value = false;
	} catch (error) {
		console.log(error);
		loading.value = false;
	}
};
const orgRef = ref<RefType>(); // 所属部门
const changeOrg = () => {
	const currentNode = orgRef.value.getCheckedNodes();
	if (currentNode) state.ruleForm.departmentName = currentNode[0].label;
	else state.ruleForm.departmentName = null;
};

// 保存
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		const request = {
			...state.ruleForm,
		};
		editSnapshotBulletin(request)
			.then(() => {
				loading.value = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('操作成功');
			})
			.catch(() => {
				loading.value = false;
			});
	});
}, 300);
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
