<template>
  <div class="notice-management">
    <el-card>
      <template #header>
        <div class="card-title">
          <span>通知公告</span>
          <el-button
            type="primary"
            @click="handleAdd"
          >
            新增公告
          </el-button>
        </div>
      </template>
      <el-table
        v-loading="loading"
        :data="dataSource"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
        />
        <el-table-column
          prop="noticeTitle"
          label="公告标题"
        />
        <el-table-column
          label="公告类型"
          width="100"
        >
          <template #default="{ row }">
            <el-tag 
              :type="row.noticeType === '1' ? 'info' : 'success'"
              effect="dark"
              style="font-weight: 500; font-size: 14px;"
            >
              {{ row.noticeType === '1' ? '通知' : '公告' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          width="80"
        >
          <template #default="{ row }">
            <el-tag 
              :type="row.status === '0' ? 'success' : 'danger'"
              effect="dark"
              style="font-weight: 500; font-size: 14px;"
            >
              {{ row.status === '0' ? '正常' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="发布时间"
        >
          <template #default="{ row }">
            {{ formatDateTime(row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150"
        >
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              size="small"
              @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              link
              size="small"
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      v-model="modalVisible"
      :title="modalTitle"
      width="500px"
    >
      <el-form
        :model="formState"
        label-width="80px"
      >
        <el-form-item label="公告标题">
          <el-input v-model="formState.noticeTitle" />
        </el-form-item>
        <el-form-item label="公告类型">
          <el-select v-model="formState.noticeType">
            <el-option
              value="1"
              label="通知"
            />
            <el-option
              value="2"
              label="公告"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input
            v-model="formState.noticeContent"
            type="textarea"
            :rows="4"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formState.status">
            <el-option
              value="0"
              label="正常"
            />
            <el-option
              value="1"
              label="停用"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="modalVisible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="handleModalOk"
        >
          确定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
import { formatDateTime } from '@/utils/dateFormat'

const loading = ref(false)
const dataSource = ref([])
const modalVisible = ref(false)
const modalTitle = ref('')
const formState = reactive({ id: null, noticeTitle: '', noticeType: '1', noticeContent: '', status: '0' })

const fetchData = async () => {
  loading.value = true
  try {
    const res: any = await request.get('/notices')
    dataSource.value = res
  } finally {
    loading.value = false
  }
}

const handleAdd = () => {
  modalTitle.value = '新增公告'
  Object.assign(formState, { id: null, noticeTitle: '', noticeType: '1', noticeContent: '', status: '0' })
  modalVisible.value = true
}

const handleEdit = (row: any) => {
  modalTitle.value = '编辑公告'
  Object.assign(formState, row)
  modalVisible.value = true
}

const handleDelete = async (row: any) => {
  try {
    await ElMessageBox.confirm('确定要删除该公告吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await request.delete(`/notice/${row.id}`)
    ElMessage.success('删除成功')
    fetchData()
  } catch (error) {
    console.error('删除通知失败:', error)
  }
}

const handleModalOk = async () => {
  try {
    if (formState.id) {
      await request.put(`/notices/${formState.id}`, formState)
    } else {
      await request.post('/notices', formState)
    }
    ElMessage.success('操作成功')
    modalVisible.value = false
    fetchData()
  } catch {
    ElMessage.error('操作失败')
  }
}

onMounted(() => fetchData())
</script>

<style scoped lang="css">
.notice-management .card-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
