<template>
	<el-dialog v-model="state.dialogVisible" draggable :title="dialogTitle" ref="dialogRef" width="50%" append-to-body destroy-on-close @close="close">
		<div class="collapse-container pb1">
			<el-form label-width="100px" ref="ruleFormRef" :model="state.ruleForm">
				<el-row :gutter="10" class="w100">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-if="state.orderDetail.isProvince">
						<el-row :gutter="0">
							<el-divider content-position="left"> <b class="font16">省工单发布内容</b> </el-divider>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
								<el-form-item label="是否公开" prop="proPublishState" :rules="[{ required: false, message: '请选择是否公开', trigger: 'change' }]">
									<el-radio-group v-model="state.ruleForm.proPublishState" :disabled="disabled">
										<el-radio :label="false">不公开</el-radio>
										<el-radio :label="true">公开</el-radio>
									</el-radio-group>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
								<el-form-item
									label="反馈人电话"
									prop="feedBackPhone"
									:rules="[
										{ required: true, message: '请输入反馈人电话', trigger: 'blur' },
										{
											pattern: /^((0\d{2,3}(-)?\d{7,8})|(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8})$/,
											message: '反馈人电话格式错误',
											trigger: 'blur',
										},
									]"
								>
									<el-input placeholder="请输入反馈人电话" v-model="state.ruleForm.feedBackPhone" clearable :disabled="disabled"></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="24" :lg="24" :xl="24" v-if="!state.ruleForm.proPublishState">
								<el-form-item label="不公开原因" prop="noPubReason" :rules="[{ required: false, message: '请输入不公开原因', trigger: 'blur' }]">
									<el-input
										type="textarea"
										placeholder="请输入不公开原因"
										:autosize="{ minRows: 6, maxRows: 10 }"
										v-model="state.ruleForm.noPubReason"
										:disabled="disabled"
									></el-input>
								</el-form-item>
							</el-col>
						</el-row>
					</el-col>
					<el-divider content-position="left"> <b class="font16">市工单发布内容</b> </el-divider>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="工单标题"> {{ state.publishDetail.orderTitle }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="整改后标题" prop="arrangeTitle" :rules="[{ required: false, message: '请输入整改后标题', trigger: 'blur' }]">
							<el-input placeholder="请输入整改后标题" v-model="state.ruleForm.arrangeTitle" clearable :disabled="disabled"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="工单内容"> {{ state.publishDetail.content }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="整改后内容" prop="arrangeContent" :rules="[{ required: false, message: '请输入整改后内容', trigger: 'blur' }]">
							<el-input
								placeholder="请输入整改后内容"
								type="textarea"
								:autosize="{ minRows: 6, maxRows: 10 }"
								v-model="state.ruleForm.arrangeContent"
								:disabled="disabled"
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="办理结果"> {{ state.publishDetail.actualOpinion }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="整改后结果" prop="arrangeOpinion" :rules="[{ required: false, message: '请输入整改后内容', trigger: 'blur' }]">
							<el-input
								type="textarea"
								placeholder="请输入整改后办件结果"
								:autosize="{ minRows: 6, maxRows: 10 }"
								v-model="state.ruleForm.arrangeOpinion"
								:disabled="disabled"
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="门户是否公开" prop="publishState" :rules="[{ required: false, message: '请输入整改后内容', trigger: 'blur' }]">
							<el-radio-group v-model="state.ruleForm.publishState" :disabled="disabled">
								<el-radio :label="false">不公开</el-radio>
								<el-radio :label="true">公开</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="实际办理部门">
              <el-checkbox :label="state.publishDetail.actualHandleOrgName">{{ state.publishDetail.actualHandleOrgName?.name }}</el-checkbox>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="需回访部门" prop="checkList" :rules="[{ required: false, message: '请选择需回访部门', trigger: 'blur' }]">
							<el-checkbox-group v-model="state.ruleForm.idNames" :disabled="disabled" :min="1">
								<el-checkbox :label="item" v-for="item in idNames" :key="item.id">{{ item.name }}</el-checkbox>
							</el-checkbox-group>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<template #footer v-if="disabled">
			<span class="dialog-footer">
				<el-button @click="onCancel" class="default-button">关 闭</el-button>
			</span>
		</template>
		<template #footer v-else>
			<span class="dialog-footer">
				<el-button @click="onCancel" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onPublish(ruleFormRef)" v-if="!disabled" :loading="state.loading">发布</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts" name="orderPublishDetail">
import { reactive, ref } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { baseData, publishOrder, publishOrderDetail } from '/@/api/business/publish';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		proPublishState: false, // 是否公开
		feedBackPhone: '', // 反馈人电话
		noPubReason: '', // 不公开原因
		arrangeContent: '', // 整改后内容
		arrangeOpinion: '', // 整改后结果
		idNames: [], // 需回访部门
		publishState: false, // 门户是否公开
	},
	orderDetail: {}, // 工单详情
	publishDetail: {}, // 发布详情
});
const ruleFormRef = ref<RefType>(); // 表单ref
const idNames = ref<EmptyArrayType>(); // 部门列表
const disabled = ref<boolean>(false); //是否查看详情
const dialogTitle = ref<string>('发布详情'); // 弹窗标题
const getBaseData = async (id: string) => {
	try {
		const res = await baseData(id);
		state.publishDetail = res.result ?? {};
		idNames.value = res.result?.idNames ?? [];
	} catch (error) {
		console.log(error);
	}
};
/*
 * @param row 工单详情
 * @param isDisabled 是否查看详情
 * @description 打开弹窗
 * */
const openDialog = async (row: any, isDisabled: boolean = false) => {
	disabled.value = isDisabled;
	if (isDisabled) {
		state.orderDetail = row.order ?? {};
		await getBaseData(state.orderDetail.id);
		const response = await publishOrderDetail(row.id);
    idNames.value = response.result?.idNames ?? [];
		state.ruleForm = response.result ?? {};
		dialogTitle.value = '发布详情';
	} else {
		state.orderDetail = row ?? {};
		await getBaseData(state.orderDetail.id);
		dialogTitle.value = '工单发布';
	}
	state.loading = false;
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value.clearValidate();
	ruleFormRef.value.resetFields();
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 发布
const onPublish = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
    if (!valid) return;
			state.loading = true;
			let request = {
				...state.ruleForm,
        idNames:[...state.ruleForm.idNames,{id:state.publishDetail.actualHandleOrgName.id,name:state.publishDetail.actualHandleOrgName.name}],
				id: state.orderDetail.id,
			};
			publishOrder(request)
				.then((res: any) => {
					ElMessage.success('操作成功');
					state.loading = false;
					closeDialog();
					emit('updateList');
				})
				.catch(() => {
					state.loading = false;
				});
		})
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>

<style lang="scss" scoped>
.collapse-box {
	.collapse-container {
	}
}
</style>
