<template>
	<el-dialog v-model="state.dialogVisible" draggable title="终止理由修改" append-to-body destroy-on-close @close="close">
		<div class="collapse-container">
			<el-form label-width="100px" ref="ruleFormRef" :model="state.ruleForm" v-loading="state.loading">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="工单编号"> {{ state.ruleForm?.order?.no }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="工单标题"> {{ state.ruleForm?.order?.title }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="终止申请理由" class="formatted-text"> {{ state.ruleForm?.content }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="修改后理由" prop="editContent" :rules="[{ required: false, message: '请填写修改后理由', trigger: 'blur' }]">
							<el-input
								placeholder="请填写修改后理由"
								type="textarea"
								:autosize="{ minRows: 6, maxRows: 10 }"
								v-model="state.ruleForm.editContent"
							></el-input>
						</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="onSave(ruleFormRef)" :loading="state.loading">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts" name="orderPublishEditDetail">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { terminateDetail, terminateUpdate } from '@/api/business/terminate';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		editContent: '',
	},
	orderDetail: {}, // 工单详情
	publishDetail: {}, // 发布详情
});
const ruleFormRef = ref<RefType>(); // 表单ref
/*
 * @param row 工单详情
 * @description 打开弹窗
 * */
const openDialog = async (row: any) => {
	state.dialogVisible = true;
	state.loading = true;
	try {
		const res = await terminateDetail(row.id);
		state.ruleForm = res.result ?? {};
		state.ruleForm.editContent = state.ruleForm.content;
		state.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value.clearValidate();
	ruleFormRef.value.resetFields();
};
// 保存
const onSave = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const rquest = {
			id: state.ruleForm.id,
			content: state.ruleForm.editContent,
		};
		terminateUpdate(rquest)
			.then(() => {
				ElMessage.success('理由修改成功');
				state.loading = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
