<!--
 * @Description: 网站黑链统计
 * @Author: xuch
 * @FilePath: \yuan-safety-manage-browser\src\views\monitoring\webStatistics.vue
 * @Date: 2022-08-25 15:33:34
 * @LastEditTime: 2023-08-31 20:32:58
 * @LastEditors: 曹文丽
-->
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import {
  getList,
  exportRequest,
} from "../../../api/monitoring/website/webStatistics";
import { getDictTypeList } from "../../../api/common";
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");
  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: "blackLinkWebsiteName",
    type: "input",
    label: "存在黑链网站名称",
    placeholder: "请输入存在黑链网站名称",
    col: 6,
  },
  {
    key: "existBlackLinkUrl",
    type: "input",
    label: "存在黑链URL",
    placeholder: "请输入存在黑链URL",
    col: 6,
  },
  {
    key: "blackLinkUrl",
    type: "input",
    label: "黑链URL",
    placeholder: "请输入黑链URL",
    col: 6,
  },
  {
    key: "dangerousLevels",
    type: "select",
    label: "危险等级",
    placeholder: "请选择危险等级",
    col: 6,
    options: [],
  },
]);
const queryFormData = reactive({
  time: [],
  blackLinkWebsiteName: "",
  websiteLocation: "",
  existBlackLinkUrl: "",
  dangerousLevels: "",
});

// 表格数据
const tableData = ref([]) as any;
const tableColumn: any[] = [
  {
    type: "selection",
    width: "65px",
  },
  {
    prop: "discoveryTime",
    label: "发现时间",
    fixed: "left",
    width: "200px",
  },
  {
    prop: "blackLinkWebsiteName",
    label: "存在黑链网站名称",
  },
  {
    prop: "existBlackLinkUrl",
    label: "存在黑链URL",
  },
  {
    prop: "blackLinkUrl",
    label: "黑链URL",
  },
  {
    prop: "blackLinkCode",
    label: "黑链信息代码段",
    minWidth: "200px",
  },
  {
    prop: "dangerousLevels",
    label: "危险等级",
    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: 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) => {
  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,
    };
    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,
    };
    const res = await exportRequest(params);
    if (res) {
      downLoadFile(res, `网站黑链`);
      getTableData("");
    }
  } catch (error) {
    console.log(error);
  }
};

const tableBtnClick = (btn, row) => {
  btn.eventName(row);
};
</script>

<template>
  <div>
    <YaPageContent :formData="queryFormData" :formColumns="queryColumns" :tableData="tableData"
      :tableColumns="tableColumn" searchLabelWidth="120px" @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>
