<template>
		<el-dialog v-model="state.dialogVisible" width="50%" draggable :title="dialogTitle">
			<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
				<el-row :gutter="10">
          <template v-if="dialogTitle === '新增通知'">
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="通知类型" prop="commonType" :rules="[{ required: true, message: '请选择通知类型', trigger: 'change' }]">
                <el-select v-model="state.ruleForm.commonType" placeholder="请选择通知类型" clearable class="w100">
                  <el-option v-for="item in commonType" :value="item.key" :key="item.key" :label="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
              <el-form-item label="通知标题" prop="content" :rules="[{ required: true, message: '请输入通知标题', trigger: 'blur' }]">
                <el-input v-model="state.ruleForm.content" placeholder="请输入通知标题" clearable  show-word-limit maxlength="200"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
              <el-form-item label="必须阅读" prop="content" :rules="[{ required: true, message: '请选择必须阅读', trigger: 'change' }]">
                <el-radio-group v-model="state.ruleForm.content">
                  <el-radio label="1">是</el-radio>
                  <el-radio label="2">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </template>
          <template v-if="dialogTitle === '新增公告'">
              <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                <el-form-item label="公告类型" prop="commonType" :rules="[{ required: true, message: '请选择公告类型', trigger: 'change' }]">
                  <el-select v-model="state.ruleForm.commonType" placeholder="请选择公告类型" clearable class="w100">
                    <el-option v-for="item in commonType" :value="item.key" :key="item.key" :label="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                <el-form-item label="公告标题" prop="content" :rules="[{ required: true, message: '请输入公告标题', trigger: 'blur' }]">
                  <el-input v-model="state.ruleForm.content" placeholder="请输入公告标题" clearable  show-word-limit maxlength="200"></el-input>
                </el-form-item>
              </el-col>
          </template>
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
              <el-form-item label="失效时间" prop="realCommunicationTime" :rules="[{ required: true, message: '请选择失效时间', trigger: 'change' }]">
                <el-date-picker
                    v-model="state.ruleForm.realCommunicationTime"
                    type="datetime"
                    placeholder="请选择失效时间"
                    value-format="YYYY-MM-DD[T]HH:mm:ss"
                    class="w100"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
              <el-form-item label="来源单位" prop="commonType" :rules="[{ required: true, message: '请选择来源单位', trigger: 'change' }]">
                <el-select v-model="state.ruleForm.commonType" placeholder="请选择来源单位" clearable class="w100">
                  <el-option v-for="item in commonType" :value="item.key" :key="item.key" :label="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
          <template v-if="dialogTitle === '新增公告'">
            <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
              <el-form-item label="公告范围" prop="commonType" :rules="[{ required: true, message: '请选择公告范围', trigger: 'change' }]">
                <el-checkbox-group v-model="state.ruleForm.commonType">
                  <el-checkbox label="Option A" />
                  <el-checkbox label="Option B" />
                  <el-checkbox label="Option C" />
                  <el-checkbox label="disabled" disabled />
                  <el-checkbox label="selected and disabled" disabled />
                </el-checkbox-group>
              </el-form-item>
            </el-col>
          </template>
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
              <el-form-item label="文档编码" prop="content" :rules="[{ required: false, message: '请输入文档编码', trigger: 'blur' }]">
                <el-input v-model="state.ruleForm.content" placeholder="请输入文档编码" 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="content" :rules="[{ required: true, message: '请输入文档内容', trigger: 'blur' }]">
                <editor v-model:get-html="state.ruleForm.content" placeholder="请输入文档内容" height="450px" />
              </el-form-item>
            </el-col>
          <template v-if="dialogTitle === '新增通知'">
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
              <el-form-item label="通知对象" prop="commonType" :rules="[{ required: true, message: '请选择通知对象', trigger: 'change' }]">
                <el-select v-model="state.ruleForm.commonType" placeholder="请选择通知对象" clearable class="w100">
                  <el-option v-for="item in commonType" :value="item.key" :key="item.key" :label="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </template>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" class="default-button">取 消</el-button>
					<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="loading">确 定</el-button>
				</span>
			</template>
		</el-dialog>
</template>

<script setup lang="ts" name="noticeAdd">
import { reactive, ref,defineAsyncComponent } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '/@/utils/tools';
import { addCommon } from '/@/api/system/commonAdvice';
import '@wangeditor/editor/dist/css/style.css'; // 引入 css
// 引入组件
const Editor = defineAsyncComponent(() => import('/@/components/Editor/index.vue'));  // 富文本编辑器
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
const props = defineProps({
  commonType: {
    type: Array,
    default: () => [],
  },
});
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
    content: '', // 意见内容
    commonType: '', // 意见类型
    isOpen:true, // 是否公开意见
	},
});
let loading = ref<boolean>(false);  // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const dialogTitle = ref<string>('新增通知');
const openDialog = async (type:string) => {
	ruleFormRef.value?.resetFields();
  switch (type){
    case '0':
      dialogTitle.value = '新增通知';
      break;
    case '1':
      dialogTitle.value = '新增公告';
      break;
    default:
      dialogTitle.value = '新增通知';
      break;
  }
	try {
		state.dialogVisible = true;
	} catch (error) {
		console.log(error);
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
    addCommon(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				emit('updateList');
			})
			.catch((error) => {

			})
			.finally(() => {
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
