"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"] = SBYXQK;
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _antd = require("antd");
var _ahooks = require("ahooks");
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 _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; }
// 1 h = 3600000 ms;
function SBYXQK(props) {
  var prefix = props.prefix,
    facilityId = props.facilityId,
    deviceId = props.deviceId;
  var _useSetState = (0, _ahooks.useSetState)({
      timeType: 0,
      totalMs: 24 * 60 * 60 * 1000,
      dataList: []
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var timeType = state.timeType,
    totalMs = state.totalMs,
    dataList = state.dataList;
  var legends = [{
    name: '备用',
    color: '#3688ff'
  }, {
    name: '工频',
    color: '#38bb35'
  }, {
    name: '变频',
    color: '#f9b31e'
  }, {
    name: '故障',
    color: '#f52117'
  }, {
    name: '未知',
    color: '#929292'
  }];
  var getStatusColor = function getStatusColor(statusChs) {
    var _a;
    return (_a = legends.find(function (item) {
      return item.name === statusChs;
    })) === null || _a === void 0 ? void 0 : _a.color;
  };
  var formatDuring = function formatDuring(mss) {
    var days = parseInt(String(mss / (1000 * 60 * 60 * 24)));
    var hours = parseInt(String(mss % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)));
    var minutes = parseInt(String(mss % (1000 * 60 * 60) / (1000 * 60)));
    var seconds = mss % (1000 * 60) / 1000;
    return (days ? days + 'day' : '') + (hours ? hours + 'h' : '') + (minutes ? minutes + 'min' : '') + (seconds ? seconds + 's' : '');
  };
  (0, _react.useEffect)(function () {
    _api.SbxlService.sbyxqk({
      facilityId: facilityId,
      deviceId: deviceId,
      startTime: (0, _moment["default"])().subtract(timeType, 'days').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
      endTime: (0, _moment["default"])().endOf('day').format('YYYY-MM-DD HH:mm:ss')
    }).then(function (data) {
      var dataList = [];
      var dataGroupObj = _lodash["default"].groupBy(data, 'name');
      var key;
      for (key in dataGroupObj) {
        dataList.push({
          name: key,
          values: dataGroupObj[key].map(function (item) {
            return Object.assign({
              width: Math.floor(item.duration / totalMs * 100) + '%',
              color: getStatusColor(item.statusChs)
            }, item);
          })
        });
      }
      setState({
        dataList: dataList
      });
    });
  }, [facilityId, deviceId, timeType]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-sbyxqk")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-title")
  }, /*#__PURE__*/_react["default"].createElement("span", null, "\u6C34\u6CF5\u8FD0\u884C\u60C5\u51B5"), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement("span", null, "\u65F6\u6BB5\uFF1A"), /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    style: {
      marginLeft: 10
    },
    options: [{
      label: '24小时',
      value: 0
    }, {
      label: '3天',
      value: 2
    }, {
      label: '7天',
      value: 6
    }],
    optionType: "button",
    value: timeType,
    onChange: function onChange(e) {
      var value = e.target.value;
      setState({
        timeType: value,
        totalMs: (value + 1) * 24 * 60 * 60 * 1000
      });
    }
  }))), (dataList === null || dataList === void 0 ? void 0 : dataList.length) ? (/*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-sbyxqk-legend")
  }, legends.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "-sbxl-main-top-sbyxqk-legend-items"),
      key: item.name
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(prefix, "-sbxl-main-top-sbyxqk-legend-items-text")
    }, item.name), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "-sbxl-main-top-sbyxqk-legend-items-icon"),
      style: {
        backgroundColor: item.color
      }
    }));
  })), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      width: '100%'
    }
  }, dataList.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      key: index,
      style: {
        width: '100%',
        display: 'flex',
        alignItems: 'center',
        marginTop: 10
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: 'inline-block',
        width: 50,
        textAlign: 'center'
      }
    }, item.name), /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: 'flex',
        alignItems: 'center',
        width: 'calc(100% - 50px)',
        height: 15,
        cursor: 'pointer'
      }
    }, item.values.map(function (value, valueIndex) {
      return /*#__PURE__*/_react["default"].createElement(_antd.Tooltip, {
        key: valueIndex,
        title: /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", null, "\u5F00\u59CB\u65F6\u95F4\uFF1A", value.startTime), /*#__PURE__*/_react["default"].createElement("div", null, "\u7ED3\u675F\u65F6\u95F4\uFF1A", value.endTime), /*#__PURE__*/_react["default"].createElement("div", null, value.statusChs, "\u65F6\u957F\uFF1A", formatDuring(value.duration)))
      }, /*#__PURE__*/_react["default"].createElement("div", {
        style: {
          width: value.width,
          height: '100%',
          display: 'inline-block',
          backgroundColor: value.color
        }
      }));
    })));
  })), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      width: 'calc(100% - 30px)',
      marginLeft: 30,
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'space-between',
      color: '#999999'
    }
  }, /*#__PURE__*/_react["default"].createElement("div", null, "00:00"), /*#__PURE__*/_react["default"].createElement("div", null, "11:59"), /*#__PURE__*/_react["default"].createElement("div", null, "23:59")))) : (/*#__PURE__*/_react["default"].createElement("div", {
    style: {
      height: 100,
      marginTop: 10
    }
  }, /*#__PURE__*/_react["default"].createElement(_vmNodata["default"], null))));
}
//# sourceMappingURL=SBYXQK.js.map
