"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"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _antd = require("antd");
var _icons = require("@ant-design/icons");
var _FeatureLayer = _interopRequireDefault(require("../../../feature-layer/FeatureLayer"));
var _getSystemInfo = _interopRequireDefault(require("../../../_util/getSystemInfo"));
var _getUrlParam = _interopRequireDefault(require("../../../_util/getUrlParam"));
var _Draw = _interopRequireDefault(require("../../Utils/Draw"));
var _Table = _interopRequireDefault(require("../../components/Table"));
var _Modal = _interopRequireDefault(require("../../components/Modal"));
var _Feature = _interopRequireDefault(require("ol/Feature"));
var _geom = require("ol/geom");
var _location = _interopRequireDefault(require("../../../assets/location.png"));
var _Bookmark = _interopRequireDefault(require("./Bookmark"));
var _useProps2 = _interopRequireDefault(require("../../../use-props"));
var _classnames = _interopRequireDefault(require("classnames"));
require("./index.less");
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 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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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 _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 confirm = _antd.Modal.confirm;
var btnList = [{
  title: '新增',
  icon: /*#__PURE__*/_react["default"].createElement(_icons.PlusOutlined, null),
  color: 'rgb(73, 119, 233)'
}, {
  title: '删除',
  icon: /*#__PURE__*/_react["default"].createElement(_icons.DeleteOutlined, null),
  color: 'rgb(255,0,0)'
}, {
  title: '编辑',
  icon: /*#__PURE__*/_react["default"].createElement(_icons.FormOutlined, null),
  color: 'rgb(22, 184, 190)'
}];
function MapBookmark(props) {
  var onClose = props.onClose;
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map;
  var _Form$useForm = _antd.Form.useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    form = _Form$useForm2[0];
  var columns = [{
    title: '书签名',
    dataIndex: 'name',
    width: 80,
    render: function render(text, record) {
      return /*#__PURE__*/_react["default"].createElement("span", {
        onClick: function onClick() {
          return locatedLabel(record);
        },
        style: {
          cursor: 'pointer',
          color: '#00A8FF'
        }
      }, text);
    }
  }, {
    title: '内容',
    dataIndex: 'content',
    width: 160
  }];
  var userId = (0, _getUrlParam["default"])('userId') || (0, _getSystemInfo["default"])('userId') || '';
  var _useState = (0, _react.useState)(1),
    _useState2 = _slicedToArray(_useState, 2),
    pageIndex = _useState2[0],
    setPageIndex = _useState2[1];
  var _useState3 = (0, _react.useState)(5),
    _useState4 = _slicedToArray(_useState3, 2),
    pageSize = _useState4[0],
    setPageSize = _useState4[1];
  var _useState5 = (0, _react.useState)([]),
    _useState6 = _slicedToArray(_useState5, 2),
    tableData = _useState6[0],
    setTableData = _useState6[1];
  var _useState7 = (0, _react.useState)([]),
    _useState8 = _slicedToArray(_useState7, 2),
    selectedRowKeys = _useState8[0],
    setRowKeys = _useState8[1];
  var _useState9 = (0, _react.useState)(false),
    _useState10 = _slicedToArray(_useState9, 2),
    modalVisible = _useState10[0],
    setModalVisible = _useState10[1];
  var _useState11 = (0, _react.useState)(''),
    _useState12 = _slicedToArray(_useState11, 2),
    modalType = _useState12[0],
    setModalType = _useState12[1];
  var _useState13 = (0, _react.useState)(''),
    _useState14 = _slicedToArray(_useState13, 2),
    recordId = _useState14[0],
    setRecordId = _useState14[1];
  var _useState15 = (0, _react.useState)(0),
    _useState16 = _slicedToArray(_useState15, 2),
    total = _useState16[0],
    setTotal = _useState16[1];
  var draw = (0, _react.useRef)();
  var bookmarkRef = (0, _react.useRef)();
  var layerRef = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    bookmarkRef.current = new _Bookmark["default"]({
      userId: userId
    });
    var bookmarkChange = function bookmarkChange(tableData) {
      setTableData(tableData);
      setTotal(tableData.length);
      layerRef.current.loadData(tableData);
    };
    bookmarkRef.current.init().then(function (data) {
      if (data !== null && data !== void 0 && data.length) {
        setTableData(data);
        setTotal(data.length);
      }
      layerRef.current = new _FeatureLayer["default"]({
        data: data,
        key: 'LOCATION',
        show: true,
        style: {
          mode: 'Simple',
          render: {
            type: 'Marker',
            style: {
              image: _location["default"],
              scale: 1
            }
          }
        },
        labelField: 'name',
        zIndex: 100,
        viewOnly: true
      });
      map.addLayer(layerRef.current);
      bookmarkRef.current.on('change', bookmarkChange);
    });
    return function () {
      map.removeLayer(layerRef.current);
      bookmarkRef.current.un('change', bookmarkChange);
      draw.current && draw.current.destroy();
    };
  }, []);
  (0, _react.useEffect)(function () {
    addLabelMakrker();
  }, [tableData]);

  // 地图上添加标签
  var addLabelMakrker = function addLabelMakrker() {
    var features = [];
    tableData.map(function (v) {
      var _v$geometryInfo;
      if ((_v$geometryInfo = v.geometryInfo) !== null && _v$geometryInfo !== void 0 && _v$geometryInfo.lngLats) {
        var _v$geometryInfo2;
        var lngLat = (_v$geometryInfo2 = v.geometryInfo) === null || _v$geometryInfo2 === void 0 || (_v$geometryInfo2 = _v$geometryInfo2.lngLats) === null || _v$geometryInfo2 === void 0 ? void 0 : _v$geometryInfo2.split(',');
        if ((lngLat === null || lngLat === void 0 ? void 0 : lngLat.length) === 2) {
          var feature = new _Feature["default"](_objectSpread({
            geometry: new _geom.Point([Number(lngLat[0]), Number(lngLat[1])])
          }, v));
          features.push(feature);
        }
      }
    });
  };

  // 定位
  var locatedLabel = function locatedLabel(record) {
    var _record$geometryInfo;
    var lngLat = record === null || record === void 0 || (_record$geometryInfo = record.geometryInfo) === null || _record$geometryInfo === void 0 || (_record$geometryInfo = _record$geometryInfo.lngLats) === null || _record$geometryInfo === void 0 ? void 0 : _record$geometryInfo.split(',');
    if ((lngLat === null || lngLat === void 0 ? void 0 : lngLat.length) === 2) {
      map.getView().animate({
        zoom: 16,
        center: [lngLat[0], lngLat[1]]
      });
    }
  };

  // 编辑时 获取详情
  var getDetail = function getDetail(id) {
    var _tableData$filter, _record$geometryInfo2;
    var record = (_tableData$filter = tableData.filter(function (item) {
      return item.id == id;
    })) === null || _tableData$filter === void 0 ? void 0 : _tableData$filter[0];
    form.setFieldsValue({
      name: record === null || record === void 0 ? void 0 : record.name,
      lngLat: record === null || record === void 0 || (_record$geometryInfo2 = record.geometryInfo) === null || _record$geometryInfo2 === void 0 ? void 0 : _record$geometryInfo2.lngLats,
      content: record === null || record === void 0 ? void 0 : record.content
    });
    setModalVisible(true);
    setRecordId(id);
  };

  // 删除标签
  var deleteLabel = function deleteLabel(ids) {
    bookmarkRef.current.removeByIds(ids);
  };
  var modalShow = function modalShow(modalType) {
    if (modalType === '新增') {
      setModalVisible(true);
    } else if (modalType === '删除') {
      if (!selectedRowKeys.length) {
        return _antd.message.info('请先勾选', 2);
      } else {
        confirm({
          title: '确认删除书签',
          okText: '确认',
          okType: 'primary',
          cancelText: '取消',
          maskStyle: {
            zIndex: 1250
          },
          zIndex: 1250,
          style: {
            top: 10
          },
          onOk: function onOk() {
            deleteLabel(selectedRowKeys);
          }
        });
      }
    } else {
      if (!(selectedRowKeys && selectedRowKeys.length)) {
        return _antd.message.info('请先勾选', 2);
      } else if (selectedRowKeys.length > 1) {
        return _antd.message.info('请只勾选1个', 2);
      } else {
        getDetail(selectedRowKeys[0]);
      }
    }
    setModalType(modalType);
  };
  var onModalSave = function onModalSave(val) {
    var params = _objectSpread(_objectSpread({}, val), {}, {
      userId: userId,
      geometryInfo: {
        coordType: 'wgs84',
        type: 'point',
        lngLats: val.lngLat
      }
    });
    delete params.lngLat;
    if (modalType === '编辑' && recordId) {
      params.id = recordId;
      bookmarkRef.current.update(recordId, params);
    } else {
      bookmarkRef.current.add(params);
    }
    modalCancel();
  };
  var modalCancel = function modalCancel() {
    setModalVisible(false);
    setModalType('');
    form.resetFields();
    draw.current && draw.current.destroy();
  };
  var onSelectChange = function onSelectChange(keys) {
    setRowKeys(keys);
  };
  var rowSelection = {
    selectedRowKeys: selectedRowKeys,
    columnWidth: 30,
    onChange: onSelectChange
  };
  var onPageChange = function onPageChange(current, size) {
    setPageIndex(current);
    setPageSize(size);
  };
  var onFinish = function onFinish(val) {
    if (val) {
      onModalSave(val);
    }
  };
  var mapBookMarkerAdd = function mapBookMarkerAdd() {
    draw.current && draw.current.destroy();
    draw.current = new _Draw["default"]({
      map: map
    });
    draw.current.draw('Point', function (feature) {
      var _feature$getGeometry;
      var lngLat = feature === null || feature === void 0 || (_feature$getGeometry = feature.getGeometry()) === null || _feature$getGeometry === void 0 ? void 0 : _feature$getGeometry.getCoordinates();
      if (lngLat) {
        form.setFieldsValue({
          lngLat: "".concat(lngLat[0], ",").concat(lngLat[1])
        });
      }
    }, '请选择点位');
  };
  var addModal = function addModal() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])('wrap')
    }, /*#__PURE__*/_react["default"].createElement(_antd.Form, {
      form: form,
      name: "basic",
      labelCol: {
        span: 4
      },
      wrapperCol: {
        span: 18
      },
      initialValues: {},
      onFinish: onFinish
    }, /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
      label: "\u4E66\u7B7E\u540D",
      name: "name",
      rules: [{
        required: true,
        message: '请输入书签名'
      }],
      colon: true
    }, /*#__PURE__*/_react["default"].createElement(_antd.Input, {
      placeholder: "\u8BF7\u8F93\u5165\u4E66\u7B7E\u540D"
    })), /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        position: 'relative',
        margin: "20px 0"
      }
    }, /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
      label: "\u7ECF\u7EAC\u5EA6",
      name: "lngLat",
      labelCol: {
        span: 4
      },
      wrapperCol: {
        span: 15
      },
      rules: [{
        required: true,
        message: '请输入经纬度'
      }],
      colon: true
    }, /*#__PURE__*/_react["default"].createElement(_antd.Input, {
      placeholder: "\u8BF7\u8F93\u5165\u7ECF\u7EAC\u5EA6",
      name: "lngLat"
    })), /*#__PURE__*/_react["default"].createElement("a", {
      onClick: mapBookMarkerAdd,
      className: (0, _classnames["default"])('lngLat'),
      style: {
        flexShrink: '0',
        marginLeft: 10,
        marginBottom: 20
      }
    }, "\u9009\u62E9\u70B9\u4F4D")), /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
      label: "\u5185\u5BB9",
      name: "content",
      colon: true
    }, /*#__PURE__*/_react["default"].createElement(_antd.Input.TextArea, {
      placeholder: "\u8BF7\u8F93\u5165\u5185\u5BB9"
    })), /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        marginTop: 20
      }
    }, /*#__PURE__*/_react["default"].createElement(_antd.Form.Item, {
      wrapperCol: {
        offset: 8,
        span: 16
      }
    }, /*#__PURE__*/_react["default"].createElement(_antd.Button, {
      type: "primary",
      onClick: modalCancel,
      style: {
        marginRight: 20
      }
    }, "\u53D6\u6D88"), /*#__PURE__*/_react["default"].createElement(_antd.Button, {
      type: "primary",
      htmlType: "submit"
    }, "\u4FDD\u5B58")))));
  };
  var renderTable = function renderTable() {
    return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])('label')
    }, btnList.map(function (v, i) {
      return /*#__PURE__*/_react["default"].createElement("span", {
        className: (0, _classnames["default"])('labelAction'),
        key: i,
        onClick: function onClick() {
          return modalShow(v.title);
        },
        style: {
          cursor: 'pointer',
          marginRight: '12px'
        }
      }, v.icon && /*#__PURE__*/_react["default"].createElement("span", {
        style: {
          color: v.color,
          marginRight: 6
        }
      }, v.icon), v.title);
    })), /*#__PURE__*/_react["default"].createElement(_Table["default"], {
      columns: columns,
      dataSource: tableData,
      rowKey: "id",
      rowSelection: rowSelection,
      orderWidth: 40,
      bordered: true,
      pagination: {
        position: ['bottomRight'],
        size: 'small',
        showTotal: function showTotal(total) {
          return "\u5171 ".concat(total, " \u6761");
        },
        onChange: onPageChange,
        current: pageIndex,
        total: total,
        pageSize: pageSize
      }
    }));
  };
  return /*#__PURE__*/_react["default"].createElement(_Modal["default"], {
    title: "".concat(modalType || '', "\u5730\u56FE\u4E66\u7B7E"),
    onClose: onClose,
    width: 500
  }, modalVisible ? addModal() : renderTable());
}
var _default = exports["default"] = MapBookmark;
//# sourceMappingURL=index.js.map
