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 totCap = '';
var totCapWidthZoom = [
  {
    zoom: 0,
    totCap: 750,
  },
  {
    zoom: 1,
    totCap: 750,
  },
  {
    zoom: 2,
    totCap: 750,
  },
  {
    zoom: 3,
    totCap: 750,
  },
  {
    zoom: 4,
    totCap: 750,
  },
  {
    zoom: 5,
    totCap: 750,
  },
  {
    zoom: 6,
    totCap: 750,
  },
  {
    zoom: 7,
    totCap: 300,
  },
  {
    zoom: 8,
    totCap: 100,
  },
  {
    zoom: 9,
    totCap: '',
  },
  {
    zoom: 10,
    totCap: '',
  },
  {
    zoom: 11,
    totCap: '',
  },
  {
    zoom: 12,
    totCap: '',
  },
  {
    zoom: 13,
    totCap: '',
  },
  {
    zoom: 14,
    totCap: '',
  },
  {
    zoom: 15,
    totCap: '',
  },
  {
    zoom: 16,
    totCap: '',
  },
  {
    zoom: 17,
    totCap: '',
  },
  {
    zoom: 18,
    totCap: '',
  },
];

import _default, * as esriLoader from 'esri-loader';
import { getSkList } from '../../../api/zhxxAPI.js';
export function initAllSkData(_self) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var allSkGraphicLayer =
        _self.mapObject.findLayerById('allSkGraphicLayer');
      allSkGraphicLayer.removeAll();
      totCapWidthZoom.forEach((item) => {
        if (item.zoom == _self.viewObject.zoom) {
          totCap = item.totCap;
        }
      });
      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 obj = {
        persGuid: _self.userId,
        adCode: _self.currentAdCode,
        count: true,
        engScal: '',
        fields: '',
        maxLgtd: maxLgtd1,
        maxLttd: maxLttd1,
        minLgtd: minLgtd1,
        minLttd: minLttd1,
        offset: '',
        orderBy: '',
        pageNum: 0,
        pageSize: 0,
        rsName: '',
        status: '',
        // "totCap": totCap
        displayLevel: _self.viewObject.zoom - 3,
      };
      var symbol = {
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku_All.png',
        width: '12px',
        height: '12px',
      };
      var gra, pt;
      cancelRequest;
      getSkList(obj).then((res) => {
        if (res.data.list) {
          res.data.list.forEach((item) => {
            if (item.latitude && item.longitude) {
              pt = new Point(
                item.longitude,
                item.latitude,
                new SpatialReference(4326),
              );
              gra = new Graphic({
                geometry: pt,
                symbol: symbol,
                attributes: item,
              });
              allSkGraphicLayer.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
export function init2018SkData(_self) {
  //2018水库
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var skGraphicLayer2018 =
        _self.mapObject.findLayerById('skGraphicLayer2018');
      skGraphicLayer2018.removeAll();
      totCapWidthZoom.forEach((item) => {
        if (item.zoom == _self.viewObject.zoom) {
          totCap = item.totCap;
        }
      });
      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 obj = {
        persGuid: _self.userId,
        adCode: _self.currentAdCode,
        count: true,
        engScal: '',
        fields: '',
        maxLgtd: maxLgtd1,
        maxLttd: maxLttd1,
        minLgtd: minLgtd1,
        minLttd: minLttd1,
        offset: '',
        orderBy: '',
        pageNum: 0,
        pageSize: 0,
        rsName: '',
        status: '1',
        totCap: totCap,
        // "displayLevel":_self.viewObject.zoom-3
      };
      var symbol = {
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku_2018.png',
        width: '12px',
        height: '12px',
      };
      var gra, pt;
      getSkList(obj).then((res) => {
        if (res.data.list) {
          res.data.list.forEach((item) => {
            if (item.latitude && item.longitude) {
              pt = new Point(
                item.longitude,
                item.latitude,
                new SpatialReference(4326),
              );
              gra = new Graphic({
                geometry: pt,
                symbol: symbol,
                attributes: item,
              });
              skGraphicLayer2018.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
export function init2019SkData(_self) {
  //2019年
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var skGraphicLayer2019 =
        _self.mapObject.findLayerById('skGraphicLayer2019');
      skGraphicLayer2019.removeAll();
      var obj = {
        persGuid: _self.userId,
        adCode: _self.currentAdCode,
        count: true,
        engScal: '',
        fields: '',
        maxLgtd: '',
        maxLttd: '',
        minLgtd: '',
        minLttd: '',
        offset: '',
        orderBy: '',
        pageNum: 0,
        pageSize: 0,
        rsName: '',
        status: '2',
        totCap: 0,
      };
      var symbol2 = {
        //已督查
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku0.png',
        width: '12px',
        height: '12px',
      };
      var symbol1 = {
        //督查中
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku1.png',
        width: '12px',
        height: '12px',
      };
      var symbol0 = {
        //未督查
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku2.png',
        width: '12px',
        height: '12px',
      };
      var gra, pt, symbol;
      getSkList(obj).then((res) => {
        if (res.data.list) {
          res.data.list.forEach((item) => {
            if (item.latitude && item.longitude) {
              if (item.rgstrStatus == '1') {
                symbol = symbol1;
              } else if (item.rgstrStatus == '2') {
                symbol = symbol2;
              } else {
                symbol = symbol0;
              }
              pt = new Point(
                item.longitude,
                item.latitude,
                new SpatialReference(4326),
              );
              gra = new Graphic({
                geometry: pt,
                symbol: symbol,
                attributes: item,
              });
              skGraphicLayer2019.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
export function initFcSkData(_self) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var fcskGraphicLayer = _self.mapObject.findLayerById('fcskGraphicLayer');
      fcskGraphicLayer.removeAll();
      var obj = {
        persGuid: _self.userId,
        adCode: _self.currentAdCode,
        count: true,
        engScal: '',
        fields: '',
        maxLgtd: '',
        maxLttd: '',
        minLgtd: '',
        minLttd: '',
        offset: '',
        orderBy: '',
        pageNum: 0,
        pageSize: 0,
        rsName: '',
        status: '3',
        totCap: 0,
      };
      var symbol2 = {
        //已督查
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku0.png',
        width: '12px',
        height: '12px',
      };
      var symbol1 = {
        //督查中
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku1.png',
        width: '12px',
        height: '12px',
      };
      var symbol0 = {
        //未督查
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku2.png',
        width: '12px',
        height: '12px',
      };
      var gra, pt, symbol;
      getSkList(obj).then((res) => {
        if (res.data.list) {
          res.data.list.forEach((item) => {
            if (item.latitude && item.longitude) {
              if (item.rgstrStatus == '1') {
                symbol = symbol1;
              } else if (item.rgstrStatus == '2') {
                symbol = symbol2;
              } else {
                symbol = symbol0;
              }
              pt = new Point(
                item.longitude,
                item.latitude,
                new SpatialReference(4326),
              );
              gra = new Graphic({
                geometry: pt,
                symbol: symbol,
                attributes: item,
              });
              fcskGraphicLayer.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
export function initJhwSkData(_self) {
  esriLoader
    .loadModules(
      [
        'esri/Graphic',
        'esri/geometry/Point',
        'esri/geometry/SpatialReference',
        'dojo/domReady!',
      ],
      options,
    )
    .then(([Graphic, Point, SpatialReference]) => {
      var jhwskGraphicLayer =
        _self.mapObject.findLayerById('jhwskGraphicLayer');
      jhwskGraphicLayer.removeAll();
      totCapWidthZoom.forEach((item) => {
        if (item.zoom == _self.viewObject.zoom) {
          totCap = item.totCap;
        }
      });
      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 obj = {
        persGuid: _self.userId,
        adCode: _self.currentAdCode,
        count: true,
        engScal: '',
        fields: '',
        maxLgtd: maxLgtd1,
        maxLttd: maxLttd1,
        minLgtd: minLgtd1,
        minLttd: minLttd1,
        offset: '',
        orderBy: '',
        pageNum: 0,
        pageSize: 0,
        rsName: '',
        status: '4',
        // "totCap": totCap
        displayLevel: _self.viewObject.zoom - 3,
      };
      var symbol = {
        type: 'picture-marker',
        url: '/src/views/zongHeXinXiNew/assets/shuiku_jhw.png',
        width: '12px',
        height: '12px',
      };
      var gra, pt;
      getSkList(obj).then((res) => {
        if (res.data.list) {
          res.data.list.forEach((item) => {
            if (item.latitude && item.longitude) {
              pt = new Point(
                item.longitude,
                item.latitude,
                new SpatialReference(4326),
              );
              gra = new Graphic({
                geometry: pt,
                symbol: symbol,
                attributes: item,
              });
              jhwskGraphicLayer.add(gra);
            }
          });
        }
      });
    })
    .catch((err) => {
      console.error(err);
    });
}
