import { ref } from "vue";
import { message } from "@/utils/message";
import axios from 'axios';
import {
  queryAndroidList,
  DeteleAndroidLog
} from "@/api/modules/Inform";
export function useData() {
  const loading = ref(true);
  const tableList = ref([]);
  const isShow = ref(false);
  const pagination = ref({
    total: 0,
    pageSize: 10,
    pageSizes: [10, 20, 50, 100],
    currentPage: 1,
    background: true
  });
  const tableLabel = ref([
    {
      label: "id",
      prop: "id"
    },
    {
      label: "用户名",
      prop: "nickname"
    },
    {
      label: "文件名",
      prop: "log_name"
    },
    {
      label: "上传时间",
      prop: "createtime"
    },
    {
      label: "操作",
      fixed: "right",
      slot: "operation"
    }
  ]);
  const onSearch = async () => {
    loading.value = true;
    const { data, code } = await queryAndroidList({
      page: pagination.value.currentPage,
      page_limit: pagination.value.pageSize,

    });
    if (code) {
      tableList.value = data.lists.map(ele => {
        return {
          ...ele, ...data.total
        }
      });
      pagination.value.total = data.count;
      pagination.value.currentPage = data.page;
    }
    loading.value = false;
  };
  const handleSizeChange = (val: number) => {
    pagination.value.pageSize = val;
    onSearch();
  };
  const handleCurrentChange = (val: number) => {
    pagination.value.currentPage = val;
    onSearch();
  };
  const handleDelete = async rowData => {
    const { code } = await DeteleAndroidLog({ id: rowData.id });
    if (code) {
      message(`删除成功`, {
        type: "success"
      });
      onSearch();
    }
  }
  const downloadData = (rowData) => {
    downloadZip(rowData.log_url, rowData.log_name)
  }
  const downloadZip = (fileUrl, fileName) => {
    // 创建一个隐藏的a标签
    const link = document.createElement('a');
    link.href = fileUrl;
    link.download = fileName; // 设置下载的文件名
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  };
  return {
    onSearch,
    isShow,
    tableList,
    tableLabel,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    handleDelete,
    downloadData,
    loading
  };
}
