<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"
	>
		<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="工单编码"> {{ state.orderDetail.no }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="工单标题"> {{ state.orderDetail.title }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="催办人"> {{ userInfos.name }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="催办部门"> {{ userInfos.orgName }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="催办时间"> {{ formatDate(Date(), '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="被催办部门" prop="org" :rules="[{ required: true, message: '请选择被催办部门', trigger: 'change' }]">
						<el-select
							v-model="state.ruleForm.org"
							placeholder="请选择被催办部门"
							class="w100"
							value-key="key"
							multiple
							collapse-tags
							collapse-tags-tooltip
						>
							<el-option v-for="item in state.orgData" :value="item" :key="item.key" :label="item.value" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="" prop="acceptSms" :rules="[{ required: false, message: '请选择发送催办短信', trigger: 'change' }]">
						<el-checkbox v-model="state.ruleForm.acceptSms" label="发送催办短信" />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="催办内容" prop="applyContent" :rules="[{ required: true, message: '请填写催办内容', trigger: 'blur' }]">
						<common-advice
							@chooseAdvice="chooseAdvice"
							v-model="state.ruleForm.applyContent"
							placeholder="请填写催办内容"
							:loading="state.loading"
							:commonEnum="commonEnum.Supervise"
						/>
					</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:format="filesFormat" classify="催办申请" />
					</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="businessUrgeDialog">
import { defineAsyncComponent, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { storeToRefs } from 'pinia';
import { useUserInfo } from '@/stores/userInfo';
import { formatDate } from '@/utils/formatTime';
import { commonEnum } from '@/utils/constants';
import { urgeOrgList } from '@/api/business/supervise';
import { urgeApply } from '@/api/business/urge';

// 引入组件
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: {
		acceptSms: false, // 发送催办短信
		applyContent: '', // 催办内容
		org: [],
	},
	orderDetail: {}, // 工单详情
	transform: 'translate(0px, 0px)', // 滚动条位置
	orgData: [], // 机构数据
});
const ruleFormRef = ref<RefType>();
const storesUserInfo = useUserInfo();
const { userInfos } = storeToRefs(storesUserInfo); // 用户信息
// 打开弹窗
const openDialog = async (val: any) => {
	if (!val.workflow) {
		ElMessage.warning('该工单未开启流程');
		return;
	}
	state.dialogVisible = true;
	state.loading = true;
	state.orderDetail = val;
	try {
		const { result } = await urgeOrgList(val.workflow.id);
		state.orgData = result ?? [];
		state.loading = false;
	} catch (error) {
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
	ruleFormRef.value.clearValidate();
	ruleFormRef.value.resetFields();
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 选中常用意见
const chooseAdvice = (item: any) => {
	state.ruleForm.applyContent += 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;
		const request = {
			files: filesFormat.value,
			orderId: state.orderDetail.id,
			applyContent: state.ruleForm.applyContent,
			acceptSms: state.ruleForm.acceptSms,
			urgeOrgDtos: state.ruleForm.org.map((item: any) => {
				return {
					orgId: item.key,
					orgName: item.value,
				};
			}),
		};
		urgeApply(request)
			.then(() => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
