<template>
	<el-dialog v-model="state.dialogVisible" draggable title="二次办理审批" width="40%" 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="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="附件" prop="handleResult" class="mb5" :rules="[{ required: false, message: '请选择附件', trigger: 'change' }]">
              <annex-list name="二次办理附件" v-model="state.ruleForm.files" readonly businessId="" classify="二次办理申请" />
            </el-form-item>
          </el-col>
					<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="auditContent" :rules="[{ required: true, 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>
		</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 { secondHandleAudit } from '@/api/business/secondHandle';
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: {
		auditContent: '', // 审批意见
		isPass: true, // 是否通过
	},
	detail: {}, // 详情
});
const ruleFormRef = ref<RefType>();
// 打开弹窗
const openDialog = async (val: any) => {
  console.log(val,'2121')
	state.loading = false;
	state.detail = val;
  state.ruleForm.files = transformFile(val.files);
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
};

// 审批
const onAudit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const request = {
			id: state.detail.id,
			auditContent: state.ruleForm.auditContent,
			state: state.ruleForm.isPass ? 2 : 3, // 审核结果 0 待申请 1 待办 2 审批通过 3 审批不通过 4 已办理
		};
		secondHandleAudit(request)
			.then(() => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
