import React, { useEffect, useState } from 'react';
import { Button, Card, Form, Input, Space, Table } from 'antd';
import { PageContainer } from '@ant-design/pro-components';
import type { ColumnsType } from 'antd/es/table';
import { queryDeviceOnline } from '@/services/device';
import moment from 'moment';
import { ReloadOutlined, SearchOutlined } from '@ant-design/icons';

interface DataType {
  device_code: string;
  created_at: string;
  status: number;
  record_id: string;
}

/**
 * 设备在线管理
 * @constructor
 */
const DeviceOnlineManagement: React.FC = () => {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);
  const [searchData, setSearchData] = useState<object | null>({});
  const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  const [dataList, setDataList] = useState([]);

  // 获取列表内容
  const getList = () => {
    const params = {
      q: 'page',
      current: pagination.current,
      pageSize: pagination.pageSize,
      ...searchData,
    };
    queryDeviceOnline(params).then((res) => {
      if (res && res.code === 0) {
        setDataList(res.data.list);
        setPagination(res.data.pagination);
        setLoading(false);
      }
    });
  };

  useEffect(() => {
    setLoading(true);
    getList();
  }, []);

  useEffect(() => {
    setLoading(true);
    getList();
  }, [searchData]);

  // 搜索
  const onFinish = () => {
    form.validateFields().then((data) => {
      setLoading(true);
      setSearchData(data);
    });
  };

  // 重置
  const onReset = () => {
    form.resetFields();
    setLoading(true);
    setSearchData(null);
  };

  // 分页切换
  const tableChange = (page: any) => {
    setLoading(true);
    const params = {
      q: 'page',
      current: page.current,
      pageSize: page.pageSize,
      ...searchData,
    };
    queryDeviceOnline(params).then((res) => {
      if (res && res.code === 0) {
        setDataList(res.data.list);
        setPagination(res.data.pagination);
        setLoading(false);
      }
    });
  };

  const columns: ColumnsType<DataType> = [
    {
      title: '序号',
      align: 'center',
      key: 'index',
      width: 250,
      render: (_: any, row: any, index: number) => index + 1,
    },
    {
      title: '设备编码',
      dataIndex: 'device_code',
      key: 'device_code',
      width: 250,
    },
    {
      title: '时间',
      dataIndex: 'created_at',
      key: 'created_at',
      width: 250,
      render: (time) => <span>{moment(time).format('YYYY-MM-DD HH:mm')}</span>,
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      width: 250,
      render: (v) => (
        <span style={{ color: v === 1 ? 'green' : 'red' }}>{{ 1: '在线', 0: '离线' }[v]}</span>
      ),
    },
  ];

  const paginationProps = {
    showSizeChanger: true,
    showQuickJumper: true,
    showTotal: (total: number) => {
      return <span> 共 {total}条 </span>;
    },
    ...pagination,
  };

  return (
    <PageContainer>
      <div>
        <Card>
          <Form form={form} layout="inline" onFinish={onFinish} style={{ marginBottom: '20px' }}>
            <Form.Item name="device_code" label="设备编码">
              <Input placeholder="请输入设备编码" />
            </Form.Item>
            <Form.Item style={{ marginBottom: '10px' }}>
              <Space>
                <Button type="primary" htmlType="submit">
                  <SearchOutlined />
                  查询
                </Button>
                <Button htmlType="button" onClick={onReset}>
                  <ReloadOutlined />
                  重置
                </Button>
              </Space>
            </Form.Item>
          </Form>
          <Table
            columns={columns}
            dataSource={dataList}
            rowKey={(record) => record.record_id}
            pagination={paginationProps}
            loading={loading}
            onChange={tableChange}
          />
        </Card>
      </div>
    </PageContainer>
  );
};
export default DeviceOnlineManagement;
