<!--
 * @Description:仿冒域名分析
 * @Author: xuch
 * @Date: 2022-08-24 16:24:38
 * @LastEditTime: 2023-03-18 17:34:01
 * @LastEditors: 李佐宁
-->
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import {
  getList,
  exportRequest,
} from "../../../api/monitoring/domain/imitationDomainAnalysis";
import { downLoadFile, deepCopy } from "../../../utils/utils";

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

// 分页参数
const page = ref(1);
const total = ref(5);
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: 'fakeSiteName',
  //   type: 'input',
  //   label: '仿冒站点名称',
  //   col: 6
  // },
  // {
  //   key: 'fakedSiteName',
  //   type: 'input',
  //   label: '被仿冒站点名称',
  //   col: 6
  // },
  {
    key: "fakeDomainName",
    type: "input",
    label: "仿冒域名",
    col: 6,
  },
  {
    key: "fakedDomainName",
    type: "input",
    label: "被仿冒域名",
    col: 6,
  },
]);
const queryFormData = reactive({
  time: [],
  startTime: "",
  endTime: "",
  fakeSiteName: "",
  fakeDomainName: "",
  fakedSiteName: "",
  fakedDomainName: "",
});

// 表格数据
const tableData = ref([]) as any;
const tableColumn: any[] = [
  {
    type: "selection",
    width: "65px",
  },
  {
    prop: "time",
    label: "发现时间",
    width: "190px",
    fixed: "left",
  },
  // {
  //   prop: 'fakeSiteName',
  //   label: '仿冒站点名称',
  // },
  {
    prop: "fakeDomainName",
    label: "仿冒域名",
  },
  {
    prop: "fakeDomainIp",
    label: "仿冒域名IP",
  },
  {
    prop: "fakeOperator",
    label: "运营商",
    width: "140px",
  },
  // {
  //   prop: "fakeEnterprise",
  //   label: "单位",
  // },
  // {
  //   prop: 'fakedSiteName',
  //   label: '被仿冒站点名称',

  // },
  {
    prop: "fakedDomainName",
    label: "被仿冒域名",
  },
  {
    prop: "similar",
    label: "域名相似度",
    width: "120px",
    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);
    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
 * @return {*}
 * @author: xuch
 */
const handleExport = async (type) => {
  console.log("调用 handleExport", type);

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

const handleExportRequest = async (data) => {
  try {
    const params = {
      ids: data.toString(),
    };
    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>
