"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _VtxMap = require("@vtx/map/lib/VtxMap");

var _ahooks = require("ahooks");

var _moment = _interopRequireDefault(require("moment"));

var _vmFilter = require("../vm-filter");

var _vmInfoCollection = _interopRequireDefault(require("../vm-info-collection"));

var _vmChart = _interopRequireWildcard(require("../vm-chart"));

var _useSettings2 = _interopRequireDefault(require("../vm-hooks/useSettings"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _api = require("./api");

require("./style");

var _jquery = _interopRequireDefault(require("jquery"));

var _ECHARTS_COLOR;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }

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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

window.$ = _jquery["default"];
window.jQuery = _jquery["default"];
var ECHARTS_COLOR = (_ECHARTS_COLOR = {}, _defineProperty(_ECHARTS_COLOR, 'blue', '#6494F9'), _defineProperty(_ECHARTS_COLOR, 'yellow', '#F6BD16'), _defineProperty(_ECHARTS_COLOR, 'green', '#63DAAA'), _defineProperty(_ECHARTS_COLOR, 'red', '#FF6767'), _defineProperty(_ECHARTS_COLOR, 'orange', '#FF9844'), _defineProperty(_ECHARTS_COLOR, 'cyan_blue', '#6DC8EC'), _defineProperty(_ECHARTS_COLOR, 'purple', '#945FB9'), _defineProperty(_ECHARTS_COLOR, 'gray', '#AEAEAE'), _ECHARTS_COLOR);
var COLOR_COLLECTION = ['#6494F9', '#63DAAA', '#FF6767', '#AEAEAE'];

var BaseContent = function BaseContent(props) {
  var id = props.id;

  var _useSetState = (0, _ahooks.useSetState)({
    start: (0, _moment["default"])().subtract(7, 'd'),
    end: (0, _moment["default"])()
  }),
      _useSetState2 = _slicedToArray(_useSetState, 2),
      state = _useSetState2[0],
      setState = _useSetState2[1];

  var _BaseData$useBaseInfo = _api.BaseData.useBaseInfo(id),
      coords = _BaseData$useBaseInfo.coords,
      info = _BaseData$useBaseInfo.info;

  var handleDateChange = function handleDateChange(start, end) {
    setState({
      start: start,
      end: end
    });
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "community-base-content",
    style: {
      width: '100%',
      height: '100%'
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "left"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "map-container"
  }, coords && /*#__PURE__*/_react["default"].createElement(_VtxMap.VtxMap, {
    mapId: "view",
    mapCenter: coords,
    mapPoints: [{
      id: 'view',
      longitude: coords[0],
      latitude: coords[1]
    }]
  })), /*#__PURE__*/_react["default"].createElement(_vmInfoCollection["default"], {
    data: info
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "right"
  }, /*#__PURE__*/_react["default"].createElement(_vmFilter.VMDatePicker, {
    value: [state.start, state.end],
    onChange: function onChange(dates) {
      return dates && handleDateChange(dates[0], dates[1]);
    },
    style: {
      position: 'absolute',
      top: 24,
      right: 32
    }
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "chart-container"
  }, /*#__PURE__*/_react["default"].createElement(_vmChart.ChartTitle, {
    title: "\u6295\u653E\u60C5\u51B5\u7EDF\u8BA1"
  }), /*#__PURE__*/_react["default"].createElement(StatChart, {
    start: state.start.format('YYYY-MM-DD'),
    end: state.end.format('YYYY-MM-DD'),
    id: id
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "chart-container"
  }, /*#__PURE__*/_react["default"].createElement(_vmChart.ChartTitle, {
    title: "\u6295\u653E\u91CF\u8D8B\u52BF\u5206\u6790"
  }), /*#__PURE__*/_react["default"].createElement(TrendChart, {
    start: state.start.format('YYYY-MM-DD'),
    end: state.end.format('YYYY-MM-DD'),
    id: id
  }))));
};

BaseContent.propTypes = {
  id: _propTypes["default"].string
};

var StatChart = function StatChart(props) {
  var id = props.id,
      start = props.start,
      end = props.end;
  var data = _api.BaseData.useBaseProdStat(id, start, end) || [];

  var _useSettings = (0, _useSettings2["default"])(),
      isDark = _useSettings.isDark;

  var total = (0, _react.useMemo)(function () {
    var result = 0;

    var _iterator = _createForOfIteratorHelper(data),
        _step;

    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var item = _step.value;
        result += item.garbageWeight;
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }

    return result;
  }, [data]);
  var chartData = data.map(function (item, index) {
    return {
      value: item.garbageWeight,
      name: item.garbageType,
      itemStyle: {
        color: item.garbageColor ? ECHARTS_COLOR[item.garbageColor] : COLOR_COLLECTION[index]
      }
    };
  });
  var option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      icon: 'circle',
      orient: 'vertical',
      top: '35%',
      left: '65%',
      itemHeight: 8,
      itemWidth: 8,
      itemGap: 14,
      textStyle: {
        fontSize: 14,
        fontFamily: 'PingFangSC-Regular',
        rich: {
          a: {
            color: isDark ? 'white' : '#666'
          },
          b: {
            color: isDark ? 'white' : '#222'
          }
        }
      },
      formatter: function formatter(name) {
        var _data$find;

        var num = ((_data$find = data.find(function (i) {
          return i.garbageType === name;
        })) === null || _data$find === void 0 ? void 0 : _data$find.garbageWeight) || 0;
        return "{a|".concat(name, "}      {b|").concat(num, "\u5428}");
      }
    },
    graphic: [{
      type: 'group',
      left: '40%',
      top: '50%',
      bounding: 'raw',
      children: [{
        type: 'text',
        style: {
          text: '投放总量',
          fontFamily: 'PingFangSC-Regular',
          fontSize: 14,
          fill: '#858A91',
          align: 'center',
          y: -20
        }
      }, {
        type: 'text',
        style: {
          text: total + '吨',
          fontWeight: 'bold',
          fontSize: 20,
          fill: isDark ? 'white' : '#222',
          fontFamily: 'DIN-Regular',
          align: 'center',
          y: 10
        }
      }]
    }],
    series: [{
      name: '投放情况统计',
      type: 'pie',
      radius: ['45%', '70%'],
      center: ['40%', '50%'],
      label: {
        show: false
      },
      labelLine: {
        show: false
      },
      data: chartData
    }]
  };
  return /*#__PURE__*/_react["default"].createElement(_vmChart["default"], {
    option: option,
    height: 260,
    width: "100%"
  });
};

