import { h, ref } from "vue";
import editForm from "./form.vue";
import {
  queryLogList
} from "@/api/modules/admin";
import { addDialog } from "@/components/ReDialog";
export function useData() {
  const formRef = ref();
  const loading = ref(true);
  const tableList = ref([]);
  const isShow = ref(false);
  const pagination = ref({
    total: 0,
    pageSize: 10,
    pageSizes: [10, 20, 50, 100, 500, 1000, 2000],
    currentPage: 1,
    background: true
  });
  const searchForm = ref({
    search_username: "",
    search_title: "",
    search_time: "",
    search_etime: ""
  });
  const searchLabel = ref([
    { label: "用户名", prop: "search_username", type: "input" },
    { label: "标题", prop: "search_title", type: "input" },
    { label: "开始时间", prop: "search_time", type: "date" },
    { label: "结束时间", prop: "search_etime", type: "date" },
  ]);
  const indexMethod = (index: number) => {
    return index + 1;
  };
  const tableLabel = ref([
    {
      type: "index",
      index: indexMethod
    },
    {
      label: "日志时间",
      prop: "createtime"
    },
    // {
    //   label: "终端设备",
    //   prop: "ip"
    // },
    {
      label: "操作类型",
      prop: "title"
    },
    {
      label: "操作人员",
      prop: "username"
    },
    {
      label: "IP地址",
      prop: "ip"
    },
    {
      label: "内容",
      prop: "url"
    },
    {
      label: "操作",
      fixed: "right",
      width: 210,
      slot: "operation"
    }
  ]);
  const onSearch = async (formData) => {
    loading.value = true;
    searchForm.value = { ...formData }
    const { data, code } = await queryLogList({
      ...formData,
      pageNum: pagination.value.currentPage,
      pageSize: pagination.value.pageSize
    });
    if (code) {
      tableList.value = data.lists;
      pagination.value.total = data.count;
      pagination.value.currentPage = data.page;
    }
    loading.value = false;
  };
  const handleSizeChange = (val: number) => {
    pagination.value.pageSize = val;
    onSearch(searchForm.value);
  };
  const handleCurrentChange = (val: number) => {
    pagination.value.currentPage = val;
    onSearch(searchForm.value);
  };
  const openDialog = (title = "新增", rowData: any) => {
    addDialog({
      title: `${title}操作日志`,
      props: {
        formInline: {
          id: rowData?.id ?? "",
        }
      },
      width: "40%",
      closeOnClickModal: false,
      contentRenderer: () => h(editForm, { ref: formRef, formInline: null })
    });
  };
  return {
    searchForm,
    searchLabel,
    onSearch,
    isShow,
    tableList,
    tableLabel,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    loading,
    openDialog
  };
}
