"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = Search;
var _react = _interopRequireWildcard(require("react"));
var _Modal = _interopRequireDefault(require("../_component/Modal"));
var _Input = _interopRequireDefault(require("../_component/Input"));
var _ToolsSelect = _interopRequireDefault(require("../_component/ToolsSelect"));
var _Observable = require("ol/Observable");
var _request = _interopRequireDefault(require("../_mapUtil/request"));
var _config = _interopRequireDefault(require("../_mapUtil/config"));
var _service = _interopRequireDefault(require("../_mapUtil/service"));
var _index = _interopRequireDefault(require("../_mapUtil/GetData/index"));
var _useProps2 = _interopRequireDefault(require("@vtx/ol-map/lib/use-props"));
var _featureDraw = _interopRequireDefault(require("@vtx/ol-map/lib/feature-draw"));
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function Search(props) {
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map,
    largeDiameterAnalysisRef = _useProps.largeDiameterAnalysisRef,
    layerRef = _useProps.layerRef,
    dsField = _useProps.dsField,
    setDsField = _useProps.setDsField,
    drawEndDsField = _useProps.drawEndDsField,
    setDrawEndDsField = _useProps.setDrawEndDsField,
    tableFilter = _useProps.tableFilter,
    setDrawEndToolsKey = _useProps.setDrawEndToolsKey,
    drawEndToolsKey = _useProps.drawEndToolsKey,
    setTableData = _useProps.setTableData,
    setResultVisible = _useProps.setResultVisible,
    _useProps$setLoading = _useProps.setLoading,
    setLoading = _useProps$setLoading === void 0 ? function () {} : _useProps$setLoading,
    _useProps$activeKey = _useProps.activeKey,
    activeKey = _useProps$activeKey === void 0 ? '' : _useProps$activeKey,
    _useProps$setLayerOpt = _useProps.setLayerOptions,
    setLayerOptions = _useProps$setLayerOpt === void 0 ? function () {} : _useProps$setLayerOpt,
    onClose = _useProps.onClose,
    setTableActive = _useProps.setTableActive,
    setTableFilter = _useProps.setTableFilter;
  var moveKey = (0, _react.useRef)();
  var _useState = (0, _react.useState)(''),
    _useState2 = _slicedToArray(_useState, 2),
    drawType = _useState2[0],
    setDrawType = _useState2[1];
  var _useState3 = (0, _react.useState)(''),
    _useState4 = _slicedToArray(_useState3, 2),
    toolsKey = _useState4[0],
    setToolsKey = _useState4[1];
  var _useState5 = (0, _react.useState)(null),
    _useState6 = _slicedToArray(_useState5, 2),
    feature = _useState6[0],
    setFeature = _useState6[1]; // 框选绘制feature
  var _useState7 = (0, _react.useState)([]),
    _useState8 = _slicedToArray(_useState7, 2),
    flatCoordinates = _useState8[0],
    setFlatCoordinates = _useState8[1];
  var _useState9 = (0, _react.useState)([]),
    _useState10 = _slicedToArray(_useState9, 2),
    data = _useState10[0],
    setData = _useState10[1];
  (0, _react.useEffect)(function () {
    var _keys = map.layers.getShowKeys() || [];
    var layerOptions = _service["default"].getLayerOption(map, _keys);
    setLayerOptions(layerOptions);
  }, [map]);
  var getDatas = function getDatas(data) {
    var _codes;
    var codes = [];
    setTableData([]);
    setResultVisible(true);
    // TODO 这边应该是写的活的
    if (activeKey == '1') {
      codes = _service["default"].getField(data, 'code', function (item) {
        return ['供水管线'].includes(item.label);
      });
    } else if (activeKey == '2') {
      codes = _service["default"].getField(data, 'code', function (item) {
        return [19].includes(item.feature);
      });
    } else if (activeKey == '3') {
      codes = _service["default"].getField(data, 'code', function (item) {
        return [10].includes(item.feature);
      });
    }
    if (!((_codes = codes) !== null && _codes !== void 0 && _codes.length)) return;
    setLoading(true);
    _request["default"].post("".concat(_config["default"].publicUrlZNFX, "/api/gisAnalyze/largeApertureAnalysis"), {
      body: {
        codeList: codes,
        type: activeKey
      }
    }).then(function (res) {
      var _res$data;
      setLoading(false);
      if ((res === null || res === void 0 ? void 0 : res.result) === 0 && res !== null && res !== void 0 && (_res$data = res.data) !== null && _res$data !== void 0 && _res$data.length) {
        setTableData(res.data || []);
        setResultVisible(true);
      } else {
        setTableData([]);
        setResultVisible(true);
      }
    });
  };
  (0, _react.useEffect)(function () {
    getDatas(data);
  }, [activeKey]);

  /**
   * @description: 图层高亮
   * @param {*} features
   * @return {*}
   */
  var highLayerFn = function highLayerFn(features) {
    layerRef.current.setHighlight(features);
  };
  (0, _react.useEffect)(function () {
    _service["default"].getOptions(map);
    if ((feature || flatCoordinates !== null && flatCoordinates !== void 0 && flatCoordinates.length) && drawEndToolsKey && tableFilter) {
      var _layerRef$current;
      layerRef === null || layerRef === void 0 || (_layerRef$current = layerRef.current) === null || _layerRef$current === void 0 || _layerRef$current.clearFeatures();
      (0, _index["default"])({
        map: map,
        feature: feature,
        drawType: drawEndToolsKey,
        keys: map.layers.getShowKeys(),
        flatCoordinates: flatCoordinates,
        highLayerFn: highLayerFn,
        filterBefore: function filterBefore(filter, layer) {
          var _filter = filter;
          var cqFilter = '';
          if (tableFilter) {
            cqFilter = tableFilter;
          }
          if ((layer === null || layer === void 0 ? void 0 : layer.name) === '供水管点') {
            if (filter) {
              _filter = "".concat(filter, " and feature in ('19','10')");
            } else {
              _filter = "feature in ('19','10')";
            }
          }
          if (cqFilter && _filter) {
            _filter = "".concat(_filter, " and ").concat(cqFilter);
          } else if (cqFilter) {
            _filter = cqFilter;
          } else if (_filter) {
            _filter = _filter;
          }
          return _filter;
        }
      }).then(function (res) {
        var _res$data2;
        setToolsKey('');
        setDrawType('');
        setTableData([]);
        setResultVisible(true);
        setData([]);
        if (res !== null && res !== void 0 && (_res$data2 = res.data) !== null && _res$data2 !== void 0 && _res$data2.length) {
          setData(res.data);
          getDatas(res.data);
        }
      });
    }
  }, [flatCoordinates, tableFilter]);
  var onDrawEnd = function onDrawEnd(feature) {
    setFeature(feature);
    var filter = '';
    if (drawEndDsField < 100) {
      setTableActive(7);
      filter = 'ds < 100';
    } else if (drawEndDsField < 200) {
      setTableActive(6);
      filter = 'ds >= 100 and ds < 200';
    } else if (drawEndDsField < 300) {
      setTableActive(5);
      filter = 'ds >= 200 and ds < 300';
    } else if (drawEndDsField < 400) {
      setTableActive(4);
      filter = 'ds >= 300 and ds < 400';
    } else if (drawEndDsField < 600) {
      setTableActive(3);
      filter = 'ds >= 400 and ds < 600';
    } else if (drawEndDsField < 800) {
      setTableActive(2);
      filter = 'ds >= 600 and ds < 800';
    } else if (drawEndDsField < 1000) {
      setTableActive(1);
      filter = 'ds >= 800 and ds < 1000';
    } else {
      setTableActive(0);
      filter = 'ds >= 1000';
    }
    setTableFilter('');
    setTimeout(function () {
      setTableFilter(filter);
    });
  };
  var onToolsChange = function onToolsChange(key) {
    try {
      map.hideTemplate();
    } catch (err) {}
    largeDiameterAnalysisRef.current.filter = '';
    moveKey.current && (0, _Observable.unByKey)(moveKey.current);
    if (key && key !== 'CurrentView') {
      setDrawType(key);
    } else if (key === 'CurrentView') {
      var _layerRef$current2;
      layerRef === null || layerRef === void 0 || (_layerRef$current2 = layerRef.current) === null || _layerRef$current2 === void 0 || _layerRef$current2.clearFeatures();
      setDrawType('');
      // 选择了当前范围
      setFlatCoordinates(_service["default"].getExtend(map));
      moveKey.current = map.on('moveend', function () {
        var extend = _service["default"].getExtend(map);
        setFlatCoordinates(extend);
      });
    }
    setToolsKey(key);
    setDrawEndToolsKey(key);
  };

  // 关闭
  var onCloseFn = function onCloseFn() {
    var _layerRef$current3;
    largeDiameterAnalysisRef.current.filter = '';
    moveKey.current && (0, _Observable.unByKey)(moveKey.current);
    setTableData([]);
    setResultVisible(false);
    layerRef === null || layerRef === void 0 || (_layerRef$current3 = layerRef.current) === null || _layerRef$current3 === void 0 || _layerRef$current3.clearFeatures();
    onClose && onClose();
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Modal["default"], {
    title: "\u5927\u53E3\u5F84\u5206\u6790",
    onClose: onCloseFn,
    width: 500
  }, /*#__PURE__*/_react["default"].createElement(_ToolsSelect["default"], {
    title: "",
    value: toolsKey,
    onChange: onToolsChange,
    options: _service["default"].btnList
  }), /*#__PURE__*/_react["default"].createElement(_Input["default"], {
    required: false,
    type: "number",
    title: "\u53E3\u5F84\u6700\u5C0F\u503C(mm)",
    value: dsField,
    onChange: function onChange(e) {
      setDsField(e);
      setDrawEndDsField(e);
    },
    width: 276,
    layout: "horizontal"
  }), /*#__PURE__*/_react["default"].createElement(_featureDraw["default"], {
    drawType: drawType,
    onDrawEnd: onDrawEnd
  })));
}
Search.propTypes = {
  show: _propTypes["default"].bool,
  onClose: _propTypes["default"].func,
  setLoading: _propTypes["default"].bool,
  onChange: _propTypes["default"].func,
  activeKey: _propTypes["default"].string,
  setLayerOptions: _propTypes["default"].func
};
//# sourceMappingURL=Search.js.map
