<template>
  <div class="mod-sys__alert">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-select v-model="state.dataForm.level" placeholder="级别" clearable>
          <el-option label="提示" value="info"></el-option>
          <el-option label="警告" value="warning"></el-option>
          <el-option label="紧急" value="danger"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.title" placeholder="标题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="state.dataForm.sourceType" placeholder="来源类型" clearable>
          <el-option label="手动发布" value="manual"></el-option>
          <el-option label="导入任务" value="import"></el-option>
          <el-option label="定时任务" value="schedule"></el-option>
          <el-option label="数据源监测" value="datasource"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:alert:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:alert:delete')" type="danger" @click="state.deleteHandle()">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="state.dataListLoading"
      :data="state.dataList"
      border
      @selection-change="state.dataListSelectionChangeHandle"
      @sort-change="state.dataListSortChangeHandle"
      style="width: 100%"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" label="ID" header-align="center" align="center" width="180" show-overflow-tooltip></el-table-column>
      <el-table-column prop="level" label="级别" sortable="custom" header-align="center" align="center" width="80">
        <template v-slot="scope">
          <el-tag v-if="scope.row.level === 'danger'" size="small" type="danger">紧急</el-tag>
          <el-tag v-else-if="scope.row.level === 'warning'" size="small" type="warning">警告</el-tag>
          <el-tag v-else size="small" type="info">提示</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" header-align="center" align="center" width="200" show-overflow-tooltip></el-table-column>
      <el-table-column prop="content" label="内容" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="sourceType" label="来源类型" header-align="center" align="center" width="110">
        <template v-slot="scope">
          <span v-if="scope.row.sourceType === 'manual'">手动发布</span>
          <span v-else-if="scope.row.sourceType === 'import'">导入任务</span>
          <span v-else-if="scope.row.sourceType === 'schedule'">定时任务</span>
          <span v-else-if="scope.row.sourceType === 'datasource'">数据源监测</span>
          <span v-else>{{ scope.row.sourceType }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="publishTime" label="发布时间" sortable="custom" header-align="center" align="center" width="170"></el-table-column>
      <el-table-column prop="expireTime" label="过期时间" sortable="custom" header-align="center" align="center" width="170">
        <template v-slot="scope">
          <span v-if="scope.row.expireTime">{{ scope.row.expireTime }}</span>
          <span v-else style="color: #999">--</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('sys:alert:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="state.hasPermission('sys:alert:delete')" type="danger" link @click="state.deleteHandle(scope.row.id)">删除</el-button>
          <el-button v-if="state.hasPermission('sys:alert:update')" type="warning" link @click="withdrawHandle(scope.row.id)">撤回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="state.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="state.limit"
      :total="state.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="state.pageSizeChangeHandle"
      @current-change="state.pageCurrentChangeHandle"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs } from "vue";
import baseService from "@/service/baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import AddOrUpdate from "./system-alert-add-or-update.vue";

const view = reactive({
  getDataListURL: "/sys/alert/page",
  getDataListIsPage: true,
  deleteURL: "/sys/alert",
  deleteIsBatch: true,
  dataForm: {
    level: "",
    title: "",
    sourceType: ""
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: string) => {
  addOrUpdateRef.value.init(id);
};

const withdrawHandle = (id: string) => {
  ElMessageBox.confirm("确定要撤回该通知吗？撤回后前端将不再展示。", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    baseService.put(`/sys/alert/${id}/withdraw`).then(() => {
      ElMessage.success("撤回成功");
      state.getDataList();
    });
  });
};
</script>
