"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"] = AlarmTotal;
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _antd = require("antd");
var _ahooks = require("ahooks");
var _echartsForReact = _interopRequireDefault(require("echarts-for-react"));
var _lodash = _interopRequireDefault(require("lodash"));
var _api = require("../../api");
var _moment = _interopRequireDefault(require("moment"));
var _vmNodata = _interopRequireDefault(require("../../../vm-nodata"));
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 _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; }
function AlarmTotal(props) {
  var prefix = props.prefix,
    id = props.id;
  var _useSetState = (0, _ahooks.useSetState)({
      timeType: 'day',
      totalData: []
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var timeType = state.timeType,
    totalData = state.totalData;
  var option = {
    tooltip: {
      trigger: 'item',
      confine: true,
      formatter: function formatter(params) {
        var _a, _b;
        //tongjia
        var unit = '次';
        var str = '';
        if (Array.isArray(params)) {
          str = "<div  style=\"text-align:left\">".concat((_a = params === null || params === void 0 ? void 0 : params[0]) === null || _a === void 0 ? void 0 : _a.axisValue, "<?div>");
          var color = (_b = params === null || params === void 0 ? void 0 : params[0].color) === null || _b === void 0 ? void 0 : _b.colorStops; // 获取系列的颜色
          params.map(function (item, index) {
            var _a;
            str += "<div  style=\"text-align:left\"><span style=\"color:".concat(item.color && typeof item.color == 'string' ? item.color : (_a = color === null || color === void 0 ? void 0 : color[index]) === null || _a === void 0 ? void 0 : _a.color, "\">\u2022</span> ").concat(item.seriesName, " &nbsp;&nbsp;&nbsp;").concat(item.value).concat(unit, "</div>");
          });
        } else {
          var _color = params === null || params === void 0 ? void 0 : params.color; // 获取系列的颜色
          str += "<div style=\"display:flex;align-items:center\"><span style=\"color:".concat(_color, ";font-size:24px \">\u2022</span>&nbsp;").concat(params === null || params === void 0 ? void 0 : params.name, "&nbsp;&nbsp;").concat(params.value).concat(unit, "</div>");
        }
        return str; // 返回带点的字符串
      }
    },
    legend: {
      top: 'center',
      left: '35%',
      type: 'scroll',
      orient: 'vertical',
      formatter: function formatter(name) {
        var _a;
        var obj = (_a = totalData === null || totalData === void 0 ? void 0 : totalData.find) === null || _a === void 0 ? void 0 : _a.call(totalData, function (item) {
          return item.name === name;
        });
        var total = _lodash["default"].sum(totalData.map(function (item) {
          return item.value;
        }));
        return "{a|".concat(name, "} {c|").concat((obj === null || obj === void 0 ? void 0 : obj.value) || 0, "\u6B21} {b|").concat(((obj === null || obj === void 0 ? void 0 : obj.value) / total * 100).toFixed(0), "%}");
        return "{a|".concat((name === null || name === void 0 ? void 0 : name.length) > 6 ? name.slice(0, 6) + '...' : name, "} {c|").concat((obj === null || obj === void 0 ? void 0 : obj.value) || 0, "\u6B21} {b|").concat(((obj === null || obj === void 0 ? void 0 : obj.value) / total * 100).toFixed(0), "%}");
      },
      textStyle: {
        color: '#000000',
        rich: {
          a: {
            width: 100
          },
          b: {
            width: 40,
            textAlign: 'right'
          },
          c: {
            width: 30,
            textAlign: 'right'
          }
        }
      }
    },
    series: [{
      name: '报警统计',
      type: 'pie',
      radius: ['45%', '75%'],
      center: ['15%', '50%'],
      avoidLabelOverlap: false,
      labelLine: {
        show: false
      },
      label: {
        show: false
      },
      data: totalData
    }]
  };
  (0, _react.useEffect)(function () {
    _api.AlarmService.alarmList({
      facilityIds: [id],
      alarmStartTimeBegin: (0, _moment["default"])().startOf(timeType).format('YYYY-MM-DD HH:mm:ss'),
      alarmStartTimeEnd: (0, _moment["default"])().endOf(timeType).format('YYYY-MM-DD HH:mm:ss'),
      dimension: 'alarmType',
      alarmTypes: ['FACTOR']
    }).then(function (data) {
      if (data) {
        var myGroup = _lodash["default"].groupBy(data, 'monitorItemName');
        var newDataList = [];
        var key;
        for (key in myGroup) {
          newDataList.push({
            name: key,
            value: myGroup[key].length
          });
        }
        setState({
          totalData: newDataList
        });
      }
    });
  }, [id, timeType]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-alarm-card")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-alarm-card-title")
  }, /*#__PURE__*/_react["default"].createElement("span", null, "\u62A5\u8B66\u7EDF\u8BA1"), /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    options: [{
      label: '日',
      value: 'day'
    }, {
      label: '月',
      value: 'month'
    }, {
      label: '年',
      value: 'year'
    }],
    value: timeType,
    onChange: function onChange(e) {
      setState({
        timeType: e.target.value
      });
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-alarm-card-content")
  }, totalData.length ? (/*#__PURE__*/_react["default"].createElement(_echartsForReact["default"], {
    option: option,
    notMerge: true,
    lazyUpdate: true,
    style: {
      height: 100,
      width: '100%'
    }
  })) : (/*#__PURE__*/_react["default"].createElement("div", {
    style: {
      height: 100,
      width: '100%'
    }
  }, /*#__PURE__*/_react["default"].createElement(_vmNodata["default"], null)))));
}
//# sourceMappingURL=AlarmTotal.js.map
