/* eslint-disable no-unused-expressions */
import React, { useEffect, useState, useRef } from 'react';
import SiteModal from '@/components/Modal/SiteModa';
import { Spin } from 'antd';
import { GetMetaDataPreview, GetTransformationGeom } from '@/services/gis/gis';
import { ArcGISSceneMap } from '@wisdom-map/arcgismap';
import axios from 'axios';
import { getConditionList } from '@wisdom-map/basemap';
const VectorPreviewModal = props => {
  const { metaData, visible } = props;

  const [currentMeta, setCurrentMeta] = useState({});
  const [isLoading, setIsLoading] = useState(false);

  useEffect(() => {
    if (visible) {
      if (metaData && metaData.Xmax < 100000000 && metaData.Xmax > 0) {
        axios.get('/PandaGIS/MapServer/' + metaData.GISServerProjectName).then(resdata => {
          let str = `${metaData.Xmin},${metaData.Ymin},${metaData.Xmax},${metaData.Ymax}`;
          GetMetaDataPreview({ serviceName: metaData?.GISServerProjectName }).then(res => {
            if (res.code === '0') {
              let layerId = res[0]?.children.find(i => i.text === 'layerSrid : 102113');

              let str1 = `${metaData.Xmin},${metaData.Ymin}`;
              let str2 = `${metaData.Xmax},${metaData.Ymax}`;
              if (!layerId) {
                getCondition(str1, resdata.data.layerSrid, str2, '1');
              } else {
                handle(str);
              }
            } else {
              handle(str);
            }
          });
        });
      } else {
        console.log('错误坐标');
      }
    } else {
      setCurrentMeta(null);
    }
  }, [visible, metaData]);

  const getCondition = (str, id, str1, value) => {
    GetTransformationGeom({
      mapServerName: metaData?.GISServerProjectName,
      inProj: id.toString(),
      outProj: '102113',
      xy: str,
    }).then(resData => {
      if (resData.code === '0') {
        let val = JSON.parse(resData.data)?.xy;
        if (value) {
          getCondition(str1, id, val);
        } else {
          let list = `${str1},${val}`;
          console.log(list);
          debugger;
          handle(`${str1},${val}`);
        }
      }
    });
  };

  const handle = str => {
    let mapSettings = {
      layers: [
        {
          id: metaData?.GISServerProjectName,
          title: metaData?.GISServerProjectName,
          icon: '',
          layerType: '',
          url: `/PandaGIS/MapServer/${metaData?.GISServerProjectName}`,
          opacity: '1.0',
          showLegend: true,
          visible: true,
          useProxy: true,
          proxyUrl: '',
          extent: str,
          baseLayer: '',
          resolution: '',
          origin: '',
          tileMatrix: '',
          wmtsUrl: '',
          schemename: '',
          roles: '',
          areaName: '',
          boundColor: '#86c8f8',
          backgroundColor: '#000000',
          boundWidth: '10px',
          backgroundOpacity: '0.6',
          basemaps: [
            {
              id: '高德地形',
              title: '高德地形',
              thumbnailUrl: 'images/thumbnail/thumbnail_1.jpg',
              baseLayers: [
                {
                  title: '高德地形',
                  icon: '',
                  layerType: 'amap-v',
                  url: '',
                  opacity: '1.0',
                  visible: false,
                  useProxy: false,
                  proxyUrl: '',
                  style: '',
                  extent: '',
                  baseLayer: '',
                  resolution: '',
                  origin: '',
                  tileMatrix: '',
                  levelStart: '',
                  levelEnd: '',
                  levelEndEnlarge: false,
                },
              ],
            },
          ],
          levelStart: '',
        },
      ],
    };
    setCurrentMeta(mapSettings);
  };

  const getMapInfo = viewObject => {
    console.log(viewObject);
  };

  const onSubmit = () => {};
  return (
    <SiteModal
      {...props}
      title="管网预览"
      bodyStyle={{ width: '100%', minHeight: '100px' }}
      style={{ top: 200, borderRadius: '20px' }}
      width="1050px"
      cancelText="取消"
      okText="确认"
      onOk={() => onSubmit()}
    >
      <Spin spinning={isLoading}>
        <div style={{ width: '1000px', height: '500px' }}>
          {currentMeta && (
            <ArcGISSceneMap
              getMapInfo={e => getMapInfo(e)}
              config={currentMeta}
              client="sandbox"
              widgets={[]}
            />
          )}
        </div>
      </Spin>
    </SiteModal>
  );
};

export default VectorPreviewModal;
