import React, { useState, useEffect } from 'react';
import {
  DatePicker,
  Table,
  Row,
  Col,
  Button,
  Select,
  Input,
  notification,
  message,
  Spin,
} from 'antd';
import { SwapRightOutlined, SyncOutlined } from '@ant-design/icons';
import { Chart, Interval, Line, Tooltip, Axis } from 'bizcharts';
// import { DataSet } from '@antv/data-set';
import moment from 'moment';
import { post, PUBLISH_SERVICE } from '@/services/index';
import styles from './index.less';
const { Option } = Select;
const { Search } = Input;

const ServiceLog = () => {
  const [loading, setLoading] = useState(false); // 源数据
  const [dataTable, setDataTable] = useState([]); // 源数据
  const [visitedCount, setVisitedCount] = useState([]); // 访问量,统计数据
  const [pathCount, setPathCount] = useState([]); // 接口调用次数,统计数据
  const [reponseTime, setReponseTime] = useState([]); // 接口调用时长,统计数据
  const [timeInterval, setTimeInterval] = useState('3'); // 时间间隔，1/2/3/4（每分钟/5分钟/小时/天），默认每小时
  const [startTime, setStartTime] = useState(moment().startOf('week')); // 默认值，当天0点00:00:00
  const [endTime, setEndTime] = useState(
    moment(new Date(), 'YYYY-MM-DD HH:mm:ss'), // 默认值，当前时间
  );
  const [logType, setLogType] = useState('9999'); // 请求参数，日志类型，默认是正常，0:成功 -1:异常 9999:全部
  const [searchWord, setSearchWord] = useState(''); // 关键字
  const [showSearchStyle, setShowSearchStyle] = useState(false); // 是否显示模糊查询样式

  // 计算时间间隔（分钟）
  const start = new Date(startTime).getTime();
  const end = new Date(endTime).getTime();
  const minuteInterval = (end - start) / (60 * 1000); // 相隔多少分钟
  const countInterval = () => {
    if (minuteInterval > 0 && minuteInterval <= 30) {
      setTimeInterval('1');
    } else if (minuteInterval > 30 && minuteInterval <= 120) {
      setTimeInterval('2');
    } else if (minuteInterval > 120 && minuteInterval <= 60 * 24) {
      setTimeInterval('3');
    } else {
      setTimeInterval('4');
    }
  };
  // 计算时间间隔（月份），禁止跨月份查询
  const startMonth = new Date(startTime).getMonth();
  const endMonth = new Date(endTime).getMonth();
  const startYear = new Date(startTime).getYear();
  const endYear = new Date(endTime).getYear();
  if (minuteInterval <= 0) {
    notification.error({
      message: '时间设置有误',
      description: '起始时间应该早于结束时间',
    });
  } else if (
    (startMonth !== endMonth && startYear === endYear) ||
    startYear !== endYear
  ) {
    notification.info({
      message: '时间设置提示',
      description: '不允许跨月份查询',
    });
  }

  const columns = [
    {
      title: '接口名称',
      dataIndex: 'Path',
      key: 'Path',
      fixed: 'left',
      width: 400,
      render: item => searchStyle(item),
    },
    {
      title: '调用时间',
      dataIndex: 'CallTime',
      key: 'CallTime',
      width: 160,
    },
    // {
    //   title: 'IP',
    //   dataIndex: 'DownstreamRequest',
    //   key: 'DownstreamRequest',
    //   width: 120,
    // },
    {
      title: '返回状态',
      dataIndex: 'Result',
      key: 'Result',
      width: 80,
    },
    {
      title: '异常信息',
      dataIndex: 'ErrorMsg',
      key: 'ErrorMsg',
      render: record => {
        if (!record) {
          return '-';
        }
        return record;
      },
    },
    {
      title: '请求方法',
      dataIndex: 'Method',
      key: 'Method',
      width: 100,
    },
    {
      title: '耗时/ms',
      dataIndex: 'ConsumerTime',
      key: 'ConsumerTime',
      width: 100,
      defaultSortOrder: 'descend',
      sortDirections: ['descend', 'ascend'],
      sorter: (a, b) => a.ConsumerTime - b.ConsumerTime,
    },
    {
      title: '返回体大小/byte',
      dataIndex: 'ResponseSize',
      key: 'ResponseSize',
      width: 140,
      sortDirections: ['descend', 'ascend'],
      sorter: (a, b) => a.ResponseSize - b.ResponseSize,
    },
  ];

   // 模糊查询匹配的样式
   const searchStyle = val => {
    let n;
    if (showSearchStyle) {
      n = val.replace(
        new RegExp(searchWord, 'g'),
        `<span style='color:red'>${searchWord}</span>`,
      );
    } else {
      n = val;
    }
    return <div dangerouslySetInnerHTML={{ __html: n }} />;
  };
  // 在起止时间任意一个变化后获取数据,且起止时间应该早于结束时间,且不允许跨月查询
  useEffect(() => {
    if (
      startTime &&
      endTime &&
      end - start > 0 &&
      startMonth === endMonth &&
      startYear === endYear
    ) {
      countInterval(); // 根据起止时间计算时间间隔
    }
  }, [startTime, endTime]);
  useEffect(() => {
    if (
      startTime &&
      endTime &&
      end - start > 0 &&
      startMonth === endMonth &&
      startYear === endYear
    ) {
      setLoading(true);
      getData('/TrafficStatistics', setVisitedCount); // 访问量统计
    }
  }, [startTime, endTime, logType, timeInterval]);
  useEffect(() => {
    if (
      startTime &&
      endTime &&
      end - start > 0 &&
      startMonth === endMonth &&
      startYear === endYear
    ) {
      setLoading(true);
      getData('/TopCountList', setPathCount); // 接口调用频次统计
      getData('/TopConsumeList', setReponseTime); // 接口平均耗时统计
      getData('/GetOMSLog', setDataTable); // 接口调用记录
    }
  }, [startTime, endTime, logType]);

  // 封装接口请求，参数url/设置方法set
  const getData = (url, set) => {
    post(`${PUBLISH_SERVICE}/LogCenter${url}`, {
      // 获取日志表数据时PageSize设置为200，其他接口默认值20
      PageIndex: 1,
      PageSize: url === '/GetOMSLog' ? 1000 : 20,
      DateFrom: startTime.format('YYYY-MM-DD HH:mm:ss'),
      DateTo: endTime.format('YYYY-MM-DD HH:mm:ss'),
      IP: '',
      Module: url === '/GetOMSLog' ? searchWord : '',
      LogType: +logType,
      Description: '',
      LoginName: '',
      UserName: '',
      StaticsType: +timeInterval,
    })
      .then(res => {
        if (res.code === 0) {
          if (!res.data) {
            set([]);
          } else {
            const result = url === '/GetOMSLog' ? res.data.list : res.data;
            set(
              result.map((item, index) => {
                item.key = index;
                if (url === '/TrafficStatistics') {
                  item.StartTime = item.StartTime.replace(' ', '-');
                }
                return item;
              }),
            );
          }
        } else {
          notification.error({
            message: '数据获取失败',
            description: res.msg,
          });
        }
        setLoading(false);
      })
      .catch(err => {
        message.error(err);
        setLoading(false);
      });
  };

  // DatePicker改变点击确定时
  const changeStartTime = time => {
    setStartTime(time);
  };
  const changeEndTime = time => {
    setEndTime(time);
  };
  // 近1/6/12/24小时,同时设置对应的时间间隔
  const setTime = (time, value) => {
    setTimeInterval(value);
    setEndTime(moment(new Date(), 'YYYY-MM-DD HH:mm:ss'));
    setStartTime(
      moment(
        new Date(new Date().getTime() - time * 60 * 1000),
        'YYYY-MM-DD HH:mm:ss',
      ),
    );
  };
  // 设置返回状态
  const changeStatus = value => {
    setLogType(value);
  };
  // 设置时间间隔
  const selectChange = value => {
    setTimeInterval(value);
  };
  // 获取搜索框的值
  const handleSearch = e => {
    setSearchWord(e.target.value);
  };
  const handleReset = () => {
    setLogType('9999');
    setTimeInterval('3');
    setStartTime(moment().startOf('week'));
    setEndTime(moment(new Date(), 'YYYY-MM-DD HH:mm:ss'));
    setSearchWord('');
    setShowSearchStyle(false);
  };
  return (
    <>
      <div className={styles.serviceLog}>
        <Row className={styles.head}>
          <Col span={24}>
            <span>时间：</span>
            <DatePicker
              showTime
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="起始时间"
              value={startTime}
              onChange={changeStartTime}
              allowClear={false}
            />
            <SwapRightOutlined style={{ verticalAlign: '0.125em' }} />
            <DatePicker
              showTime
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="结束时间"
              value={endTime}
              onChange={changeEndTime}
              style={{ marginRight: '10px' }}
              allowClear={false}
            />
            <Button onClick={() => setTime(15, '1')}>近15分钟</Button>
            <Button onClick={() => setTime(60, '2')}>近1小时</Button>
            <Button onClick={() => setTime(12 * 60, '3')}>近12小时</Button>
            <Button onClick={() => setTime(24 * 60, '3')}>近1天</Button>
            <Button onClick={() => setTime(24 * 7 * 60, '4')}>近1周</Button>
            <span style={{ marginLeft: '20px' }}>返回状态：</span>
            <Select
              defaultValue="全部"
              value={logType}
              onChange={changeStatus}
              className={styles.sel}
            >
              <Option value="9999">全部</Option>
              <Option value="0">正常</Option>
              <Option value="-1">异常</Option>
            </Select>
            <Search
              allowClear
              style={{ width: 250, marginLeft: '20px' }}
              placeholder="请输入接口名称"
              onSearch={() => {
                getData('/GetOMSLog', setDataTable);
                setShowSearchStyle(true);
              }}
              onChange={e => handleSearch(e)}
              enterButton
              value={searchWord}
            />
            <Button
              icon={<SyncOutlined className={styles.icon} />}
              onClick={handleReset}
              style={{
                marginLeft: '25px',
                verticalAlign: 'middle',
                marginTop: '-3px',
              }}
            >
              重置
            </Button>
          </Col>
        </Row>
        <Spin spinning={loading} tip="loading">
          <Row style={{ background: 'white' }}>
            <Col span={8} style={{ paddingTop: '8px', paddingLeft: '5px' }}>
              <span style={{ fontSize: '14px', fontWeight: '600' }}>
                访问量统计
              </span>
              <span style={{ paddingLeft: '60%' }}>间隔：</span>
              <Select
                defaultValue="每小时"
                size="small"
                value={timeInterval}
                onChange={selectChange}
                className={styles.sel}
              >
                <Option value="1">每分钟</Option>
                <Option value="2">每5分钟</Option>
                <Option value="3">每小时</Option>
                <Option value="4">每天</Option>
              </Select>
            </Col>
            <Col span={7} offset={1} style={{ paddingTop: '8px' }}>
              <span style={{ fontSize: '14px', fontWeight: '600' }}>
                接口调用频次统计
              </span>
            </Col>
            <Col span={7} offset={1} style={{ paddingTop: '8px' }}>
              <span style={{ fontSize: '14px', fontWeight: '600' }}>
                接口平均耗时统计
              </span>
            </Col>
          </Row>
          <Row className={styles.chart}>
            <Col span={8}>
              <Chart
                height={300}
                autoFit
                data={visitedCount}
                interactions={['active-region']}
                padding="auto"
                renderer="svg"
                scale={{
                  Count: { alias: '计数' },
                  StartTime: { alias: '访问开始时间' },
                }}
              >
                <Axis
                  name="StartTime"
                  label=""
                  title={{ offset: 20 }}
                  className={styles.fontColor}
                />
                <Axis name="Count" title />
                <Line shape="smooth" position="StartTime*Count" />
                <Tooltip shared lock />
              </Chart>
            </Col>
            <Col span={7} offset={1}>
              <Chart
                height={300}
                autoFit
                data={pathCount}
                interactions={['active-region']}
                padding="auto"
                renderer="svg"
                scale={{
                  Count: { alias: '计数' },
                  Path: { alias: '接口调用频次' },
                }}
              >
                <Axis
                  name="Path"
                  label=""
                  title={{ offset: 20 }}
                  className={styles.fontColor}
                />
                <Axis name="Count" title />
                <Interval position="Path*Count" />
                <Tooltip shared lock />
              </Chart>
            </Col>
            <Col span={7} offset={1}>
              <Chart
                height={300}
                autoFit
                data={reponseTime}
                interactions={['active-region']}
                padding="auto"
                renderer="svg"
                scale={{
                  AvgTime: { alias: '响应时长/ms' },
                  Path: { alias: '接口平均响应时长' },
                }}
              >
                <Axis name="Path" label="" title={{ offset: 20 }} />
                <Axis name="AvgTime" title />
                <Interval position="Path*AvgTime" />
                <Tooltip shared lock />
              </Chart>
            </Col>
          </Row>
          <div className={styles.table}>
            <Table
              size="small"
              bordered
              columns={columns}
              dataSource={dataTable}
              scroll={{ x: 'max-content', y: 'calc(100vh - 580px)' }}
              pagination={{
                showTotal: (total, range) =>
                  `第${range[0]}-${range[1]} 条/共 ${total} 条`,
                pageSizeOptions: [10, 20, 50, 100],
                defaultPageSize: 20,
                showQuickJumper: true,
                showSizeChanger: true,
              }}
            />
          </div>
        </Spin>
      </div>
    </>
  );
};
export default ServiceLog;
