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 wintFlow = ''; //取水流量
var wintFlowWidthZoom = [
  {
    zoom: 0,
    wintFlow: 16,
  },
  {
    zoom: 1,
    wintFlow: 16,
  },
  {
    zoom: 2,
    wintFlow: 16,
  },
  {
    zoom: 3,
    wintFlow: 16,
  },
  {
    zoom: 4,
    wintFlow: 16,
  },
  {
    zoom: 5,
    wintFlow: 16,
  },
  {
    zoom: 6,
    wintFlow: 11,
  },
  {
    zoom: 7,
    wintFlow: 10,
  },
  {
    zoom: 8,
    wintFlow: 5,
  },
  {
    zoom: 9,
    wintFlow: 3,
  },
  {
    zoom: 10,
    wintFlow: 2,
  },
  {
    zoom: 11,
    wintFlow: 1,
  },
  {
    zoom: 12,
    wintFlow: '',
  },
  {
    zoom: 13,
    wintFlow: '',
  },
  {
    zoom: 14,
    wintFlow: '',
  },
  {
    zoom: 15,
    wintFlow: '',
  },
  {
    zoom: 16,
    wintFlow: '',
  },
  {
    zoom: 17,
    wintFlow: '',
  },
  {
    zoom: 18,
    wintFlow: '',
  },
];

import _default, * as esriLoader from 'esri-loader';
import { getAllQskList } from '../../../api/zhxxAPI.js';
export function getQskList(_self) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var graphicLayerQsk = _self.mapObject.findLayerById('graphicLayerQsk');
      graphicLayerQsk.removeAll();
      wintFlowWidthZoom.forEach((item) => {
        if (item.zoom == _self.viewObject.zoom) {
          wintFlow = item.wintFlow;
        }
      });
      var symbol = {
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/qsk.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;
      getAllQskList(
        _self.currentAdCode,
        maxLgtd1,
        maxLttd1,
        minLgtd1,
        minLttd1,
        wintFlow,
        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,
              });
              graphicLayerQsk.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
