const options = {
  url: '/arcgisApi/4.7/init.js',
};

import axios from 'axios';
import { _getLngLat } from './mercatorToLngLat';

let cancelRequest;
var cancelToken = new axios.CancelToken(function executor(c) {
  cancelRequest = c;
  console.log(c);
  // 这个参数 c 就是CancelToken构造函数里面自带的取消请求的函数，这里把该函数当参数用
});
var gateFlow = '';
var gateFlowWidthZoom = [
  {
    zoom: 0,
    gateFlow: 1000,
  },
  {
    zoom: 1,
    gateFlow: 1000,
  },
  {
    zoom: 2,
    gateFlow: 1000,
  },
  {
    zoom: 3,
    gateFlow: 1000,
  },
  {
    zoom: 4,
    gateFlow: 700,
  },
  {
    zoom: 5,
    gateFlow: 700,
  },
  {
    zoom: 6,
    gateFlow: 400,
  },
  {
    zoom: 7,
    gateFlow: 400,
  },
  {
    zoom: 8,
    gateFlow: 200,
  },
  {
    zoom: 9,
    gateFlow: 200,
  },
  {
    zoom: 10,
    gateFlow: 100,
  },
  {
    zoom: 11,
    gateFlow: 100,
  },
  {
    zoom: 12,
    gateFlow: '',
  },
  {
    zoom: 13,
    gateFlow: '',
  },
  {
    zoom: 14,
    gateFlow: '',
  },
  {
    zoom: 15,
    gateFlow: '',
  },
  {
    zoom: 16,
    gateFlow: '',
  },
  {
    zoom: 17,
    gateFlow: '',
  },
  {
    zoom: 18,
    gateFlow: '',
  },
];

import _default, * as esriLoader from 'esri-loader';
import { getAllSZList, getShgcListForMap } from '../../../api/zhxxAPI.js';
export function getSzList(_self) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var szGraphicLayer = _self.mapObject.findLayerById('szGraphicLayer');
      szGraphicLayer.removeAll();
      gateFlowWidthZoom.forEach((item) => {
        if (item.zoom == _self.viewObject.zoom) {
          // gateFlow = item.gateFlow;
        }
      });
      var symbol = {
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/sz.png',
        width: '12px',
        height: '12px',
      };
      var maxLgtd = _self.viewObject.extent.xmax;
      var maxLttd = _self.viewObject.extent.ymax;
      var minLgtd = _self.viewObject.extent.xmin;
      var minLttd = _self.viewObject.extent.ymin;
      var bottomLeft = _getLngLat({
        x: minLgtd,
        y: minLttd,
      });
      var topRight = _getLngLat({
        x: maxLgtd,
        y: maxLttd,
      });
      var maxLgtd1 = topRight.lng;
      var maxLttd1 = topRight.lat;
      var minLgtd1 = bottomLeft.lng;
      var minLttd1 = bottomLeft.lat;
      var gra, pt, symbol;
      cancelRequest;
      getShgcListForMap(
        _self.userId,
        '6',
        _self.currentAdCode,
        '',
        '',
        '',
        '',
        '',
      ).then((res) => {
        if (res.data) {
          res.data.forEach((item) => {
            if (item.centerY && item.centerX) {
              pt = new Point(
                item.centerX,
                item.centerY,
                new SpatialReference(4326),
              );
              gra = new Graphic({
                geometry: pt,
                symbol: symbol,
                attributes: item,
              });
              szGraphicLayer.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
export function getAllSzList(_self) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var szGraphicLayerAll =
        _self.mapObject.findLayerById('szGraphicLayerAll');
      szGraphicLayerAll.removeAll();
      gateFlowWidthZoom.forEach((item) => {
        if (item.zoom == _self.viewObject.zoom) {
          gateFlow = item.gateFlow;
        }
      });
      var symbol = {
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/szAll.png',
        width: '12px',
        height: '12px',
      };
      var maxLgtd = _self.viewObject.extent.xmax;
      var maxLttd = _self.viewObject.extent.ymax;
      var minLgtd = _self.viewObject.extent.xmin;
      var minLttd = _self.viewObject.extent.ymin;
      var bottomLeft = _getLngLat({
        x: minLgtd,
        y: minLttd,
      });
      var topRight = _getLngLat({
        x: maxLgtd,
        y: maxLttd,
      });
      var maxLgtd1 = topRight.lng;
      var maxLttd1 = topRight.lat;
      var minLgtd1 = bottomLeft.lng;
      var minLttd1 = bottomLeft.lat;
      var displayLevel = _self.viewObject.zoom - 3;
      var gra, pt, symbol;
      cancelRequest;
      getAllSZList(
        _self.currentAdCode,
        maxLgtd1,
        maxLttd1,
        minLgtd1,
        minLttd1,
        gateFlow,
        displayLevel,
      ).then((res) => {
        if (res.data) {
          res.data.forEach((item) => {
            if (item.centerY && item.centerX) {
              pt = new Point(
                item.centerX,
                item.centerY,
                new SpatialReference(4326),
              );
              gra = new Graphic({
                geometry: pt,
                symbol: symbol,
                attributes: item,
              });
              szGraphicLayerAll.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
