<template>
	<el-dialog
		v-model="state.dialogVisible"
		draggable
		title="批量审核"
		append-to-body
		@mouseup="mouseup"
		:style="'transform: ' + state.transform + ';'"
		ref="dialogRef"
		destroy-on-close
		@close="close"
	>
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
			<el-row :gutter="35">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="审批结果" prop="isPass" :rules="[{ required: true, message: '请选择审批结果', trigger: 'change' }]">
						<el-radio-group v-model="state.ruleForm.isPass">
							<el-radio :value="true">同意</el-radio>
							<el-radio :value="false">不同意</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="" prop="isSms">
						<el-checkbox v-model="state.ruleForm.isSms" label="短信通知" />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="办理意见" class="textarea" prop="opinion" :rules="[{ required: !state.ruleForm.isPass, message: '请填写办理意见', trigger: 'blur' }]">
						<common-advice
							@chooseAdvice="chooseAdvice"
							v-model="state.ruleForm.opinion"
							placeholder="请填写办理意见"
							:loading="state.loading"
							:commonEnum="commonEnum.OrderCirculation"
							:maxlength="2000"
						/>
					</el-form-item>
				</el-col>
				<!--				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
									<el-form-item label="附件" prop="additions">
										<annex-list name="办理附件" businessId="" classify="办理上传" v-model:format="handleFiles" />
									</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="state.loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, defineAsyncComponent } from 'vue';
import { ElNotification, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { commonEnum } from '@/utils/constants';
import { knowledgeBatchAudit } from '@/api/knowledge';

// 引入组件
const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue')); // 附件列表
const CommonAdvice = defineAsyncComponent(() => import('@/components/CommonAdvice/index.vue')); // 常用意见

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		opinion: null, // 办理内容
		isPass:true,
		isSms:false,
	},
	orderDetail: {}, // 工单信息
	fileList: [], // 附件列表
	loading: false, // 提交按钮loading
	transform: 'translate(0px, 0px)', // 滚动条位置
});
const ruleFormRef = ref<RefType>();
const handleFiles = ref<EmptyArrayType>([]); // 附件列表
// 打开弹窗
const ids = ref<EmptyArrayType>([]);
const openDialog = async (val: any) => {
	ids.value = val;
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 选中常用意见
const chooseAdvice = (item: any) => {
	state.ruleForm.opinion += item.content;
};
// 设置抽屉
const dialogRef = ref<RefType>();
const mouseup = () => {
	state.transform = dialogRef.value.dialogContentRef.$el.style.transform;
};
// 提交
const onSubmit = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		let request = {
			...state.ruleForm,
			knowledgeIds:ids.value
		};
		knowledgeBatchAudit(request)
			.then((res:any) => {
				state.loading = false;
				state.dialogVisible = false;
				emit('updateList');
				ElNotification({
					dangerouslyUseHTMLString: true,
					message: `<p style="word-break: break-all;max-height: 500px;overflow: auto">${res.result}</p>`,
					type: 'info',
				})
			})
			.catch(() => {
				state.loading = false;
				state.dialogVisible = false;
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
