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

import axios from 'axios';

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

import _default, * as esriLoader from 'esri-loader';
import { getShgcListForMap } from '../../../../api/zhxxAPI.js';
import { _getLngLat } from '../mercatorToLngLat';
export function getNcysgcList(_self) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var ncysgcGraphicLayer =
        _self.mapObject.findLayerById('ncysgcGraphicLayer');
      ncysgcGraphicLayer.removeAll();
      desWasuScalWidthZoom.forEach((item) => {
        if (item.zoom == _self.viewObject.zoom) {
          desWasuScal = item.desWasuScal;
        }
      });
      var symbol = {
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/ncysgc.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,
        '21',
        _self.currentAdCode,
        maxLgtd1,
        maxLttd1,
        minLgtd1,
        minLttd1,
        desWasuScal,
      ).then((res) => {
        if (res.data) {
          res.data.forEach((item) => {
            if (item.cwsLat && item.cwsLong) {
              pt = new Point(
                item.cwsLong,
                item.cwsLat,
                new SpatialReference(4326),
              );
              gra = new Graphic({
                geometry: pt,
                symbol: symbol,
                attributes: item,
              });
              ncysgcGraphicLayer.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
