<template>
	<el-dialog v-model="state.dialogVisible" draggable title="特提审批" width="50%" append-to-body destroy-on-close @close="close">
		<div class="collapse-container" v-loading="state.loading">
			<el-form label-width="110px" ref="ruleFormRef" :model="state.ruleForm">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="工单编码" class="mb5"> {{ state.detail?.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.detail?.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.detail?.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.detail?.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.detail?.creationTime, 'YYYY-mm-dd HH:MM:SS') }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
						<el-form-item label="特提原因" class="mb5">
							{{ state.detail?.cause }}
						</el-form-item>
					</el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
            <el-form-item label="申请特提节点" class="mb5">
              {{ state.detail?.stepName  }}
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
            <el-form-item label="审批通过后节点" class="mb5">
              {{ state.detail?.nextStepName  }}
            </el-form-item>
          </el-col>
					<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
						<el-form-item label="审批通过后办理对象" class="mb5" label-width="140px">
							{{ state.detail?.nextHandlers?.map((item: any) => item.value).join(',')  }}
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="特提理由" class="formatted-text mb5">
							{{ state.detail?.reason }}
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="附件" class="mb5">
							<annex-list name="特提附件" readonly classify="特提申请" v-model="state.detail.files" />
						</el-form-item>
					</el-col>
					<el-divider />
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :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="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="审批意见" prop="opinion" :rules="[{ required: true, message: '请填写特提审批意见', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.opinion" type="textarea" :autosize="{ minRows: 6, maxRows: 10 }" placeholder="请填写特提审批意见" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onAudit(ruleFormRef)" :loading="state.loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { defineAsyncComponent, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { specialApplyBase, specialApprove, specialDetail } from '@/api/business/special';
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: {
		nextStepCode: null, // 特提节点
		nextHandlers: [], // 处理对象
		timeLimitUnit: 2, // 期满时间单位
		orgId: null, // 部门id
		orgName: null, // 部门名称
		alterTime: false, // 是否修改期满时间
		opinion: '', // 审批意见
		isPass: true, // 是否通过
	},
	detail: {}, // 详情信息
	workflowId: null, // 工作流id
});
const ruleFormRef = ref<RefType>();
const steps = ref<EmptyArrayType>([]); // 特提节点
const stepsItems = ref<EmptyArrayType>([]); // 处理对象
const specialTimeType = ref<EmptyArrayType>([]); // 期满时间单位
// 打开弹窗
const openDialog = async (val: any) => {
	state.loading = true;
	state.dialogVisible = true;
	state.workflowId = val.workflowId;
	try {
		const [baseRes, detailRes] = await Promise.all([specialApplyBase(val.workflowId), specialDetail(val.id)]);
		steps.value = baseRes.result?.step?.steps ?? [];
		specialTimeType.value = baseRes.result.specialTimeType ?? [];
		state.detail = detailRes.result;
		state.detail.files = transformFile(detailRes.result.files);
		state.ruleForm.nextStepCode = detailRes.result.nextStepCode;
		state.ruleForm.nextStepName = detailRes.result.nextStepName;
		selectTrace(detailRes.result.nextStepCode);
		state.ruleForm.nextHandlers = detailRes.result.nextHandlers;
    state.ruleForm.alterTime = detailRes.result.alterTime;
    state.ruleForm.timeLimit = detailRes.result.timeLimit;
    state.ruleForm.timeLimitUnit = detailRes.result.timeLimitUnit;
    state.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
	steps.value = [];
	stepsItems.value = [];
};

// 选择节点确定处理对象
const selectTrace = (val: any) => {
	const step = steps.value.find((item) => item.key === val);
	state.ruleForm.nextStepName = step?.value;
	ruleFormRef.value?.resetFields('nextHandlers');
	stepsItems.value = step?.items ?? [];
	stepsItems.value = stepsItems.value.map((item: any) => {
		return {
			value: {
				...item,
			},
			label: item.value,
		};
	});
	state.ruleForm.flowDirection = step?.flowDirection;
};
// 审批
const onAudit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		let request: {};
		if (state.ruleForm.isPass) {
			request = {
				id: state.detail.id,
				orderId: state.detail.orderId,
				opinion: state.ruleForm.opinion,
				workflowId: state.workflowId,
				state: 1, // 审核结果 0 待审核 1 审核通过 2 审核不通过
			};
		} else {
			request = {
				id: state.detail.id,
				orderId: state.detail.orderId,
				opinion: state.ruleForm.opinion,
				workflowId: state.workflowId,
				state: 2, // 审核结果 0 待审核 1 审核通过 2 审核不通过
			};
		}
		specialApprove(request)
			.then(() => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
