"use client";

import {fetchApi, fetchFile} from "@/app/_modules/func";
import {
  ClearOutlined,
  DeleteOutlined,
  ExclamationCircleFilled,
  ReloadOutlined,
  UnlockOutlined,
} from "@ant-design/icons";
import type {ActionType, ProColumns, ProFormInstance,} from "@ant-design/pro-components";
import {PageContainer, ProTable,} from "@ant-design/pro-components";
import {App, Button, Modal, Space, Tag} from "antd";
import {useRouter} from "next/navigation";
import {faCheck, faDownload, faToggleOff, faToggleOn, faXmark,} from "@fortawesome/free-solid-svg-icons";
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";

import {useRef, useState} from "react";
import globalMessage from "@/app/_modules/globalMessage";

export default function OperLog() {
  const { push } = useRouter();

  // 添加用于控制删除确认模态框的状态
  const [deleteModalVisible, setDeleteModalVisible] = useState(false);
  
  // 添加用于控制清空确认模态框的状态
  const [clearModalVisible, setClearModalVisible] = useState(false);
  
  // 添加用于控制解锁确认模态框的状态
  const [unlockModalVisible, setUnlockModalVisible] = useState(false);

  //表格列定义
  const columns: ProColumns[] = [
    {
      title: "访问编号",
      dataIndex: "infoId",
      search: false,
    },
    {
      title: "用户名称",
      fieldProps: {
        placeholder: "请输入用户名称",
      },
      dataIndex: "userName",
      order: 3,
      sorter: true,
    },
    {
      title: "登录地址",
      fieldProps: {
        placeholder: "请输入登录地址",
      },
      dataIndex: "ipaddr",
      order: 4,
    },
    {
      title: "登录地点",
      dataIndex: "loginLocation",
      search: false,
    },

    {
      title: "浏览器",
      dataIndex: "browser",
      search: false,
    },
    {
      title: "操作系统",
      dataIndex: "os",
      search: false,
    },
    {
      title: "登录状态",
      fieldProps: {
        placeholder: "请选择登录状态",
      },
      dataIndex: "status",
      valueType: "select",
      order: 2,
      render: (_, record) => {
        return (
          <Space>
            <Tag
              color={record.status == 0 ? "green" : "red"}
              icon={
                record.status == 0 ? (
                  <FontAwesomeIcon icon={faCheck} />
                ) : (
                  <FontAwesomeIcon icon={faXmark} />
                )
              }
            >
              {_}
            </Tag>
          </Space>
        );
      },
      valueEnum: {
        0: {
          text: "成功",
          status: "0",
        },
        1: {
          text: "失败",
          status: "1",
        },
      },
    },
    {
      title: "操作信息",
      dataIndex: "msg",
      search: false,
    },
    {
      title: "登录时间",
      dataIndex: "loginTime",
      valueType: "dateTime",
      search: false,
      sorter: true,
    },
    {
      title: "操作时间",
      fieldProps: {
        placeholder: ["开始日期", "结束日期"],
      },
      dataIndex: "loginTimeRange",
      valueType: "dateRange",
      hideInTable: true,
      order: 1,
      search: {
        transform: (value) => {
          return {
            "params[beginTime]": `${value[0]} 00:00:00`,
            "params[endTime]": `${value[1]} 23:59:59`,
          };
        },
      },
    },
  ];

  //查询日志数据
  const getLog = async (params: any, sorter: any, filter: any) => {
    const searchParams = {
      pageNum: params.current,
      ...params,
    };

    delete searchParams.current;

    const queryParams = new URLSearchParams(searchParams);

    Object.keys(sorter).forEach((key) => {
      queryParams.append("orderByColumn", key);
      if (sorter[key] === "ascend") {
        queryParams.append("isAsc", "ascending");
      } else {
        queryParams.append("isAsc", "descending");
      }
    });

    const body = await fetchApi(
      `/api/monitor/logininfor/list?${queryParams}`,
      push
    );
    console.log("data:", body);
    return body;
  };

  //删除按钮是否可用，选中行时才可用
  const [rowCanDelete, setRowCanDelete] = useState(false);

  //选中行操作
  const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  const [selectedRow, setSelectedRow] = useState(undefined as any);

  //解锁按钮是否可用
  const [rowCanUnlock, setRowCanUnlock] = useState(false);

  const rowSelection = {
    onChange: (newSelectedRowKeys: React.Key[], selectedRows: any[]) => {
      setSelectedRowKeys(newSelectedRowKeys);
      setRowCanDelete(newSelectedRowKeys && newSelectedRowKeys.length > 0);

      if (newSelectedRowKeys && newSelectedRowKeys.length == 1) {
        console.log("row:", selectedRows[0]);
        setSelectedRow(selectedRows[0]);
        setRowCanUnlock(true);
      } else {
        setRowCanUnlock(false);
        setSelectedRow(undefined);
      }
    },
  };

  //点击删除按钮
  const onClickDeleteRow = () => {
    setDeleteModalVisible(true);
  };

  //确定删除选中的日志数据
  const executeDeleteRow = async () => {
    const body = await fetchApi(
      `/api/monitor/logininfor/${selectedRowKeys.join(",")}`,
      push,
      {
        method: "DELETE",
      }
    );
    if (body !== undefined) {
      if (body.code == 200) {
        globalMessage.success("删除成功");

        //删除按钮变回不可点击
        setRowCanDelete(false);
        //选中行数据重置为空
        setSelectedRowKeys([]);
        //刷新列表
        if (actionRef.current) {
          actionRef.current.reload();
        }
      } else {
        globalMessage.error(body.msg);
      }
    }
    setDeleteModalVisible(false);
  };

  //取消删除操作
  const cancelDeleteRow = () => {
    setDeleteModalVisible(false);
  };

  //点击清空按钮
  const onClickClear = () => {
    setClearModalVisible(true);
  };

  //确定清空日志数据
  const executeClear = async () => {
    const body = await fetchApi("/api/monitor/logininfor/clean", push, {
      method: "DELETE",
    });

    if (body !== undefined) {
      if (body.code == 200) {
        globalMessage.success("清空成功");
        //选中行数据重置为空
        setSelectedRowKeys([]);
        //刷新列表
        if (actionRef.current) {
          actionRef.current.reload();
        }
      } else {
        globalMessage.error(body.msg);
      }
    }
    setClearModalVisible(false);
  };

  //取消清空操作
  const cancelClear = () => {
    setClearModalVisible(false);
  };

  //点击解锁按钮
  const onClickUnlock = () => {
    setUnlockModalVisible(true);
  };

  //确定解锁用户
  const executeUnlock = async () => {
    if (!selectedRow) return;
    
    const body = await fetchApi(
      `/api/monitor/logininfor/unlock/${selectedRow.userName}`,
      push
    );

    if (body !== undefined) {
      if (body.code == 200) {
        globalMessage.success("解锁成功");
        //刷新列表
        if (actionRef.current) {
          actionRef.current.reload();
        }
      } else {
        globalMessage.error(body.msg);
      }
    }
    setUnlockModalVisible(false);
  };

  //取消解锁操作
  const cancelUnlock = () => {
    setUnlockModalVisible(false);
  };

  //搜索栏显示状态
  const [showSearch, setShowSearch] = useState(true);
  //action对象引用
  const actionRef = useRef<ActionType>(null);
  //表单对象引用
  const formRef = useRef<ProFormInstance>(null!);

  //当前页数和每页条数
  const [page, setPage] = useState(1);
  const defaultPageSize = 10;
  const [pageSize, setPageSize] = useState(defaultPageSize);

  const pageChange = (page: number, pageSize: number) => {
    setPage(page);
    setPageSize(pageSize);
  };

  //导出日志文件
  const exportTable = async () => {
    if (formRef.current) {
      const formData = new FormData();

      const data = {
        pageNum: page,
        pageSize: pageSize,
        ...formRef.current.getFieldsValue(),
      };

      Object.keys(data).forEach((key) => {
        if (data[key] !== undefined) {
          formData.append(key, data[key]);
        }
      });

      await fetchFile(
        "/api/monitor/logininfor/export",
        push,
        {
          method: "POST",
          body: formData,
        },
        `logininfor_${new Date().getTime()}.xlsx`
      );
    }
  };

  return (
    <PageContainer title={false}>
      <ProTable
        formRef={formRef}
        rowKey="infoId"
        rowSelection={{
          selectedRowKeys,
          ...rowSelection,
        }}
        columns={columns}
        request={async (params: any, sorter: any, filter: any) => {
          // 表单搜索项会从 params 传入，传递给后端接口。
          const data = await getLog(params, sorter, filter);
          if (data !== undefined) {
            return Promise.resolve({
              data: data.rows,
              success: true,
              total: data.total,
            });
          }
          return Promise.resolve({
            data: [],
            success: true,
          });
        }}
        pagination={{
          defaultPageSize: defaultPageSize,
          showQuickJumper: true,
          showSizeChanger: true,
          onChange: pageChange,
        }}
        search={
          showSearch
            ? {
                defaultCollapsed: false,
                searchText: "搜索",
              }
            : false
        }
        dateFormatter="string"
        actionRef={actionRef}
        toolbar={{
          actions: [
            <Button
              key="danger"
              danger
              icon={<DeleteOutlined />}
              disabled={!rowCanDelete}
              onClick={onClickDeleteRow}
            >
              删除
            </Button>,
            <Button
              key="clear"
              danger
              icon={<ClearOutlined />}
              onClick={onClickClear}
            >
              清空
            </Button>,
            <Button
              key="unlock"
              icon={<UnlockOutlined />}
              disabled={!rowCanUnlock}
              onClick={onClickUnlock}
            >
              解锁
            </Button>,
            <Button
              key="export"
              type="primary"
              icon={<FontAwesomeIcon icon={faDownload} />}
              onClick={exportTable}
            >
              导出
            </Button>,
          ],
          settings: [
            {
              key: "switch",
              icon: showSearch ? (
                <FontAwesomeIcon icon={faToggleOn} />
              ) : (
                <FontAwesomeIcon icon={faToggleOff} />
              ),
              tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
              onClick: (key?: string | undefined) => {
                setShowSearch(!showSearch);
              },
            },
            {
              key: "refresh",
              tooltip: "刷新",
              icon: <ReloadOutlined />,
              onClick: (key?: string | undefined) => {
                if (actionRef.current) {
                  actionRef.current.reload();
                }
              },
            },
          ],
        }}
      />
      
      {/* 删除确认模态框 */}
      <Modal
        title={
          <div style={{ display: 'flex', alignItems: 'center' }}>
            <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
            <span>系统提示</span>
          </div>
        }
        open={deleteModalVisible}
        onOk={executeDeleteRow}
        onCancel={cancelDeleteRow}
        okText="确认"
        cancelText="取消"
      >
        <p>{`确定删除访问编号为“${selectedRowKeys.join(",")}”的数据项？`}</p>
      </Modal>
      
      {/* 清空确认模态框 */}
      <Modal
        title={
          <div style={{ display: 'flex', alignItems: 'center' }}>
            <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
            <span>系统提示</span>
          </div>
        }
        open={clearModalVisible}
        onOk={executeClear}
        onCancel={cancelClear}
        okText="确认"
        cancelText="取消"
      >
        <p>是否确认清空所有操作日志数据项？</p>
      </Modal>
      
      {/* 解锁确认模态框 */}
      <Modal
        title={
          <div style={{ display: 'flex', alignItems: 'center' }}>
            <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
            <span>系统提示</span>
          </div>
        }
        open={unlockModalVisible}
        onOk={executeUnlock}
        onCancel={cancelUnlock}
        okText="确认"
        cancelText="取消"
      >
        <p>{`确定解锁用户"${selectedRow?.userName}"数据项?`}</p>
      </Modal>
    </PageContainer>
  );
}
