<!--
 * @Description: 重点网站事件
 * @Author: xuch
 * @Date: 2022-08-25 14:19:18
 * @LastEditTime: 2023-08-31 11:35:03
 * @LastEditors: 曹文丽
-->
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import {
  getList,
  exportRequest,
} from "../../../api/monitoring/website/keySiteEvents";
import { downLoadFile, deepCopy } from "../../../utils/utils";

onMounted(() => {
  init();
});

// 分页参数
const page = ref(1);
const total = ref(0);
const size = ref(20);

/**
 * @description: 初始化方法
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const init = () => {
  getTableData("init", "loading");
  getLeftButtons();
};

/**
 * @description: 获取按钮
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const buttons = ref([]) as any;
const getLeftButtons = () => {
  buttons.value = [
    {
      plain: true,
      text: "批量导出",
    },
  ];
};

// 查询参数
const queryColumns = reactive([
  {
    key: "time",
    type: "daterange",
    label: "时间范围",
    col: 6,
    valueFormat: "YYYY-MM-DD",
    startPlaceholder: "开始时间",
    endPlaceholder: "结束时间",
  },
  {
    key: "attackSitesName",
    type: "input",
    label: "被攻击网站名称",
    placeholder: "请输入被攻击网站名称",
    col: 6,
  },
  {
    key: "attackDomainName",
    type: "input",
    label: "被攻击域名",
    placeholder: "请输入被攻击域名",
    col: 6,
  },
  {
    key: "eventName",
    type: "input",
    label: "事件名称",
    placeholder: "请输入事件名称",
    col: 6,
  },
  {
    key: "attackIp",
    type: "input",
    label: "攻击源IP",
    placeholder: "请输入攻击源IP",
    col: 6,
  },
]);
const queryFormData = reactive({
  time: [],
  attackSitesName: "",
  eventName: "",
  attackDomainName: "",
  attackIp: "",
});

// 表格数据
const tableData = ref([]) as any;
const tableColumn: any[] = [
  {
    type: "selection",
    width: "65px",
    fixed: "left",
  },
  {
    prop: "discoveryTime",
    label: "发现时间",
    fixed: "left",
    width: "190px",
  },
  {
    prop: "attackSitesName",
    label: "被攻击网站名称",
    minWidth: "150px",
  },
  {
    prop: "attackDomainName",
    label: "被攻击域名",
    minWidth: "220px",
  },
  {
    prop: "eventName",
    label: "事件名称",
    minWidth: "130px",
  },
  {
    prop: "attackIp",
    label: "攻击源IP",
    width: "160px",
  },
  {
    prop: "attackOperator",
    label: "运营商",
    width: "120px",
  },
  // {
  //   prop: "attackEnterprise",
  //   label: "单位",
  //   minWidth: "120px",
  // },
  {
    prop: "attackCount",
    label: "被攻击次数（次）",
    width: "160px",
    fixed: "right",
  },
];

/**
 * @description: 表格多选操作
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const selectData = ref([]);
const handleSelect = (data) => {
  selectData.value = data;
};

/**
 * @description: 查询操作
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const search = () => {
  getTableData("init");
};

/**
 * @description: 重置操作
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const reset = () => {
  console.log("调用 reset");
  getTableData("init");
};

/**
 * @description: 获取表格数据
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const getTableData = async (type, loading?) => {
  console.log("调用 search", type);
  let dataObj = deepCopy(queryFormData);
  if (type === "init") {
    page.value = 1;
  }
  if (queryFormData.time) {
    if (queryFormData.time.length > 0) {
      dataObj.startTime = queryFormData.time[0] + " 00:00:00";
      dataObj.endTime = queryFormData.time[1] + " 23:59:59";
    }
    delete dataObj.time;
  }
  try {
    const params = {
      pageNum: page.value,
      pageSize: size.value,
      ...dataObj,
    };
    params.yaLoading = loading;
    const res = await getList(params);
    tableData.value = [];
    if (res.code !== 200) return;
    if (res.data) {
      nextTick(() => {
        tableData.value = res.data.rows || [];
        total.value = res.data.total;
      });
    }
  } catch (error) {
    console.log(error);
  }
};

/**
 * @description: 页面容量切换
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const sizeChange = function (val: number) {
  size.value = val;
  page.value = 1;
  getTableData("page");
};

/**
 * @description: 分页切换
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const currentChange = function (val: number) {
  page.value = val;
  getTableData("page");
};

/**
 * @description: 批量 / 全部 导出
 * @param {*} type batch | all
 * @return {*}
 * @author: xuch
 */
const handleExport = async (type) => {
  console.log("调用 handleExport", type);

  if (selectData.value.length === 0) {
    ElMessage.warning("请选择要批量导出的数据");
    return;
  }
  // @ts-ignore
  const data = selectData.value.map((item) => item.id);

  handleExportRequest(data);
};

/**
 * @description: 批量 / 全部 导出请求
 * @param {*} data
 * @return {*}
 * @author: xuch
 */
const handleExportRequest = async (data) => {
  console.log(data);
  try {
    const params = {
      ids: data,
    };
    const res = await exportRequest(params);
    if (res) {
      downLoadFile(res, `重点网站事件`);
      getTableData("");
    }
  } catch (error) {
    console.log(error);
  }
};
</script>

<template>
  <div>
    <YaPageContent :formData="queryFormData" :formColumns="queryColumns" :tableData="tableData"
      :tableColumns="tableColumn" searchLabelWidth="110px" @search="search" @reset="reset" :currentPage="page"
      :pageSize="size" :total="total" @sizeChange="sizeChange" @selectionChange="handleSelect"
      @currentChange="currentChange" :leftButtons="buttons" @tableBtnClick="handleExport">
    </YaPageContent>
  </div>
</template>

<style scoped lang="scss"></style>
