<template>
	<el-dialog v-model="state.dialogVisible" draggable title="退回审批" width="50%" append-to-body destroy-on-close @close="close">
		<el-form label-width="110px" ref="ruleFormRef" :model="state.ruleForm" v-loading="state.loading">
			<el-row :gutter="35">
				<el-divider content-position="left">
					<el-text tag="b" size="large"> 退回详情 </el-text>
				</el-divider>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="工单编码" class="mb5"> {{ state.orderDetail.order?.no }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="工单标题" class="mb5"> {{ state.orderDetail.order?.title }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="申请人" class="mb5"> {{ state.orderDetail.creatorName }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="申请部门" class="mb5"> {{ state.orderDetail.creatorOrgName }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="申请时间" class="mb5"> {{ formatDate(state.orderDetail?.creationTime, 'YYYY-mm-dd HH:MM:SS') }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="申请理由" class="formatted-text"> {{ state.orderDetail.content }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="退回附件">
						<annex-list name="退回附件" v-model="state.orderDetail.files" readonly classify="退回附件" businessId=""/>
					</el-form-item>
				</el-col>
				<el-divider content-position="left">
					<el-text tag="b" size="large"> 审批详情 </el-text>
				</el-divider>
				<el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
					<el-form-item label="审批结果" prop="state" :rules="[{ required: true, message: '请选择审批结果', trigger: 'change' }]">
						<el-radio-group v-model="state.ruleForm.state">
							<el-radio :value="1">审批通过</el-radio>
							<el-radio :value="2">审批拒绝</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" v-if="['ZiGong'].includes(themeConfig.appScope) && state.orderDetail.isReturnAgainShow">
					<el-form-item
						label="是否允许再退回"
						prop="isReturnAgain"
						:rules="[{ required: true, message: '请选择是否允许再退回', trigger: 'change' }]"
						label-width="120px"
					>
						<el-radio-group v-model="state.ruleForm.isReturnAgain">
							<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="24" :md="24" :lg="24" :xl="24">
					<el-form-item
						label="审批意见"
						prop="auditContent"
						:rules="[{ required: state.ruleForm.state === 2, message: '请填写审批意见', trigger: 'blur' }]"
					>
						<el-input v-model="state.ruleForm.auditContent" type="textarea" :autosize="{ minRows: 6, maxRows: 10 }" placeholder="请填写审批意见" />
					</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" name="returnAuditDialog">
import { defineAsyncComponent, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { returnAuditAdd, returnAuditDetail } from '@/api/business/return';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';
import { transformFile } from '@/utils/tools';

const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue')); // 附件列表
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		state: 1, // 审批结果
		auditContent: '', // 拒绝原因
		isReturnAgain: true, // 默认允许
	},
	orderDetail: {}, // 工单详情
	workflowId: null, // 工作流id
});
const ruleFormRef = ref<RefType>();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
/**
 * @description 打开弹窗
 * @param {object} val 工单详情
 */
const openDialog = async (val: any) => {
	state.loading = true;
	state.dialogVisible = true;
	try {
		const { result } = await returnAuditDetail(val.id);
		if (result) state.orderDetail = result;
		else state.orderDetail = val;
		state.orderDetail.files = transformFile(state.orderDetail.files);
		state.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const request = {
			...state.ruleForm,
			id: state.orderDetail.id,
		};
		returnAuditAdd(request)
			.then(() => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
