<template>
	<div class="knowledge-upDate-container">
		<el-dialog title="发起更新申请" v-model="state.dialogVisible" draggable>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="100px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="关联知识" prop="knowledgeName" :rules="[{ required: true, message: '请选择关联知识', trigger: 'change' }]">
							{{ state.ruleForm.knowledgeName }}
						</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' }]">
							<el-input
								v-model="state.ruleForm.content"
								type="textarea"
								:autosize="{ minRows: 6, maxRows: 10 }"
								placeholder="请填写申请内容"
								clearable
								maxlength="2000"
							>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="部门名称" prop="department">
							{{ state.ruleForm.department }}
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="截止日期" prop="expiredTime" :rules="[{ required: false, message: '请选择截止日期', trigger: 'change' }]">
							<template #label>
								<div style="height: 34px; display: flex; align-items: center">
									截止日期
									<el-tooltip placement="top">
										<SvgIcon name="ele-QuestionFilled" size="18px" class="ml3" />
										<template #content> 超过处理截止日期未反馈，将影响处理部门绩效考核，请谨慎设置 </template>
									</el-tooltip>
								</div>
							</template>
							<el-date-picker
								v-model="state.ruleForm.expiredTime"
								type="date"
								placeholder="请选择截止日期"
								class="w100"
								value-format="YYYY-MM-DD[T]HH:mm:ss"
							/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" class="default-button">取 消</el-button>
					<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="state.loading">确 定 </el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="knowledgeUpdate">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { KnowledgeApplyAdd } from '/@/api/knowledge/apply';

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

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
    knowledgeName: '', //关联知识
    department: '', //部门名称
    departmentId: '', //部门id
		knowledgeApplyType: 1, //申请类型 1更新 2删除
		expiredTime: [], //截止日期
	},
	loading: false,
	knowledgeOptions: [], //关联知识列表
});
const ruleFormRef = ref<any>(); //表单ref
// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm.knowledgeApplyType = 1; //更新
	state.ruleForm.department = row.creationBMName;
	state.ruleForm.departmentId = row.creatorOrgId;
	state.ruleForm.knowledgeId = row.id; //关联知识
	state.ruleForm.knowledgeName = row.title; //关联知识
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 新增
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		KnowledgeApplyAdd(state.ruleForm)
			.then(() => {
				emit('updateList');
				closeDialog(); // 关闭弹窗
				state.loading = false;
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
