<template>
	<!-- 特提列表发起特提 -->
	<el-dialog
		v-model="state.dialogVisible"
		draggable
		title="特提申请"
		width="50%"
		append-to-body
		destroy-on-close
		@close="close"
		@mouseup="mouseup"
		:style="'transform: ' + state.transform + ';'"
		ref="dialogRef"
	>
		<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="工单编码"> {{ 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="nextStepCode" :rules="[{ required: true, message: '请选择特提节点', trigger: 'change' }]">
							<el-select v-model="state.ruleForm.nextStepCode" placeholder="请选择特提节点" class="w100" @change="selectTrace">
								<el-option v-for="item in steps" :value="item.key" :key="item.key" :label="item.value"> </el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" v-if="state.ruleForm.nextStepCode">
						<el-form-item label="办理对象" prop="nextHandlers" :rules="[{ required: true, message: '请选择办理对象', trigger: 'change' }]">
							<el-select-v2
								v-model="state.ruleForm.nextHandlers"
								:options="stepsItems"
								placeholder="请选择办理对象"
								class="w100"
								multiple
								clearable
								collapse-tags
								collapse-tags-tooltip
								filterable
								value-key="key"
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
						<el-form-item label="特提原因" prop="cause" :rules="[{ required: true, message: '请选择特提原因', trigger: 'change' }]">
							<el-select v-model="state.ruleForm.cause" placeholder="请选择特提原因" class="w100">
								<el-option v-for="item in specialReason" :value="item.dicDataName" :key="item.dicDataName" :label="item.dicDataName" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="特提理由" prop="reason" :rules="[{ required: true, message: '请填写特提理由', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.reason" type="textarea" :autosize="{ minRows: 6, maxRows: 10 }" placeholder="请填写特提理由" />
							<common-advice
								@chooseAdvice="chooseAdvice"
								v-model="state.ruleForm.reason"
								placeholder="请填写特提理由"
								:loading="state.loading"
								:commonEnum="commonEnum.Teti"
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="附件" prop="files" :rules="[{ required: false, message: '请选择附件', trigger: 'change' }]">
							<annex-list name="特提附件" v-model:format="filesFormat" 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 { 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 { specialApplyBase, specialApply } from '@/api/business/special';
import { commonEnum } from '@/utils/constants';

// 引入组件
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: {
		nextStepCode: null, // 特提节点
		cause: null, // 特提原因
		reason: '', // 特提理由
		nextHandlers: [], // 处理对象
	},
	orderDetail: {}, // 工单详情
	workflowId: null, // 工作流id
	transform: 'translate(0px, 0px)', // 滚动条位置
});
const ruleFormRef = ref<RefType>();
const storesUserInfo = useUserInfo();
const { userInfos } = storeToRefs(storesUserInfo); // 用户信息
const specialReason = ref<EmptyArrayType>([]); // 特提理由
const steps = ref<EmptyArrayType>([]); // 特提节点
const stepsItems = ref<EmptyArrayType>([]); // 处理对象
/**
 * @description 打开弹窗
 * @param {object} val 工单详情
 */
const openDialog = async (val: any) => {
	state.orderDetail = val;
	state.workflowId = val.workflowId;
	state.loading = true;
	state.dialogVisible = true;
	try {
		const { result } = await specialApplyBase(val.workflowId);
		steps.value = result?.step?.steps ?? [];
		specialReason.value = result.specialReason ?? [];
		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 chooseAdvice = (item: any) => {
	state.ruleForm.reason += item.content;
};
// 设置抽屉
const dialogRef = ref<RefType>();
const mouseup = () => {
	state.transform = dialogRef.value.dialogContentRef.$el.style.transform;
};
// 选择节点确定办理对象
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,
		};
	});
};
// 提交
const filesFormat = ref<EmptyArrayType>([]); // 附件列表
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		let request = {
			...state.ruleForm,
			workflowId: state.workflowId,
			orderId: state.orderDetail.id,
			files: filesFormat.value,
		};
		specialApply(request)
			.then(() => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('特提申请成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
