"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"] = SBXLFX;
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _antd = require("antd");
var _ahooks = require("ahooks");
var _lodash = _interopRequireDefault(require("lodash"));
var _echartsForReact = _interopRequireDefault(require("echarts-for-react"));
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 _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, 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; }
function SBXLFX(props) {
  var prefix = props.prefix,
    facilityId = props.facilityId,
    deviceId = props.deviceId;
  var _useSetState = (0, _ahooks.useSetState)({
      timeType: 'day',
      outData: [],
      innerData: []
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var timeType = state.timeType,
    innerData = state.innerData,
    outData = state.outData;
  function formatDuration(mss) {
    var days = (mss / (1000 * 60 * 60 * 24)).toFixed(0);
    var hours = (mss % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)).toFixed(0);
    var minutes = (mss % (1000 * 60 * 60) / (1000 * 60)).toFixed(0);
    var seconds = (mss % (1000 * 60) / 1000).toFixed(0);
    return "".concat(days != '0' ? days + 'day' : '').concat(hours != '0' ? hours + 'h' : '').concat(minutes != '0' ? minutes + 'min' : '').concat(seconds != '0' ? seconds + 's' : '');
  }
  var colorList = ['#A0D2FF', '#A0B0FF', '#FAAD14', '#FFD685', '#90F6DE', '#52C41A', '#1890FF', '#FAAD14', '#52C41A'];
  var option = {
    tooltip: {
      trigger: 'item',
      formatter: function formatter(params) {
        var innerSum = _lodash["default"].sum(innerData.map(function (item) {
          return item.value;
        }));
        var outSum = _lodash["default"].sum(outData.map(function (item) {
          return item.value;
        }));
        if (params.componentIndex === 0) {
          return params.marker + params.name + '<br/>' + '累计运行时长：' + formatDuration(params.value) + '<br />' + '占比：' + (params.value / innerSum * 100).toFixed(2) + '%';
        }
        if (params.componentIndex === 1) {
          return params.marker + params.name + '  ' + formatDuration(params.value) + '   ' + (params.value / outSum * 100).toFixed(2) + '%';
        }
      }
    },
    legend: {
      top: 'center',
      left: '60%',
      type: 'scroll',
      orient: 'vertical',
      textStyle: {
        color: '#000000',
        rich: {
          a: {
            width: 50
          }
        }
      }
    },
    series: [{
      name: '',
      type: 'pie',
      radius: [0, '30%'],
      center: ['30%', '50%'],
      avoidLabelOverlap: false,
      labelLine: {
        show: false
      },
      label: {
        show: false
      },
      data: innerData,
      itemStyle: {
        normal: {
          color: function color(colors) {
            return colorList[colors.dataIndex];
          }
        }
      }
    }, {
      name: '',
      type: 'pie',
      radius: ['30%', '60%'],
      center: ['30%', '50%'],
      avoidLabelOverlap: false,
      labelLine: {
        show: false
      },
      label: {
        show: false
      },
      data: outData,
      itemStyle: {
        normal: {
          color: function color(colors) {
            return colorList[colors.dataIndex + 1];
          }
        }
      }
    }]
  };
  (0, _react.useEffect)(function () {
    _api.SbxlService.sbxlfx({
      facilityId: facilityId,
      deviceId: deviceId,
      startTime: (0, _moment["default"])().startOf(timeType).format('YYYY-MM-DD HH:mm:ss'),
      endTime: (0, _moment["default"])().endOf(timeType).format('YYYY-MM-DD HH:mm:ss')
    }).then(function (data) {
      var innerDataObj = _lodash["default"].groupBy(data, 'name');
      var innerData = [];
      for (var key in innerDataObj) {
        innerData.push({
          name: key,
          value: _lodash["default"].sum(innerDataObj[key].map(function (item) {
            return item.duration;
          }))
        });
      }
      setState({
        outData: data.map(function (item) {
          return {
            name: "".concat(item.name).concat(item.typeChs),
            value: item.duration
          };
        }),
        innerData: innerData
      });
    });
  }, [facilityId, deviceId, timeType]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-sbxlfx")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-title")
  }, /*#__PURE__*/_react["default"].createElement("span", null, "\u6C34\u6CF5\u6548\u7387\u5206\u6790"), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement("span", null, "\u65F6\u95F4\uFF1A"), /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    style: {
      marginLeft: 10
    },
    options: [{
      label: '当天',
      value: 'day'
    }, {
      label: '当月',
      value: 'month'
    }, {
      label: '当年',
      value: 'year'
    }],
    optionType: "button",
    value: timeType,
    onChange: function onChange(e) {
      setState({
        timeType: e.target.value
      });
    }
  }))), /*#__PURE__*/_react["default"].createElement("div", null, (outData === null || outData === void 0 ? void 0 : outData.length) ? (/*#__PURE__*/_react["default"].createElement(_echartsForReact["default"], {
    option: option,
    notMerge: true,
    lazyUpdate: true,
    style: {
      height: 250,
      width: '100%'
    }
  })) : (/*#__PURE__*/_react["default"].createElement("div", {
    style: {
      height: 100,
      marginTop: 10
    }
  }, /*#__PURE__*/_react["default"].createElement(_vmNodata["default"], null)))));
}
//# sourceMappingURL=SBXLFX.js.map
