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

<script lang="ts" setup>
import { ref, reactive, onMounted, watch } from "vue";
import { ElMessage } from "element-plus";
import {
  getFetchList,
  exportRequest,
} from "../../../api/monitoring/threatCenter";
import { downLoadFile, deepCopy } from "../../../utils/utils";
import { getDictTypeList } from "../../../api/common";
import { useRouter } from 'vue-router'
onMounted(() => {
  init();
});

// watch(
//   () => queryFormData.threatType,
//   (data) => {
//     console.log(data)
//   }
// )
/**
 * @description: 获取字典
 */
const threatType = ref([]);
const threatChildren = ref([]);

const initDict = () => {
  getDictTypeList({ dictType: "threatType", pid: 1474 }).then((res) => {
    res.forEach((item) => {
      item.value = item.dictKey;
      item.label = item.dictName;
      item.id = item.id;
    });
    threatType.value = res;
  });
};
const getChildrenList = (e) => {
  getDictTypeList({ dictType: "threatType", pid: e }).then((res) => {
    res.forEach((item) => {
      item.value = item.dictKey;
      item.label = item.dictName;
    });
    threatChildren.value = res;
  });
};

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

/**
 * @description: 初始化方法
 * @param {*}
 * @return {*}
 * @author: xuch
 */
const init = () => {
  getTableData("init", "loading");
  getLeftButtons();
  initDict();
};
const resetObj = (e) => {
  let key = Object.keys(e);
  let value = Object.values(e);
  for (var i = 0; i < key.length; i++) {
    queryFormData[key[i]] = value[i];
  }
  page.value = e.page;
  size.value = e.size;
};
/**
 * @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: "srcIp",
    type: "input",
    label: "攻击源IP",
    placeholder: "请输入攻击源IP",
    col: 6,
  },
  {
    key: "destIp",
    type: "input",
    label: "目的IP",
    col: 6,
    placeholder: "请输入目的IP",
  },
  {
    key: "domain",
    type: "input",
    label: "域名",
    placeholder: "请输入域名",
    col: 6,
  },
  {
    key: "threatType",
    type: "select",
    label: "威胁类型",
    options: threatType,
    col: 6,
    placeholder: "请选择威胁类型",
    onChange(e) {
      console.log(e);
      threatChildren.value = [];
      queryFormData.threatSubType = "";
      threatType.value.forEach((item) => {
        if (item.value == e) {
          getChildrenList(item.id);
        }
      });
    },
  },
  {
    key: "threatSubType",
    type: "select",
    label: "威胁子类",
    options: threatChildren,
    col: 6,
    placeholder: "请选择威胁子类",
  },
]);
const queryFormData = reactive({
  time: [],
  srcIp: "",
  destIp: "",
  threatType: "",
  threatSubType: "",
  domain: "",
});
// watch(queryFormData,(newValue,oldValue)=>{
//   console.log(newValue,oldValue);
// })
// 表格数据
const tableData = ref([]) as any;
const tableColumn: any[] = [
  {
    type: "selection",
    width: "65px",
    fixed: "left",
  },
  {
    prop: "srcIp",
    label: "攻击源IP",
    width: "160px",
  },
  {
    prop: "srcCity",
    label: "源IP位置",
    width: "110px",
  },
  {
    prop: "srcOperator",
    label: "源IP运营商",
    width: "120px",
  },
  {
    prop: "srcEnterprise",
    label: "源IP单位",
    width: "120px",
  },
  {
    prop: "destIp",
    label: "目的IP",
    minWidth: "160px",
  },
  {
    prop: "destCity",
    label: "目的IP位置",
    width: "120px",
  },
  {
    prop: "destOperator",
    label: "目的IP运营商",
    width: "140px",
  },
  {
    prop: "destEnterprise",
    label: "目的IP单位",
    width: "160px",
  },
  {
    prop: "domain",
    label: "域名",
    width: "200px",
  },
  {
    prop: "attackTime",
    label: "攻击时间",
    minWidth: "200px",
  },
  {
    prop: "threatType",
    label: "威胁类型",
    minWidth: "180px",
  },
  {
    prop: "threatSubType",
    label: "威胁子类",
    minWidth: "150px",
  },
  {
    prop: "infoType",
    label: "情报类型",
    width: "110px",
  },
];
/**
 * @description: 详情操作
 * @param {*}
 * @return {*}
 * @author: tqh
 */
const handleDetail = async (row) => {
  let _row = row; // handle data row
  //
  console.log(_row);
};
/**
 * @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 = () => {
  threatChildren.value = [];
  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,
    };
    let obj = {
      ...queryFormData,
      page: page.value,
      size: size.value,
    };
    console.log(obj, "obj====");
    params.yaLoading = loading;
    const res = await getFetchList(params);
    console.log(res, "reerr");
    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, val) => {
  console.log("调用 handleExport", type, val);

  if (type.text == "批量导出") {
    if (selectData.value.length === 0) {
      ElMessage.warning("请选择要批量导出的数据");
      return;
    }
    // @ts-ignore
    const data = selectData.value.map((item) => item.id);
    handleExportRequest(data);
  } else {
    router.push("./SpreadThelogDetail?id=" + val.id);
  }
};

/**
 * @description: 批量 / 全部 导出请求
 * @param {*} data
 * @return {*}
 * @author: xuch
 */
const handleExportRequest = async (data) => {
  try {
    const params = {
      ids: data,
    };
    const res = await exportRequest(params);
    if (res) {
      downLoadFile(res, `监测能力查询`);
      getTableData("");
    }
  } catch (error) {
    console.log(error);
  }
};

const router = useRouter();
const tableBtnClick = (btn, row) => {
  console.log(btn);
};
</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">
::v-deep .ya-table-search .ya-search-spread {
  max-height: 300px;
}
</style>
