<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">
			<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">
								<el-text tag="b" size="large"> 省工单发布内容 </el-text>
							</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: false, 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>
							<template v-if="isZGZFW">
								<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
									<el-form-item label="是否联系" prop="isContact" :rules="[{ required: true, message: '请选择是否公开', trigger: 'change' }]">
										<el-radio-group v-model="state.ruleForm.isContact" :disabled="disabled">
											<el-radio :label="true">是</el-radio>
											<el-radio :label="false">否</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="isVisited" :rules="[{ required: true, message: '请选择是否公开', trigger: 'change' }]">
										<el-radio-group v-model="state.ruleForm.isVisited" :disabled="disabled">
											<el-radio :label="true">是</el-radio>
											<el-radio :label="false">否</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="网民评价" prop="netizen" :rules="[{ required: false, message: '请选择网民评价', trigger: 'change' }]">
										<el-radio-group v-model="state.ruleForm.netizen" :disabled="disabled">
											<el-radio :label="item.key" v-for="item in netizenEvaluateType" :key="item.key">{{ item.value }}</el-radio>
										</el-radio-group>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :sm="12" :md="24" :lg="24" :xl="24" v-if="state.ruleForm.isVisited">
									<el-form-item label="评价内容" prop="evaluateContent" :rules="[{ required: false, message: '请输入评价内容', trigger: 'blur' }]">
										<el-input
											type="textarea"
											placeholder="请输入评价内容"
											:autosize="{ minRows: 6, maxRows: 10 }"
											v-model="state.ruleForm.evaluateContent"
											:disabled="disabled"
										></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :sm="12" :md="24" :lg="24" :xl="24">
									<el-form-item label="备注" prop="remark" :rules="[{ required: false, message: '请输入备注', trigger: 'blur' }]">
										<el-input
											type="textarea"
											placeholder="请输入备注"
											:autosize="{ minRows: 6, maxRows: 10 }"
											v-model="state.ruleForm.remark"
											:disabled="disabled"
										></el-input>
									</el-form-item>
								</el-col>
							</template>
						</el-row>
					</el-col>
					<el-divider content-position="left">
						<el-text tag="b" size="large"> 市工单发布内容 </el-text>
					</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="实际办理部门">
							{{ state.publishDetail.actualHandleOrgName?.value }}
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-if="idNamesArray.length">
						<el-form-item label="需回访部门" prop="idNames" :rules="[{ required: false, message: '请选择需回访部门', trigger: 'blur' }]">
							<el-checkbox-group v-model="state.ruleForm.idNames" :disabled="disabled">
								<el-checkbox :label="item" v-for="item in idNamesArray" :key="item.key">{{ item.value }}</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="closeDialog" 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 { computed, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { baseData, publishOrderDetail } from '@/api/business/publish';
import { publishOrder } from '@/api/todo/publish';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		proPublishState: false, // 是否公开
		isContact: true, // 是否联系
		isVisited: true, // 是否评价
		feedBackPhone: '', // 反馈人电话
		noPubReason: '', // 不公开原因
		arrangeContent: '', // 整改后内容
		arrangeOpinion: '', // 整改后结果
		idNames: [], // 需回访部门
		publishState: false, // 门户是否公开
	},
	orderDetail: {}, // 工单详情
	publishDetail: {}, // 发布详情
});
const ruleFormRef = ref<RefType>(); // 表单ref
const idNamesArray = ref<EmptyArrayType>(); // 部门列表
const disabled = ref<boolean>(false); //是否查看详情
const dialogTitle = ref<string>('发布详情'); // 弹窗标题
const netizenEvaluateType = ref<EmptyArrayType>([]);
/*
 * @param row 工单详情
 * @param isDisabled 是否查看详情
 * @description 打开弹窗
 * */
const openDialog = async (row: any, isDisabled: boolean = false) => {
	disabled.value = isDisabled;
	state.loading = true;
	try {
		if (isDisabled) {
			state.orderDetail = row.order ?? {};
			const [response, res] = await Promise.all([publishOrderDetail(row.id), baseData(state.orderDetail.id)]);
			state.ruleForm = response.result ?? {};
			state.ruleForm.netizen = response.result?.netizenEvaluate?.key;
			state.publishDetail = res.result ?? {};
			idNamesArray.value = res.result?.idNames ?? [];
			netizenEvaluateType.value = res.result?.netizenEvaluateType ?? [];

			dialogTitle.value = '发布详情';
			state.loading = false;
			state.dialogVisible = true;
		} else {
			state.orderDetail = row ?? {};
			const res = await baseData(state.orderDetail.id);
			state.publishDetail = res.result ?? {};
			state.ruleForm.arrangeTitle = state.publishDetail.orderTitle;
			state.ruleForm.arrangeContent = state.publishDetail.content;
			state.ruleForm.arrangeOpinion = state.publishDetail.actualOpinion;
			idNamesArray.value = res.result?.idNames ?? [];
			netizenEvaluateType.value = res.result?.netizenEvaluateType ?? [];
			dialogTitle.value = '工单发布';
			state.loading = false;
			state.dialogVisible = true;
		}
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 中国政府网特殊字段
const isZGZFW = computed(() => {
	return state.orderDetail.provinceNo.includes('ZGZFW');
});
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value.clearValidate();
	ruleFormRef.value.resetFields();
};
// 发布
const onPublish = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		let idNames = [];
		state.ruleForm.netizenEvaluate = netizenEvaluateType.value.find((item) => item.key === state.ruleForm.netizen);
		if (state.ruleForm.idNames.length > 0) {
			// 如果选择了部门
			idNames = [...state.ruleForm.idNames];
		} else {
			idNames = [{ key: state.publishDetail.actualHandleOrgName?.key, value: state.publishDetail.actualHandleOrgName?.value }];
		}
		const request = {
			...state.ruleForm,
			idNames,
			id: state.orderDetail.id,
		};
		publishOrder(request)
			.then(() => {
				ElMessage.success('发布成功');
				state.loading = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
