import React, { useState, useEffect, useRef } from 'react';
import { Form, Input, Button, Table, Select, Pagination, Space, Menu, Dropdown, Modal, Breadcrumb, message, Row, Col, Tooltip } from 'antd';
import { DownOutlined, PlusOutlined, ExclamationCircleOutlined } from '@ant-design/icons';
import '@common/common.less';
import { useSelector } from 'react-redux'
import apiObj from '@api/index';
const { post, api, xPost } = apiObj;
const { Option } = Select;
function LoginLog() {
  useEffect(() => {
    getLoginLog();
  }, []);
  const [logList, setLogList] = useState([]);
  const [total, setTotal] = useState(0);
  const [size, setSize] = useState(15);
  const [current, setCurrent] = useState(1);
  const [params, setParams] = useState({
    pages: 1,
    current: 1,
    size: 15
  });
  const [form] = Form.useForm();

  let list = []
  let data = {
    pages: 1,
    current: 1,
    size: size
  }
  //表格数据
  function getLoginLog(param) {
    post(api.getLoginLog, param || params).then((res) => {
      if (res.data.code === 200) {
        const data = res.data.data;
        setLogList(data.records);
        setTotal(data.total)
      }
    })
  }

  function onSizeChange(current, pageSize) {
    params.current = current
    params.size = pageSize
    setSize(pageSize)
    setCurrent(current)
    setParams(params)
    getLoginLog()
  }
  function changePage(page, pageSize) {
    params.current = page
    params.size = pageSize
    setCurrent(page)
    setParams(params)
    getLoginLog()
  }
  const onFinish = (value) => {
    const param = {
      ...data,
      ...value
    }
    setCurrent(1)
    setParams(param)
    getLoginLog(param);
  };
  const onReset = () => {
    setCurrent(1)
    setParams(data)
    form.resetFields();
    getLoginLog(data);
  };

  const columns = [
    { title: '编号', dataIndex: 'loginId', key: 'index' },
    { title: '登录名', dataIndex: 'loginName', key: 'index' },
    { title: '登录日期', dataIndex: 'loginDate', key: 'index' },
    { title: 'IP地址', dataIndex: 'loginIp', key: 'index' },
    { title: '地区', dataIndex: 'loginAddress', key: 'index' },
    { title: '浏览器', dataIndex: 'loginBrowser', key: 'index' },
  ]
  return (
    <div className="wrapper">
      <div className="filter-box">
        <Form
          form={form}
          name="normal_login"
          onFinish={onFinish}
          initialValues={{ status: '' }}
        >
          <Row gutter={24}>
            <Col span={5} key={0}>
              <Form.Item label="登录名" name="loginName">
                <Input placeholder="登录名" autoComplete='off'/>
              </Form.Item>
            </Col>
            <Col span={5} key={1}>
              <Form.Item label="IP地址" name="loginIp">
                <Input placeholder="IP地址" autoComplete='off' />
              </Form.Item>
            </Col>
            <Col span={6} key={3}>
              <Form.Item>
                <Button type="primary" htmlType="submit">
                  查询
                </Button>
                <Button onClick={onReset}>
                  重置
                </Button>
              </Form.Item>
            </Col>
          </Row>
        </Form>
      </div>

      <div className="table">
        <div className="table-header">
          <h2 className="table-title">登录日志</h2>
        </div>

        <Table
          columns={columns}
          scroll={{ y: 'calc(100vh - 360px)' }}
          dataSource={logList}
          rowKey={record => record.id}
          pagination={{
            current: current,
            pageSize: size,
            size: 'small',
            showSizeChanger: true,
            pageSizeOptions: ['15', '30', '60', '120'],
            showTotal: (total, range) => `第${range[0]}-${range[1]} 条/共 ${total} 条数据`,
            onShowSizeChange: (current, pageSize) => onSizeChange(current, pageSize), // 改变每页数量时更新显示
            onChange: (page, pageSize) => changePage(page, pageSize),//点击页码事件
            total: total
          }} />
      </div>
    </div >
  )
}

export default LoginLog;