"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"] = AlarmRecord;
var _react = _interopRequireWildcard(require("react"));
var _ahooks = require("ahooks");
var _moment = _interopRequireDefault(require("moment"));
var _sumBy = _interopRequireDefault(require("lodash/sumBy"));
var _vmTable = _interopRequireDefault(require("../../../vm-table"));
var _vmSubTabs = require("../../../vm-sub-tabs");
var _vmSubTitle = _interopRequireDefault(require("../../../vm-sub-title"));
var _vmFilter = require("../../../vm-filter");
var _util = require("../../../util");
var _api = require("../../api");
var _useSettings2 = _interopRequireDefault(require("../../../vm-hooks/useSettings"));
var _level = _interopRequireDefault(require("../../imgs/level1.png"));
var _level2 = _interopRequireDefault(require("../../imgs/level2.png"));
var _level3 = _interopRequireDefault(require("../../imgs/level3.png"));
var _level4 = _interopRequireDefault(require("../../imgs/level4.png"));
var _level5 = _interopRequireDefault(require("../../imgs/level5.png"));
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 AlarmRecord(props) {
  var id = props.id;
  var _useSettings = (0, _useSettings2["default"])(),
    sDate = _useSettings.startDate,
    eDate = _useSettings.endDate;
  var _useSetState = (0, _ahooks.useSetState)({
      standardCode: 'all',
      alarmTabs: [{
        key: 'all',
        label: '全部'
      }],
      startDate: sDate || (0, _moment["default"])().format('YYYY-MM-DD'),
      endDate: eDate || (0, _moment["default"])().format('YYYY-MM-DD'),
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      },
      sort: '',
      order: ''
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var alarmTabs = state.alarmTabs,
    pagination = state.pagination,
    standardCode = state.standardCode,
    startDate = state.startDate,
    endDate = state.endDate,
    sort = state.sort,
    order = state.order;
  var _BaseService$useBaseA = _api.BaseService.useBaseAlarmTypes(id, "".concat(startDate, " 00:00:00"), "".concat(endDate, " 23:59:59")),
    alarmTs = _BaseService$useBaseA.alarmTypeRes;
  var _AlarmService$useAlar = _api.AlarmService.useAlarmRecord(id, standardCode === 'all' ? '' : standardCode, startDate, endDate, pagination.current, sort, order),
    detail = _AlarmService$useAlar.detail;
  // 格式化报警类型数据
  var formatAlarmType = function formatAlarmType(dataList) {
    var list = [];
    var totalCount = 0; // 报警类型总数
    totalCount = (0, _sumBy["default"])(dataList, function (o) {
      return o.count;
    });
    dataList.forEach(function (ele) {
      var obj = {
        key: ele.standardCode,
        label: "".concat(ele.standardName, " (").concat(ele.count, ")")
      };
      list.push(obj);
    });
    return {
      list: list,
      totalCount: totalCount
    };
  };
  (0, _react.useEffect)(function () {
    var arr = [{
      key: 'all',
      label: '全部'
    }]; // 报警明细标签
    if (alarmTs.length > 0) {
      var _formatAlarmType = formatAlarmType(alarmTs),
        list = _formatAlarmType.list,
        totalCount = _formatAlarmType.totalCount;
      arr[0].label = "\u5168\u90E8 (".concat(totalCount, ")");
      setState({
        alarmTabs: arr.concat(list)
      });
    }
  }, [alarmTs]);
  var getLevel = function getLevel(text) {
    var src = '',
      color = '#1FEFA9',
      name = '';
    switch (text) {
      case 1:
        src = _level["default"];
        name = '一';
        color = '#FF6A65';
        break;
      case 2:
        src = _level2["default"];
        name = '二';
        color = '#FFBD1E';
        break;
      case 3:
        src = _level3["default"];
        name = '三';
        color = '#E86CFF';
        break;
      case 4:
        src = _level4["default"];
        name = '四';
        color = '#00A8FF';
        break;
      case 5:
        src = _level5["default"];
        name = '五';
        color = '#1FEFA9';
        break;
      default:
        src = _level["default"];
        name = '一';
        color = '#FF6A65';
        break;
    }
    return {
      src: src,
      name: "\u7B49\u7EA7".concat(name),
      color: color
    };
  };
  var columns = [{
    title: '监测指标',
    dataIndex: 'standardName',
    key: 'standardName',
    ellipsis: true
  }, {
    title: '报警名称',
    dataIndex: 'name',
    key: 'name',
    ellipsis: true
  }, {
    title: '报警等级',
    dataIndex: 'level',
    key: 'level',
    render: function render(text) {
      var _getLevel = getLevel(text),
        src = _getLevel.src,
        name = _getLevel.name,
        color = _getLevel.color;
      return /*#__PURE__*/_react["default"].createElement("span", {
        className: "alarmLevel",
        style: {
          color: color
        }
      }, /*#__PURE__*/_react["default"].createElement("img", {
        src: src
      }), name);
    }
  }, {
    title: '报警开始时间',
    dataIndex: 'startDate',
    key: 'startDate',
    sorter: true
  }, {
    title: '报警结束时间',
    dataIndex: 'endDate',
    key: 'endDate',
    sorter: true
  }, {
    title: '报警时长',
    dataIndex: 'dur',
    key: 'dur',
    width: 80,
    sorter: function sorter(a, b) {
      return (0, _moment["default"])(a.endDate).diff((0, _moment["default"])(a.startDate)) - (0, _moment["default"])(b.endDate).diff((0, _moment["default"])(b.startDate));
    },
    render: function render(_text, record) {
      return (record === null || record === void 0 ? void 0 : record.startDate) && (record === null || record === void 0 ? void 0 : record.endDate) && (0, _util.secondToFormatTime)((0, _moment["default"])(record.endDate).diff((0, _moment["default"])(record.startDate)) / 1000);
    }
  }];
  var pageChange = function pageChange(page) {
    setState({
      pagination: Object.assign(Object.assign({}, pagination), {
        current: page
      })
    });
  };
  var handleTableChange = function handleTableChange(sorter) {
    sorter.columnKey !== 'dur' && setState({
      sort: sorter.columnKey,
      order: sorter.order === 'ascend' ? 'ASC' : 'DESC'
    });
  };
  var tableProps = {
    pagination: Object.assign(Object.assign({}, pagination), {
      total: detail.total,
      defaultPageSize: 10,
      onChange: pageChange
    }),
    rowKey: 'id',
    handleTableChange: handleTableChange,
    height: 160,
    dataSource: detail.dataSource
  };
  var handleDateChange = function handleDateChange(start, end) {
    setState({
      startDate: start,
      endDate: end
    });
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "alarm-record"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "alarm-record-head"
  }, /*#__PURE__*/_react["default"].createElement(_vmSubTitle["default"], {
    title: "\u62A5\u8B66\u8BB0\u5F55"
  }), /*#__PURE__*/_react["default"].createElement(_vmFilter.VMRangePicker, {
    value: [(0, _moment["default"])(startDate), (0, _moment["default"])(endDate)],
    onChange: function onChange(dates) {
      return dates && handleDateChange((0, _moment["default"])(dates[0]).format('YYYY-MM-DD'), (0, _moment["default"])(dates[1]).format('YYYY-MM-DD'));
    }
  })), /*#__PURE__*/_react["default"].createElement(_vmSubTabs.SubTabsLine, {
    items: alarmTabs,
    activeKey: standardCode,
    onChange: function onChange(key) {
      setState({
        standardCode: key
      });
    }
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "alarm-record-table"
  }, /*#__PURE__*/_react["default"].createElement(_vmTable["default"], Object.assign({
    columns: columns
  }, tableProps))));
}
//# sourceMappingURL=AlarmRecord.js.map
