<script setup lang="ts">
import { reactive, onMounted, ref, nextTick } from "vue";
import { getNoticeList } from "@/api/notice";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

defineOptions({ name: "AdminNotice" });

const queryRef = ref();

interface ColumnItem {
  label: string;
  prop: string;
  width?: number;
}

const state = reactive({
  formQuery: {
    title: ""
  },
  pageQuery: {
    pageNum: 1,
    pageSize: 10,
    total: 0
  },
  tableData: {
    height: 500,
    data: [] as Array<any>,
    loading: false,
    columns: [
      { label: "ID", prop: "id", width: 80 },
      { label: "标题", prop: "title", width: 200 },
      { label: "内容", prop: "content", width: 300 },
      { label: "发布者", prop: "adminUserName", width: 120 },
      { label: "状态", prop: "status", width: 100 },
      { label: "开始时间", prop: "startTime", width: 170 },
      { label: "结束时间", prop: "endTime", width: 170 },
      { label: "创建时间", prop: "createTime", width: 170 }
    ] as ColumnItem[]
  },
  detailVisible: false,
  currentNotice: null as any
});

onMounted(() => {
  loadData();
  nextTick(() => {
    const bodyHeight = document.body.clientHeight;
    const queryHeight = queryRef.value?.$el?.clientHeight || 0;
    state.tableData.height = bodyHeight - queryHeight - 230;
  });
});

const loadData = (refresh: boolean = false) => {
  if (refresh) {
    state.pageQuery.pageNum = 1;
  }
  state.tableData.loading = true;
  getNoticeList({ ...state.formQuery, ...state.pageQuery })
    .then((res: any) => {
      const { list, total } = res || {};
      state.tableData.data = list || [];
      state.pageQuery.total = total || 0;
    })
    .catch(() => {
      state.tableData.data = [];
    })
    .finally(() => {
      state.tableData.loading = false;
    });
};

const handleSizeChange = (size: number) => {
  state.pageQuery.pageSize = size;
  loadData(true);
};

const handleCurrentChange = (page: number) => {
  state.pageQuery.pageNum = page;
  loadData();
};

const handleSearch = () => {
  loadData(true);
};

const handleReset = () => {
  state.formQuery = {
    title: ""
  };
  loadData(true);
};

const handleDetail = (row: any) => {
  state.currentNotice = row;
  state.detailVisible = true;
};
</script>

<template>
  <div class="page-container">
    <el-card shadow="hover">
      <template #header>
        <span class="card-header">系统公告</span>
      </template>
      <el-form ref="queryRef" :model="state.formQuery" inline class="search-form">
        <el-form-item label="标题">
          <el-input v-model="state.formQuery.title" placeholder="请输入标题" clearable @keyup.enter="handleSearch" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri/search-line')"
            @click="handleSearch"
          >
            查询
          </el-button>
          <el-button
            :icon="useRenderIcon('ri/refresh-line')"
            @click="handleReset"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="state.tableData.loading"
        :data="state.tableData.data"
        :height="state.tableData.height"
        border
        stripe
      >
        <template #empty>
          <el-empty description="暂无数据" />
        </template>
        <el-table-column
          v-for="col in state.tableData.columns"
          :key="col.prop"
          :prop="col.prop"
          :label="col.label"
          :width="col.width"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link size="small" @click="handleDetail(row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="state.pageQuery.pageNum"
          v-model:page-size="state.pageQuery.pageSize"
          :total="state.pageQuery.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog v-model="state.detailVisible" :title="state.currentNotice?.title || '公告详情'" width="600px">
      <template v-if="state.currentNotice">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="标题">{{ state.currentNotice.title }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="state.currentNotice.status === 1 ? 'success' : 'info'" size="small">
              {{ state.currentNotice.status === 1 ? '已发布' : '未发布' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="发布者">{{ state.currentNotice.adminUserName || '-' }}</el-descriptions-item>
          <el-descriptions-item label="生效时间">{{ state.currentNotice.startTime || '-' }} ~ {{ state.currentNotice.endTime || '-' }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ state.currentNotice.createTime || '-' }}</el-descriptions-item>
          <el-descriptions-item label="内容">{{ state.currentNotice.content || '-' }}</el-descriptions-item>
        </el-descriptions>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.page-container {
  padding: 15px;
}

.card-header { font-size: 16px; font-weight: 600; }

.search-form {
  margin-bottom: 15px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
