<template>
	<el-dialog
		v-model="state.dialogVisible"
		draggable
		title="二次办理申请"
		@mouseup="mouseup"
		:style="'transform: ' + state.transform + ';'"
		ref="dialogRef"
		width="50%"
		append-to-body
		destroy-on-close
		@close="close"
	>
		<div class="collapse-container">
			<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="content" :rules="[{ required: true, message: '请填写办理理由', trigger: 'blur' }]">
							<common-advice
								@chooseAdvice="chooseAdvice"
								v-model="state.ruleForm.content"
								placeholder="请填写办理理由"
								:loading="state.loading"
								:commonEnum="commonEnum.Seat"
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="附件" prop="handleResult" :rules="[{ required: false, message: '请选择附件', trigger: 'change' }]">
							<annex-list name="二次办理附件" v-model="state.ruleForm.files" v-model:format="filesFormat" :businessId="businessId" classify="二次办理申请" />
						</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="onSubmit(ruleFormRef)" :loading="state.loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { computed, defineAsyncComponent, reactive, ref } from "vue";
import { ElMessage, FormInstance } from 'element-plus';
import { commonEnum } from '@/utils/constants';
import { secondHandleAdd, secondHandleDetail } from '@/api/business/secondHandle';
import { transformFile } from "@/utils/tools";

// 引入组件
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, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		content: '', // 申请理由
	},
	detail: {}, // 详情
  secondaryHandling:{},
	transform: 'translate(0px, 0px)', // 滚动条位置
});
const ruleFormRef = ref<RefType>();
// 打开弹窗
const isEdit = ref(false);
const openDialog = (val: any, edit = false) => {
	isEdit.value = edit;
	state.detail = val;
	if (edit) {
		secondHandleDetail(val.secondaryHandling?.id)
			.then((res: any) => {
        state.secondaryHandling = res.result;
        state.ruleForm.content = res.result.content;
        state.ruleForm.files = transformFile(res.result.files);
        state.dialogVisible = true;
      })
			.catch((err: any) => {});
	} else {
		console.log(val);
		state.dialogVisible = true;
	}
};
const businessId = computed(() => {
  if(isEdit.value){ // 修改
    return state.secondaryHandling.id;
  }else{
    return '';
  }
})
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 选中常用意见
const chooseAdvice = (item: any) => {
	state.ruleForm.content += item.content;
};
// 设置抽屉
const dialogRef = ref<RefType>();
const mouseup = () => {
	state.transform = dialogRef.value.dialogContentRef.$el.style.transform;
};
// 提交
const filesFormat = ref<EmptyArrayType>([]); // 附件列表
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		let request;
		if (isEdit.value) {
			request = {
				id: state.secondaryHandling.id,
				orderId: state.detail.order?.id,
				visitId: state.detail.orderVisit?.id,
				visitDetailId: state.detail?.id,
				files: filesFormat.value,
				content: state.ruleForm.content,
			};
		} else {
			request = {
				orderId: state.detail.order?.id,
				visitId: state.detail.orderVisit?.id,
				visitDetailId: state.detail?.id,
				files: filesFormat.value,
				content: state.ruleForm.content,
			};
		};
		secondHandleAdd(request)
			.then(() => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
