<template>
	<div class="knowledge-apply-add-container">
		<el-dialog title="发起申请" v-model="state.isShowDialog" 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="knowledgeApplyType" :rules="[{ required: true, message: '请选择申请类型', trigger: 'change' }]">
							<el-radio-group v-model="state.ruleForm.knowledgeApplyType">
								<el-radio :label="0">新增</el-radio>
								<el-radio :label="1">更新</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-if="state.ruleForm.knowledgeApplyType === 1">
						<el-form-item label="关联知识" prop="knowledgeId" :rules="[{ required: true, message: '请选择关联知识', trigger: 'change' }]">
							<el-select v-model="state.ruleForm.knowledgeId" value-key="dicDataValue" clearable filterable placeholder="请选择关联知识" class="w100">
								<el-option v-for="item in state.knowledgeOptions" :key="item.id" :label="item.title" :value="item.id" />
							</el-select>
						</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" v-if="state.ruleForm.knowledgeApplyType === 0">
						<el-form-item label="处理部门" prop="departmentId" :rules="[{ required: true, message: '请选择处理部门', trigger: 'change' }]">
							<el-cascader
								:options="state.orgData"
								filterable
								:props="{ checkStrictly: true, value: 'id', label: 'orgName', emitPath: false }"
								placeholder="请选择处理部门"
								clearable
								class="w100"
								v-model="state.ruleForm.departmentId"
								ref="cascaderRef"
								@change="getselKnowledgeList"
							>
								<template #default="{ node, data }">
									<span>{{ data.orgName }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-else>
						<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.sss[Z]"
							/>
						</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)" v-waves="'light'" :loading="state.loading">确 定 </el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

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

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

// 定义变量内容
const ruleFormRef = ref<any>();
const state = reactive<any>({
	isShowDialog: false,
	ruleForm: {
		knowledgeApplyType: 0,
	},
	orgData: [], // 上级
	loading: false,
	knowledgeOptions: [], //关联知识列表
});
// 打开弹窗
const openDialog = (orgData: any, knowledgeOptions: any) => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
	state.orgData = orgData;
	state.knowledgeOptions = knowledgeOptions;
	state.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
const cascaderRef = ref();
// 获取选择name值
const getselKnowledgeList = () => {
	let currentNode = cascaderRef.value.getCheckedNodes();
	state.ruleForm.department = currentNode[0]?.label ?? '';
};
watch(
	() => state.ruleForm.knowledgeId,
	(val) => {
		const item = state.knowledgeOptions.find((v: any) => v.id === val);
		state.ruleForm.department = item.department;
		state.ruleForm.departmentId = item.departmentId;
	}
);
// 新增
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid, fields) => {
		if (valid) {
			state.loading = true;
			KnowledgeApplyAdd(state.ruleForm)
				.then(() => {
					emit('updateList');
					closeDialog(); // 关闭弹窗
					state.loading = false;
					ElMessage.success('新增成功');
				})
				.catch(() => {
					state.loading = false;
				});
		} else {
			state.loading = false;
			console.log('error submit!', fields);
		}
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
