<script setup lang="ts">
import { ref } from "vue";
import { useAnnouncement } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "~icons/ep/delete";
import Edit from "~icons/ep/edit";
import Refresh from "~icons/ep/refresh";
import AddFill from "~icons/ri/add-circle-line";
import Top from "~icons/ep/top";
import VideoPlay from "~icons/ep/video-play";
import VideoPause from "~icons/ep/video-pause";

defineOptions({
  name: "SystemAnnouncement"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handlePublish,
  handleOffline,
  handleSetTop,
  handleSizeChange,
  handleCurrentChange
} = useAnnouncement();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="公告标题：" prop="title">
        <el-input
          v-model="form.title"
          placeholder="请输入公告标题"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item label="类型：" prop="type">
        <el-select
          v-model="form.type"
          placeholder="请选择"
          clearable
          class="w-[180px]!"
        >
          <el-option label="系统公告" :value="1" />
          <el-option label="活动公告" :value="2" />
          <el-option label="维护公告" :value="3" />
          <el-option label="其他" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择"
          clearable
          class="w-[180px]!"
        >
          <el-option label="草稿" :value="0" />
          <el-option label="已发布" :value="1" />
          <el-option label="已下线" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri/search-line')"
          :loading="loading"
          @click="onSearch(true)"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title="公告管理"
      :columns="columns"
      @refresh="onSearch"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog()"
        >
          新增公告
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(Edit)"
              @click="openDialog('编辑', row)"
            >
              编辑
            </el-button>
            <el-popconfirm
              v-if="row.status === 0"
              title="确认发布该公告?"
              @confirm="handlePublish(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="success"
                  :size="size"
                  :icon="useRenderIcon(VideoPlay)"
                >
                  发布
                </el-button>
              </template>
            </el-popconfirm>
            <el-popconfirm
              v-if="row.status === 1"
              title="确认下线该公告?"
              @confirm="handleOffline(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="warning"
                  :size="size"
                  :icon="useRenderIcon(VideoPause)"
                >
                  下线
                </el-button>
              </template>
            </el-popconfirm>
            <el-popconfirm
              :title="row.isTop === 1 ? '确认取消置顶?' : '确认置顶该公告?'"
              @confirm="handleSetTop(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  :type="row.isTop === 1 ? 'danger' : 'primary'"
                  :size="size"
                  :icon="useRenderIcon(Top)"
                >
                  {{ row.isTop === 1 ? '取消置顶' : '置顶' }}
                </el-button>
              </template>
            </el-popconfirm>
            <el-popconfirm
              title="是否确认删除?"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
