"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 _message = _interopRequireDefault(require("antd/lib/message"));
var _Modal = _interopRequireDefault(require("../_component/Modal"));
var _ToolsSelect = _interopRequireDefault(require("../_component/ToolsSelect"));
var _MyForm = _interopRequireDefault(require("../_component/MyForm"));
var _featureDraw = _interopRequireDefault(require("@vtx/ol-map/lib/feature-draw"));
var _useProps2 = _interopRequireDefault(require("@vtx/ol-map/lib/use-props"));
var _analysisLayer = require("@vtx/ol-map/lib/analysis-layer");
var _HighLayer = _interopRequireDefault(require("@vtx/ol-map/lib/high-layer/HighLayer"));
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; } /*
 * @description: 碰撞分析
 */
function Search(props) {
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map,
    highLayer = _useProps.highLayer,
    analysisLayer = _useProps.analysisLayer,
    onClose = _useProps.onClose,
    setVisible = _useProps.setVisible,
    setTableData = _useProps.setTableData,
    setLoading = _useProps.setLoading;
  var formRef = (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),
    activeBtn = _useState4[0],
    setActiveBtn = _useState4[1]; // 框选选择
  var _useState5 = (0, _react.useState)({}),
    _useState6 = _slicedToArray(_useState5, 2),
    formVal = _useState6[0],
    setFormVal = _useState6[1];
  var selectTools = (0, _react.useRef)('');
  (0, _react.useEffect)(function () {
    highLayer.current = new _HighLayer["default"]({
      map: map
    });
    // 地图点击事件
    map.on('click', mapClick);
    return function () {
      highLayer.current && highLayer.current.destroy();
    };
  }, [map]);
  var mapClick = function mapClick(event) {
    if (selectTools.current === '1') {
      setActiveBtn('');
      selectTools.current = '';
      var layers = map.getVisibleLayersByKeys(map.layers.getShowKeys());
      if (layers !== null && layers !== void 0 && layers.length) {
        (0, _analysisLayer.getWMSFeatureInfoByLayers)(event, map, layers).then(function (features) {
          if (features !== null && features !== void 0 && features.length) {
            setSearchVal(features[0], selectTools.current);
          }
        });
      }
    }
  };
  var setSearchVal = function setSearchVal(feature, key) {
    var _analysisLayer$curren;
    var data_json = analysisLayer === null || analysisLayer === void 0 || (_analysisLayer$curren = analysisLayer.current) === null || _analysisLayer$curren === void 0 ? void 0 : _analysisLayer$curren.getFeatureData(feature, key, function () {
      selectTools.current = '';
    });
    highLayer.current.setHighlight(feature);
    setFormVal(data_json);
    formRef.current.setFieldsValue(data_json);
  };
  var onCloseModal = function onCloseModal() {
    setVisible(false);
    clearDraw();
    onClose && onClose();
  };
  var onDrawEnd = function onDrawEnd(feature) {
    setActiveBtn('');
    setSearchVal(feature, selectTools.current);
  };
  var onSearch = function onSearch(val) {
    var p1Z = val.start_z,
      p2Z = val.end_z,
      o_ds = val.ds;
    var layers = map.getWmsVisibleLayers(map.layers.getShowKeys());
    var code = formVal.code,
      geometry = formVal.geometry;
    var _geometry$getCoordina = geometry.getCoordinates(),
      _geometry$getCoordina2 = _slicedToArray(_geometry$getCoordina, 2),
      p1 = _geometry$getCoordina2[0],
      p2 = _geometry$getCoordina2[1];
    var coor = "".concat(formVal.startPointLngLat.split(',').join(' '), ",").concat(formVal.endPointLngLat.split(',').join(' '));
    var cqFilter = "CROSSES(geo_location, LINESTRING(".concat(coor, "))");
    if (code) {
      var _code$split = code.split('_'),
        _code$split2 = _slicedToArray(_code$split, 2),
        startCode = _code$split2[0],
        endCode = _code$split2[1];
      cqFilter = "".concat(cqFilter, " and code not like '%").concat(startCode, "%' and code not like '%").concat(endCode, "%'");
    }
    setVisible(true);
    setTableData([]);
    setLoading(true);
    (0, _analysisLayer.getWFSFeatureInfoByLayers)(layers, cqFilter).then(function (features) {
      var _tableData = [];
      if (features !== null && features !== void 0 && features.length) {
        features.map(function (v) {
          var _analysisLayer$curren3;
          var _v$values_ = v.values_,
            data_json = _v$values_.data_json,
            code = _v$values_.code;
          var _ref = data_json ? JSON.parse(v.values_.data_json) : {},
            ds = _ref.ds,
            q1Z = _ref.startZ,
            q2Z = _ref.endZ;
          var _v$values_$geometry$g = v.values_.geometry.getCoordinates(),
            _v$values_$geometry$g2 = _slicedToArray(_v$values_$geometry$g, 2),
            q1 = _v$values_$geometry$g2[0],
            q2 = _v$values_$geometry$g2[1];
          var _analysisLayer$curren2 = analysisLayer === null || analysisLayer === void 0 || (_analysisLayer$curren3 = analysisLayer.current) === null || _analysisLayer$curren3 === void 0 ? void 0 : _analysisLayer$curren3.calculateDistances({
              p1: [].concat(_toConsumableArray(p1), [p1Z]),
              p2: [].concat(_toConsumableArray(p2), [p2Z]),
              q1: [].concat(_toConsumableArray(q1), [q1Z]),
              q2: [].concat(_toConsumableArray(q2), [q2Z])
            }),
            horizontalDistance = _analysisLayer$curren2.horizontalDistance,
            verticalDistance = _analysisLayer$curren2.verticalDistance;
          horizontalDistance = Number(horizontalDistance - ds / 1000 / 2 - o_ds / 1000 / 2).toFixed(2);
          verticalDistance = Number(verticalDistance - ds / 1000 / 2 - o_ds / 1000 / 2).toFixed(2);
          _tableData.push(_objectSpread(_objectSpread({}, v), {}, {
            code: code,
            type: v.name,
            horizontalDistance: horizontalDistance,
            verticalDistance: verticalDistance
          }));
        });
      } else {
        _message["default"].warning('不存在与所选管线/绘制管线碰撞的管线!');
      }
      features.push(formVal.feature);
      highLayer.current.setHighlight(features);
      setTableData(_tableData);
      setLoading(false);
    })["catch"](function () {
      setLoading(false);
    });
  };
  var clearDraw = function clearDraw() {
    try {
      map.hideTemplate();
    } catch (err) {}
    highLayer === null || highLayer === void 0 || highLayer.current.clearFeatures();
    formRef.current.resetFields();
    setFormVal({});
    setTableData([]);
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Modal["default"], {
    title: "\u78B0\u649E\u5206\u6790",
    onClose: onCloseModal,
    width: 400
  }, /*#__PURE__*/_react["default"].createElement(_ToolsSelect["default"], {
    title: "",
    value: activeBtn,
    onChange: function onChange(e) {
      selectTools.current = e;
      setActiveBtn(e);
      clearDraw();
      setDrawType('');
      if (e === '2') {
        setDrawType('LineString');
      }
    },
    options: [{
      label: '选择管线',
      key: '1',
      icon: 'icon-kuangxuan'
    }, {
      label: '绘制管线',
      key: '2',
      icon: 'icon-duobianxingxuanze'
    }]
  }), /*#__PURE__*/_react["default"].createElement(_featureDraw["default"], {
    drawType: drawType,
    onDrawEnd: onDrawEnd,
    onlyDraw: true,
    maxPoints: 2
  }), /*#__PURE__*/_react["default"].createElement(_MyForm["default"], {
    ref: formRef,
    onFinish: onSearch,
    labelCol: {
      flex: '140px'
    },
    items: [{
      label: '起点经纬度',
      paramName: 'startPointLngLat',
      type: 'input',
      required: true
    }, {
      label: '终点经纬度',
      paramName: 'endPointLngLat',
      type: 'input',
      required: true
    }, {
      label: '起点管底高程(m)',
      paramName: 'start_z',
      type: 'number',
      required: true
    }, {
      label: '终点管底高程(m)',
      paramName: 'end_z',
      type: 'number',
      required: true
    }, {
      label: '管径(mm)',
      paramName: 'ds',
      type: 'number',
      required: true
    }]
  })));
}
//# sourceMappingURL=Search.js.map
