<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="80px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="公告简介" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        row-key="NOTICECORP_ID"
        :data="list"
        @get-data="fnGetData"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="CREATOR" label="发布人" width="200" />
        <el-table-column prop="CREATTIME" label="发布时间" width="160" />
        <el-table-column
          prop="SYNOPSIS"
          label="公告简介"
          show-overflow-tooltip
        />
        <el-table-column label="启用状态" width="100">
          <template #default="{ row }">
            <el-switch
              v-model="row.STATUS"
              active-value="1"
              inactive-value="0"
              @change="fnChangeStatus(row.NOTICECORP_ID)"
            />
          </template>
        </el-table-column>
        <el-table-column label="查看状态" width="100">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="fnViewStatus(row.NOTICECORP_ID)"
            >
              {{ row.countCk }}/{{ row.countAll }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template #default="{ row }">
            <el-button type="primary" text link @click="fnView(row)">
              查看
            </el-button>
            <el-button
              v-if="buttonJurisdiction.edit"
              type="primary"
              text
              link
              @click="fnTopping(row.NOTICECORP_ID, row.ISTOP === '0' ? 1 : 0)"
            >
              {{ row.ISTOP === "0" ? "置顶" : "取消置顶" }}
            </el-button>
            <el-button
              v-if="buttonJurisdiction.del"
              type="primary"
              text
              link
              @click="fnDelete(row.NOTICECORP_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            v-if="buttonJurisdiction.add"
            type="primary"
            @click="
              router.push({
                path: '/notification_announcement/notification_announcement/add',
              })
            "
          >
            新增
          </el-button>
          <el-button
            v-if="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDeletion"
          >
            批量删除
          </el-button>
        </template>
      </layout-table>
    </layout-card>
    <view-info
      v-model:visible="data.viewDialog.visible"
      :info="data.viewDialog.info"
    />
    <view-status
      :id="data.viewStatusDialog.id"
      v-model:visible="data.viewStatusDialog.visible"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import {
  getNotificationAnnouncementList,
  setNotificationAnnouncementChangeStatus,
  setNotificationAnnouncementDelete,
  setNotificationAnnouncementBatchDeletion,
  setNotificationAnnouncementTopping,
} from "@/request/notification_announcement.js";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import useListData from "@/assets/js/useListData.js";
import { useRouter } from "vue-router";
import { reactive } from "vue";
import ViewInfo from "./components/view.vue";
import ViewStatus from "./components/view_status.vue";

const router = useRouter();
const { list, pagination, searchForm, tableRef, fnGetData, fnResetPagination } =
  useListData(getNotificationAnnouncementList, {
    callbackFn: (list, resData) => {
      const countNotList = resData.countNotList;
      for (let i = 0; i < list.length; i++) {
        const notMap = list[i];
        const notId = notMap.NOTICECORP_ID;
        let countCk = 0;
        let countWCK = 0;
        let countAll = 0;
        for (let j = 0; j < countNotList.length; j++) {
          const countNotId = countNotList[j].noticecorp_id;
          if (notId === countNotId) {
            if (countNotList[j].type === 1) countCk = countNotList[j].count;
            if (countNotList[j].type === 0) countWCK = countNotList[j].count;
          }
        }
        countAll = countCk + countWCK;
        notMap.countCk = countCk;
        notMap.countAll = countAll;
      }
    },
  });
const data = reactive({
  viewDialog: {
    visible: false,
    info: {},
  },
  viewStatusDialog: {
    visible: false,
    id: "",
  },
});
const buttonJurisdiction = await useButtonJurisdiction("noticecorp");
const fnChangeStatus = debounce(
  1000,
  async (NOTICECORP_ID) => {
    await setNotificationAnnouncementChangeStatus({ NOTICECORP_ID });
    ElMessage.success("状态修改成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnTopping = debounce(
  1000,
  async (NOTICECORP_ID, ISTOP) => {
    const message = ISTOP === 1 ? "置顶" : "取消置顶";
    await ElMessageBox.confirm("确定要" + message + "吗？", {
      type: "warning",
    });
    await setNotificationAnnouncementTopping({ NOTICECORP_ID, ISTOP });
    ElMessage.success(message + "成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnDelete = debounce(
  1000,
  async (NOTICECORP_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setNotificationAnnouncementDelete({ NOTICECORP_ID });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnBatchDeletion = debounce(
  1000,
  async () => {
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要删除的项");
      return;
    }
    const DATA_IDS = selectionData.map((item) => item.NOTICECORP_ID).join(",");
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setNotificationAnnouncementBatchDeletion({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnView = (row) => {
  data.viewDialog.info = row;
  data.viewDialog.visible = true;
};
const fnViewStatus = (NOTICECORP_ID) => {
  data.viewStatusDialog.visible = true;
  data.viewStatusDialog.id = NOTICECORP_ID;
};
</script>

<style scoped></style>
