<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" v-loading="state.loading">
				<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 :value="false">不公开</el-radio>
										<el-radio :value="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' }]">
									<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 :value="true">是</el-radio>
											<el-radio :value="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 :value="true">是</el-radio>
											<el-radio :value="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 :value="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="工单内容" class="formatted-text"> {{ 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="办理结果" class="formatted-text"> {{ 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="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="门户是否公开" prop="publishState">
							<el-radio-group v-model="state.ruleForm.publishState" :disabled="disabled">
								<el-radio :value="false">不公开</el-radio>
								<el-radio :value="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="resolve" :rules="[{ required: true, message: '请选择处理结果', trigger: 'change' }]">
							<el-radio-group v-model="state.ruleForm.resolve" :disabled="disabled">
								<el-radio :value="true">已得到解决</el-radio>
								<el-radio :value="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="实际办理部门">
							{{ 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 :value="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" :loading="state.loading">取 消</el-button>
				<el-button type="primary" @click="onReturn" title="业务信件退回" v-auth="'business:publish:return'" :loading="state.loading"> 退回 </el-button>
				<el-button type="primary" @click="onRedo" title="业务信件重办" v-auth="'business:publish:redo'" :loading="state.loading" v-if="['ZiGong'].includes(themeConfig.appScope)"> 重办 </el-button>
				<el-button type="primary" @click="onPublish(ruleFormRef)" v-if="!disabled" :loading="state.loading">发布</el-button>
			</span>
		</template>
	</el-dialog>
	<!-- 工单退回 -->
	<order-publish-return ref="orderPublishReturnRef" @updateList="updateList" />
	<order-publish-redo ref="orderPublishRedoRef" @updateList="updateList" />
</template>
<script setup lang="ts" name="orderPublishDetail">
import { computed, reactive, ref, defineAsyncComponent } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { baseData, publishOrderDetail } from '@/api/business/publish';
import { publishOrder } from '@/api/todo/publish';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';

const OrderPublishReturn = defineAsyncComponent(() => import('@/views/business/publish/component/Order-publish-return.vue')); // 工单退回
const OrderPublishRedo = defineAsyncComponent(() => import('@/views/business/publish/component/Order-publish-redo.vue')); // 工单重办
// 定义子组件向父组件传值/事件
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, // 门户是否公开
		resolve: null, // 处理结果
	},
	orderDetail: {}, // 工单详情
	publishDetail: {}, // 发布详情
});
const ruleFormRef = ref<RefType>(); // 表单ref
const idNamesArray = ref<EmptyArrayType>([]); // 部门列表
const disabled = ref<boolean>(false); //是否查看详情
const dialogTitle = ref<string>('发布详情'); // 弹窗标题
const netizenEvaluateType = ref<EmptyArrayType>([]);
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
/*
 * @param row 工单详情
 * @param isDisabled 是否查看详情
 * @description 打开弹窗
 * */
const openDialog = async (row: any, isDisabled: boolean = false) => {
	disabled.value = isDisabled;
	state.loading = true;
  state.dialogVisible = 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;
		} 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:EmptyArrayType;
		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;
			});
	});
};
// 退回
const orderPublishReturnRef = ref<RefType>(); // 工单退回ref
const onReturn = () => {
	orderPublishReturnRef.value.openDialog(state.orderDetail);
};
// 重办
const orderPublishRedoRef = ref<RefType>();
const onRedo = () => {
	orderPublishRedoRef.value.openDialog(state.orderDetail);
};
// 重办和退回成功
const updateList = () => {
	closeDialog();
	emit('updateList');
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
