"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 _ToolsSelect = _interopRequireDefault(require("../_component/ToolsSelect"));
var _TipText = _interopRequireDefault(require("../_component/TipText"));
var _MyForm = _interopRequireDefault(require("../_component/MyForm"));
var _useProps2 = _interopRequireDefault(require("@vtx/ol-map/lib/use-props"));
var _FeatureLayer = _interopRequireDefault(require("@vtx/ol-map/lib/feature-layer/FeatureLayer"));
var _analysisLayer = require("@vtx/ol-map/lib/analysis-layer");
var _MyTabs = _interopRequireDefault(require("../_component/MyTabs"));
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 && {}.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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; } /*
 * @description: 碰撞分析
 */
var SelectResult = function SelectResult(props) {
  var _props$data = props.data,
    data = _props$data === void 0 ? [] : _props$data,
    onReset = props.onReset;
  var _useState = (0, _react.useState)('0'),
    _useState2 = _slicedToArray(_useState, 2),
    activeKey = _useState2[0],
    setActiveKey = _useState2[1];
  var formRef = (0, _react.useRef)([]);
  if (!(data !== null && data !== void 0 && data.length)) {
    return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null);
  }
  var onFinish = function onFinish() {
    var _props$onFinish;
    var values = [];
    formRef.current.map(function (form) {
      values.push(form.getFieldsValue());
    });
    props === null || props === void 0 || (_props$onFinish = props.onFinish) === null || _props$onFinish === void 0 || _props$onFinish.call(props, values);
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_MyTabs["default"], {
    tabsList: data.map(function (item, index) {
      return {
        label: item.code,
        key: String(index)
      };
    }),
    activeKey: activeKey,
    onChange: setActiveKey
  }), data.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement(_MyForm["default"], {
      style: {
        display: activeKey === String(index) ? 'block' : 'none'
      },
      key: index,
      ref: function ref(_ref) {
        return formRef.current[index] = _ref;
      },
      onReset: onReset,
      onFinish: onFinish,
      formData: item,
      labelCol: {
        flex: '140px'
      },
      items: [{
        label: '管道类别',
        type: 'blank',
        content: item.layerName
      }, {
        label: '起点经纬度',
        paramName: 'startPointLngLat',
        type: 'input',
        required: true
      }, {
        label: '终点经纬度',
        paramName: 'endPointLngLat',
        type: 'input',
        required: true
      }, {
        label: '起点管底高程(m)',
        paramName: 'startZ',
        type: 'number',
        required: true
      }, {
        label: '终点管底高程(m)',
        paramName: 'endZ',
        type: 'number',
        required: true
      }, {
        label: '管径(mm)',
        paramName: 'ds',
        type: 'number',
        required: true
      }]
    });
  }));
};
SelectResult.propTypes = {
  data: _propTypes["default"].array,
  onReset: _propTypes["default"].func,
  onFinish: _propTypes["default"].func
};
function Search() {
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map,
    highLayer = _useProps.highLayer,
    analysisLayer = _useProps.analysisLayer,
    onClose = _useProps.onClose,
    setVisible = _useProps.setVisible,
    setTableData = _useProps.setTableData;
  var _useState3 = (0, _react.useState)(''),
    _useState4 = _slicedToArray(_useState3, 2),
    activeBtn = _useState4[0],
    setActiveBtn = _useState4[1]; // 框选选择
  var _useState5 = (0, _react.useState)([]),
    _useState6 = _slicedToArray(_useState5, 2),
    selectData = _useState6[0],
    setSelectData = _useState6[1];
  var selectTools = (0, _react.useRef)('');
  var selectDataRef = (0, _react.useRef)([]);
  (0, _react.useEffect)(function () {
    highLayer.current = new _FeatureLayer["default"]({
      key: 'SelectLayer',
      viewOnly: true,
      layerControl: false,
      zIndex: 99,
      style: {
        type: 'Polyline',
        style: {
          fill: {
            color: '#ff4d4f',
            opacity: 0.2
          },
          stroke: {
            color: '#ff4d4f',
            opacity: 1,
            width: 4
          }
        }
      }
    });
    map.addLayer(highLayer.current);
    // 地图点击事件
    map.on('click', mapClick);
    return function () {
      map.removeLayer(highLayer.current);
      map.toolboxActive = false;
    };
  }, [map]);
  var mapClick = function mapClick(event) {
    if (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]);
          }
        });
      }
    }
  };
  var setSearchVal = function setSearchVal(feature) {
    var _analysisLayer$curren, _selectDataRef$curren, _selectDataRef$curren2;
    var layer = map.getLayerByFeature(feature);
    var data_json = analysisLayer === null || analysisLayer === void 0 || (_analysisLayer$curren = analysisLayer.current) === null || _analysisLayer$curren === void 0 ? void 0 : _analysisLayer$curren.getFeatureData(feature);
    data_json.layerName = layer === null || layer === void 0 ? void 0 : layer.name;
    selectDataRef.current.push(data_json);
    highLayer.current.getSource().addFeature(feature);
    if (((_selectDataRef$curren = selectDataRef.current) === null || _selectDataRef$curren === void 0 ? void 0 : _selectDataRef$curren.length) >= 2) {
      setSelectData(selectDataRef.current);
      closeSelect();
      return;
    }
    map.showPointTip("\u5DF2\u9009\u62E9".concat((_selectDataRef$curren2 = selectDataRef.current) === null || _selectDataRef$curren2 === void 0 ? void 0 : _selectDataRef$curren2.length, "\u6761\u7BA1\u7EBF"));
  };
  var closeSelect = function closeSelect() {
    setActiveBtn('');
    selectTools.current = '';
    map.toolboxActive = false;
  };
  var onCloseModal = function onCloseModal() {
    setVisible(false);
    onClose && onClose();
  };
  var onActive = function onActive(e) {
    if (selectTools.current) {
      return;
    }
    setTableData([]);
    highLayer.current.getSource().clear();
    selectDataRef.current = [];
    map.toolboxActive = true;
    selectTools.current = e;
    setActiveBtn(e);
    setSelectData([]);
    map.showPointTip('请选择两条管线');
  };
  var onReset = function onReset() {
    onActive('1');
  };
  var onFinish = function onFinish(data) {
    var _analysisLayer$curren3;
    var _data = _slicedToArray(data, 2),
      one = _data[0],
      two = _data[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(one.startPointLngLat.split(',')), [one.startZ]),
        p2: [].concat(_toConsumableArray(one.endPointLngLat.split(',')), [one.endZ]),
        q1: [].concat(_toConsumableArray(two.startPointLngLat.split(',')), [two.startZ]),
        q2: [].concat(_toConsumableArray(two.endPointLngLat.split(',')), [two.endZ])
      }),
      horizontalDistance = _analysisLayer$curren2.horizontalDistance,
      verticalDistance = _analysisLayer$curren2.verticalDistance;
    horizontalDistance = horizontalDistance ? Number(horizontalDistance - Number(two.ds) / 1000 / 2 - Number(one.ds) / 1000 / 2).toFixed(2) : 0;
    verticalDistance = verticalDistance ? Number(verticalDistance - Number(two.ds) / 1000 / 2 - Number(one.ds) / 1000 / 2).toFixed(2) : 0;
    var minDs = Math.min(Number(one.ds), Number(two.ds));
    setTableData([{
      key: 0,
      content: /*#__PURE__*/_react["default"].createElement(_TipText["default"], {
        text: "\u6C34\u5E73\u51C0\u8DDD",
        tip: "\u4E24\u6761(\u78B0\u649E)\u7A7A\u95F4\u76F8\u4EA4\u7684\u7BA1\u7EBF\u7684\u516C\u5782\u7EBF\u6BB5\u7684\u6C34\u5E73\u8DDD\u79BB"
      }),
      calcResult: horizontalDistance,
      standard: '管径≤200毫米，水平净距≥1.0米；管径≥200毫米，水平净距≥1.5米',
      analyzeResult: minDs <= 200 && horizontalDistance >= 1 || minDs >= 200 && horizontalDistance >= 1.5 ? '合格' : '不合格'
    }, {
      key: 1,
      content: /*#__PURE__*/_react["default"].createElement(_TipText["default"], {
        text: "\u5782\u76F4\u51C0\u8DDD",
        tip: "\u4E24\u6761(\u78B0\u649E)\u7A7A\u95F4\u76F8\u4EA4\u7684\u7BA1\u7EBF\u7684\u516C\u5782\u7EBF\u6BB5\u7684\u5782\u76F4\u8DDD\u79BB"
      }),
      calcResult: verticalDistance,
      standard: '垂直净距≥0.15米',
      analyzeResult: verticalDistance >= 0.15 ? '合格' : '不合格'
    }]);
    setVisible(true);
    map.fitView(highLayer.current);
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Modal["default"], {
    title: "\u51C0\u8DDD\u5206\u6790",
    onClose: onCloseModal,
    width: 400
  }, (selectData === null || selectData === void 0 ? void 0 : selectData.length) === 0 ? /*#__PURE__*/_react["default"].createElement(_ToolsSelect["default"], {
    title: "",
    value: activeBtn,
    onChange: onActive,
    options: [{
      label: '选择管线',
      key: '1',
      icon: 'icon-kuangxuan'
    }]
  }) : /*#__PURE__*/_react["default"].createElement(SelectResult, {
    data: selectData,
    onFinish: onFinish,
    onReset: onReset
  })));
}
//# sourceMappingURL=Search.js.map
