<template>
  <el-container v-loading="loading" :element-loading-text="t('modules.notification.loading')">
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" :label-width="formLabelWidth">
        <el-form-item :label="t('modules.notification.messageTitle')" prop="title">
          <el-input v-model="form.model.title" :placeholder="t('modules.notification.enterMessageTitle')"></el-input>
        </el-form-item>
        <el-form-item :label="t('modules.notification.messageContent')" prop="message">
          <el-input v-model="form.model.message" type="textarea" :rows="5"
            :placeholder="t('modules.notification.enterMessageContent')"></el-input>
        </el-form-item>
        <el-form-item :label="t('modules.notification.pagePath')" prop="pages">
          <el-input v-model="form.model.pages" :placeholder="t('modules.notification.enterPagePath')"></el-input>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button v-if="modal.params.type == 'add'" type="primary" @click="confirm">{{ t('modules.notification.save')
      }}</el-button>
      <el-button v-if="modal.params.type == 'edit'" type="primary" @click="confirm">{{ t('modules.notification.update')
      }}</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
import { cloneDeep } from 'lodash';
import { onMounted, reactive, ref, unref } from 'vue';
import { useI18n } from 'vue-i18n';
import { ElMessage } from 'element-plus';
import { useFormConfig } from '@/hooks/useFormConfig';
import { api } from '../content.service';

const { t } = useI18n();

// 使用表单配置hooks
const { formLabelWidth } = useFormConfig({ enWidth: '140px' });
const emit = defineEmits(['modalCallBack']);
const props = defineProps({
  modal: {
    type: Object,
  },
});
// 添加 编辑 form
let formRef = ref(null);
const form = reactive({
  model: {
    title: '',
    message: '',
    pages: '',
  },
  rules: {
    title: [{ required: true, message: t('modules.notification.messageTitleRequired'), trigger: 'blur' }],
    message: [{ required: true, message: t('modules.notification.messageContentRequired'), trigger: 'blur' }],
  },
});
const loading = ref(false);
// 获取详情
async function getDetail(id) {
  loading.value = true;
  try {
    const { code, data } = await api.notification.detail(id);
    if (code == 200) {
      form.model = data;
    }
  } catch (error) {
    console.error('获取详情失败:', error);
  }
  loading.value = false;
}
// 表单关闭时提交
async function confirm() {
  unref(formRef).validate(async (valid) => {
    if (!valid) return;
    let submitForm = cloneDeep(form.model);
    try {
      const { code } =
        props.modal.params.type == 'add'
          ? await api.notification.add(submitForm)
          : await api.notification.edit(props.modal.params.id, submitForm);
      if (code == 200) {
        ElMessage.success(t('message.saveSuccess'));
        emit('modalCallBack', { event: 'confirm' });
      }
    } catch (error) {
      console.error('提交失败:', error);
    }
  });
}
async function init() {
  if (props.modal.params.id) {
    await getDetail(props.modal.params.id);
  }
}
onMounted(() => {
  init();
});
</script>
