<!--
 * @Description: 异常特征样本
 * @Author: tqh
 * @Date: 2022-08-31 17:00
-->

<script lang="ts" setup>
import { ref, reactive, onMounted, defineProps } from "vue";
import { ElMessage } from "element-plus";
import {
  getList,
  exportRequest,
} from "../../../api/monitoring/attackEvent/AbnormalSample";
import { getDictTypeList } from "../../../api/common";
import { downLoadFile, deepCopy } from "../../../utils/utils";
import { useToButtonRouter } from "../../../composables/useToButtonRouter";
const { toButtonRouter } = useToButtonRouter();

onMounted(() => {
  init();
});
const { detailPath } = defineProps({
  detailPath: String
})
let searchForm = ref(
  (sessionStorage.getItem("searchForm") &&
    JSON.parse(sessionStorage.getItem("searchForm"))) ||
  {}
);

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

/**
 * @description: 初始化方法
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const init = () => {
  // if (sessionStorage.getItem('searchForm')) {
  //   let obj = JSON.parse(sessionStorage.getItem('searchForm'))
  //   if (obj.pageNum && obj.pageSize) {
  //     page.value = obj.pageNum
  //     size.value = obj.pageSize
  //   }
  //   queryFormData.attackedIp =  obj.attackedIp
  //   queryFormData.attackedPort =  obj.attackedPort
  //   queryFormData.attackType = obj.attackType
  //   queryFormData.alarmLevel = obj.alarmLevel
  //   getTableData('')
  // } else {
  //   getTableData('init')
  // }
  getTableData("init", "loading");
  getLeftButtons();
  getDictList();
};

/**
 * @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: "attackedIp",
    type: "input",
    label: "被攻击IP",
    placeholder: "请输入被攻击IP",
    col: 6,
  },
  {
    key: "attackedPort",
    type: "input",
    label: "被攻击端口",
    placeholder: "请输入被攻击端口",
    col: 6,
  },
  {
    key: "attackType",
    type: "input",
    label: "攻击类型",
    placeholder: "请输入攻击类型",
    col: 6,
  },
  {
    key: "alarmLevel",
    type: "select",
    label: "告警级别",
    placeholder: "请选择告警级别",
    col: 6,
    options: [],
  },
]);
const queryFormData = reactive({
  time: [],
  attackedIp: "",
  attackedPort: "",
  attackType: "",
  alarmLevel: "",
});

// 表格数据
const tableData = ref([]) as any;
const tableColumn: any[] = [
  {
    type: "selection",
    width: "65px",
  },
  {
    prop: "startTime",
    label: "事件开始时间",
    fixed: "left",
    width: "200px",
  },
  {
    prop: "endTime",
    label: "事件结束时间",
    width: "200px",
  },
  {
    prop: "attackedIp",
    label: "被攻击IP",
    minWidth: "160px",
  },
  {
    prop: "operator",
    label: "运营商",
    minWidth: "160px",
  },
  // {
  //   prop: "attackedEnterprise",
  //   label: "单位",
  //   minWidth: "220px",
  // },
  {
    prop: "protocol",
    label: "攻击类型",
    minWidth: "120px",
  },
  {
    prop: "excepData",
    label: "异常流量",
    minWidth: "180px",
  },
  {
    prop: "attackedPort",
    label: "被攻击端口",
    minWidth: "300px",
  },
  {
    prop: "attackCount",
    label: "连接数（个）",
    minWidth: "180px",
  },
  {
    prop: "eventLevel",
    label: "告警级别",
    minWidth: "180px",
  },
  {
    label: "操作",
    buttons: [
      {
        title: "详情",
        eventName: function (row) {
          handleDetail(row);
        },
      },
    ],
  },
];
/**
 * @description: 详情操作
 * @param {*}
 * @return {*}
 * @author: tqh
 */
const handleDetail = async (row) => {
  console.log("详情点击");
  let _row = row;
  const _detailPath = detailPath || "/monitoring/attackEvent/AbnormalSampleDetail";
  toButtonRouter(_detailPath, {
    ip: _row.attackedIp,
    startTime: _row.startTime,
    endTime: _row.endTime,
    sumDate: _row.excepData,
  });
};
/**
 * @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: tqh
 */
const getDictList = async (type) => {
  try {
    const params = {
      dictType: "告警级别",
    };
    const res = await getDictTypeList(params);
    let arr = [];
    res.forEach((item) => {
      arr.push({
        value: item.dictValue,
        label: item.dictValue,
      });
    });
    queryColumns[4].options = arr;
  } catch (error) {
    console.log(error);
  }
};

/**
 * @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,
    };
    // sessionStorage.setItem('searchForm',JSON.stringify(params))
    params.yaLoading = loading;
    const res = await getList(params);
    if (res.code !== 200) return;
    if (res.data) {
      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.toString(),
    };
    const res = await exportRequest(params);
    if (res) {
      downLoadFile(res, `DDos事件`);
      getTableData("");
    }
  } catch (error) {
    console.log(error);
  }
};

const tableBtnClick = (btn, row) => {
  if (btn.text == "批量导出") {
    handleExport(btn);
  } else {
    btn.eventName(row);
  }
};
</script>

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

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