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 desAnnWasuPop = ''; //设计年供水人口
var wintFlowWidthZoom = [
  {
    zoom: 0,
    desAnnWasuPop: 10,
  },
  {
    zoom: 1,
    desAnnWasuPop: 10,
  },
  {
    zoom: 2,
    desAnnWasuPop: 10,
  },
  {
    zoom: 3,
    desAnnWasuPop: 10,
  },
  {
    zoom: 4,
    desAnnWasuPop: 10,
  },
  {
    zoom: 5,
    desAnnWasuPop: 10,
  },
  {
    zoom: 6,
    desAnnWasuPop: 10,
  },
  {
    zoom: 7,
    desAnnWasuPop: 8,
  },
  {
    zoom: 8,
    desAnnWasuPop: 8,
  },
  {
    zoom: 9,
    desAnnWasuPop: 3,
  },
  {
    zoom: 10,
    desAnnWasuPop: '',
  },
  {
    zoom: 11,
    desAnnWasuPop: '',
  },
  {
    zoom: 12,
    desAnnWasuPop: '',
  },
  {
    zoom: 13,
    desAnnWasuPop: '',
  },
  {
    zoom: 14,
    desAnnWasuPop: '',
  },
  {
    zoom: 15,
    desAnnWasuPop: '',
  },
  {
    zoom: 16,
    desAnnWasuPop: '',
  },
  {
    zoom: 17,
    desAnnWasuPop: '',
  },
  {
    zoom: 18,
    desAnnWasuPop: '',
  },
];

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