import rightBack from '@/assets/images/newMain/rightBack.png';
import DescriptionItem from '@/components/DescriptionItem';
import { useGlobalModalServices } from '@/pages/GlobalModalServices/provider';
import services from '@/services';
import { ConfigProvider, Popover } from 'antd';
import classNames from 'classnames';
import React, { useEffect, useState } from 'react';
import styles from './index.less';

const LS = {
  color: '#BCCEE9',
  // fontFamily: 'DingTalk JinBuTi',
  fontSize: '20px',
  fontWeight: 400,
  lineHeight: 1.1,
};

const ResourceElements: React.FC = () => {
  const { dispatch } = useGlobalModalServices();
  const [sensorCount, setSensorCount] = useState<number>(0);
  const [dataSource, setDataSource] = useState({
    total: 0,
    source: 0,
    bjNum: 0,
    wgNum: 0,
  });

  const handleClick = (item: { url?: string; onClick?: () => void }) => {
    if (item.onClick) {
      item.onClick();
    } else if (item.url) {
      window.open(item.url, '_blank');
    }
  };
  const jumpSystem = [
    {
      name: '物联感知平台',
      url: 'https://10.1.174.34:13200/iot-platform-screen',
    },
    {
      name: '网格基座',
      url: 'https://10.1.174.34:13600/user/autoLogin',
    },
  ];

  const list = [
    {
      name: '汇聚数据',
      unit: '亿条',
      decimals: 2,
      path: 'DataGovernance',
      value: dataSource.total,
    },
    {
      name: '感知委内接入',
      unit: '万个',
      decimals: 2,
      path: '',
      value: sensorCount,
    },
    {
      name: '资源',
      unit: '类',
      path: '',
      value: dataSource.source,
    },
    {
      name: '部件',
      unit: '万个',
      decimals: 2,
      path: '',
      value: dataSource.bjNum,
    },
    {
      name: '网格',
      unit: '个',
      value: dataSource.wgNum,
      path: '',
    },
  ];

  /** 获取数据   */
  const getData = () => {
    services.DataBase.getGzInfoData({
      typeCode: 'gz-sbzs',
    }).then((res) => {
      if (res.code === 200) {
        // 将感知源数量转换为万个单位
        console.log(res.data?.sensorInner);
        const sensorCountInTenThousand =
          Math.floor(((res.data?.sensorInner || 0) / 10000) * 100) / 100;
        // console.log(sensorCountInTenThousand);
        setSensorCount(sensorCountInTenThousand);
      }
    });

    services.DataBase.getSjdzInfoData({
      typeCode: 'zzcc-dp-sjzljr',
    }).then((res: any) => {
      if (res.code === 200) {
        const record = res.data?.data?.records[0];
        setDataSource({
          total: record.lt.sjjr,
          source: record.tt.find((v: any) => v.name === '资源')?.num,
          bjNum: record.tt.find((v: any) => v.name === '部件')?.num,
          wgNum: record.tt.find((v: any) => v.name === '网格')?.num,
        });
      }
    });
  };

  useEffect(() => {
    getData();
  }, []);

  return (
    <div className={styles.container}>
      <div
        style={{
          display: 'flex',
          justifyContent: 'space-between',
          alignItems: 'center',
        }}
      >
        <div className={styles.title}>
          <span>城管资源要素</span>
        </div>
        <ConfigProvider
          theme={{
            token: {
              sizePopupArrow: 0,
              colorBgElevated: 'rgba(41, 84, 136, 0.95)',
            },
          }}
        >
          <Popover
            trigger={'click'}
            content={
              <div className={styles.otherSystem}>
                {jumpSystem?.map((it) => (
                  <div key={it.name} className={styles.systemItem}>
                    <span onClick={() => handleClick(it)}>{it.name}</span>
                  </div>
                ))}
              </div>
            }
            placement="bottomRight"
          >
            <div
              className={classNames(styles.rightBtn, {
                [styles.bindClick]: !Boolean(jumpSystem?.length),
              })}
            >
              <img src={rightBack} alt="" />
            </div>
          </Popover>
        </ConfigProvider>
      </div>

      <div className={styles.content}>
        {list.map((item) => {
          return (
            <DescriptionItem
              key={item.name}
              onClick={
                item.path.length > 0
                  ? () => {
                      dispatch.push(item.path as any);
                    }
                  : undefined
              }
              value={item.value}
              decimals={item.decimals ?? 0}
              name={item.name}
              unit={item.unit}
              labelStyle={LS}
            />
          );
        })}
      </div>
    </div>
  );
};

export default ResourceElements;
