<template>
	<el-dialog v-model="state.dialogVisible" draggable title="发布修改" 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="是否公开">
									{{ state.ruleForm.proPublishState ? '公开' : '不公开' }}
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
								<el-form-item label="反馈人电话">
									{{ state.ruleForm.feedBackPhone }}
								</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="不公开原因">
									{{ state.ruleForm.noPubReason }}
								</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="是否联系">
										{{ state.ruleForm.isContact ? '是' : '否' }}
									</el-form-item>
								</el-col>
								<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
									<el-form-item label="是否评价">
										{{ state.ruleForm.isVisited ? '是' : '否' }}
									</el-form-item>
								</el-col>
								<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
									<el-form-item label="网民评价">
										{{ state.ruleForm.netizenEvaluate?.value }}
									</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="评价内容">
										{{ state.ruleForm.evaluateContent }}
									</el-form-item>
								</el-col>
								<el-col :xs="24" :sm="12" :md="24" :lg="24" :xl="24">
									<el-form-item label="备注">
										{{ state.ruleForm.remark }}
									</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></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"
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="办理结果">
							<el-text>{{ state.publishDetail.actualOpinion }} </el-text>
						</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"
							></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">
								<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="resolve" >
              <el-radio-group v-model="state.ruleForm.resolve" 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="实际办理部门">
							{{ 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">
                <el-checkbox-group v-model="state.ruleForm.idNames" 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>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onPublish(ruleFormRef)" :loading="state.loading">提交</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts" name="orderPublishEditDetail">
import { computed, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { baseData, publishOrderDetail, publishOrderUpdate } from '@/api/business/publish';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		arrangeTitle: '', // 整改后标题
		arrangeContent: '', // 整改后内容
		arrangeOpinion: '', // 整改后结果
    publishState:null, // 门户是否公开
	},
	orderDetail: {}, // 工单详情
	publishDetail: {}, // 发布详情
});
const ruleFormRef = ref<RefType>(); // 表单ref
const idNamesArray = ref<EmptyArrayType>(); // 部门列表
const dialogTitle = ref<string>('发布详情'); // 弹窗标题
/*
 * @param row 工单详情
 * @description 打开弹窗
 * */
const openDialog = async (row: any) => {
	state.loading = true;
	try {
		state.orderDetail = row.order ?? {};
		const [res, response] = await Promise.all([baseData(row.order.id), publishOrderDetail(row.id)]);
		state.ruleForm = response.result ?? {};
		state.publishDetail = res.result ?? {};
		idNamesArray.value = res.result?.idNames ?? [];
		state.loading = false;
		state.dialogVisible = true;
	} catch (e) {
		state.loading = false;
		console.log(e);
	}
};
// 中国政府网特殊字段
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;
		publishOrderUpdate(state.ruleForm)
			.then(() => {
				ElMessage.success('操作成功');
				state.loading = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