StatChart.propTypes = {
  id: _propTypes["default"].string,
  start: _propTypes["default"].string,
  end: _propTypes["default"].string
};

var TrendChart = function TrendChart(props) {
  var id = props.id,
      start = props.start,
      end = props.end;
  var data = _api.BaseData.useBaseProdTrend(id, start, end) || [];
  var xAxisData = [];
  var firstItem = data[0];
  var legendData = (firstItem === null || firstItem === void 0 ? void 0 : firstItem.throwProductionDTOList.map(function (i) {
    return i.garbageType;
  })) || [];

  var getSeries = function getSeries() {
    if (!firstItem) return [];
    var typeCount = firstItem.throwProductionDTOList.length;
    var series = [];

    for (var i = 0; i < typeCount; i++) {
      var item = firstItem.throwProductionDTOList[i];
      series.push({
        name: item.garbageType,
        type: 'line',
        itemStyle: {
          color: item.garbageColor ? ECHARTS_COLOR[item.garbageColor] : COLOR_COLLECTION[i]
        },
        data: []
      });
    }

    var _iterator2 = _createForOfIteratorHelper(data),
        _step2;

    try {
      for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
        var _item = _step2.value;
        xAxisData.push(_item.date);

        for (var _i2 = 0; _i2 < typeCount; _i2++) {
          series[_i2].data.push(_item.throwProductionDTOList[_i2].garbageWeight);
        }
      }
    } catch (err) {
      _iterator2.e(err);
    } finally {
      _iterator2.f();
    }

    return series;
  };

  var option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: legendData
    },
    grid: {
      left: '5%',
      right: '5%',
      bottom: '10%'
    },
    xAxis: {
      data: xAxisData
    },
    yAxis: {
      name: '(吨)'
    },
    series: getSeries()
  };
  return /*#__PURE__*/_react["default"].createElement(_vmChart["default"], {
    option: option,
    height: 260,
    width: "100%"
  });
};

TrendChart.propTypes = {
  id: _propTypes["default"].string,
  start: _propTypes["default"].string,
  end: _propTypes["default"].string
};
var _default = BaseContent;
exports["default"] = _default;
//# sourceMappingURL=base.js.map
