"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"] = AlarmRecord;
var _react = _interopRequireWildcard(require("react"));
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 _api = require("../../api");
var _ahooks = require("ahooks");
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 _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 = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function AlarmRecord(props) {
  var id = props.id;
  var _useSetState = (0, _ahooks.useSetState)({
      alarmTypeCode: 'all',
      alarmTabs: [{
        key: '',
        label: '全部'
      }],
      startDate: (0, _moment["default"])().startOf('M').format('YYYY-MM-DD'),
      endDate: (0, _moment["default"])().format('YYYY-MM-DD'),
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      }
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var alarmTabs = state.alarmTabs,
    pagination = state.pagination,
    alarmTypeCode = state.alarmTypeCode,
    startDate = state.startDate,
    endDate = state.endDate;
  var _AlarmService$useAlar = _api.AlarmService.useAlarmTrend(id, startDate, endDate),
    alarmTypes = _AlarmService$useAlar.alarmTypes;
  var _AlarmService$useAlar2 = _api.AlarmService.useAlarmRecord(id, alarmTypeCode === 'all' ? '' : alarmTypeCode, startDate, endDate, pagination.current),
    detail = _AlarmService$useAlar2.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.alarmTypeCode,
        label: "".concat(ele.standardName, " (").concat(ele.count, ")")
      };
      list.push(obj);
    });
    return {
      list: list,
      totalCount: totalCount
    };
  };
  (0, _react.useEffect)(function () {
    var arr = [{
      key: '',
      label: '全部'
    }]; // 报警明细标签
    if ((alarmTypes === null || alarmTypes === void 0 ? void 0 : alarmTypes.length) > 0) {
      var _formatAlarmType = formatAlarmType(alarmTypes),
        list = _formatAlarmType.list,
        totalCount = _formatAlarmType.totalCount;
      arr[0].label = "\u5168\u90E8 (".concat(totalCount, ")");
      setState({
        alarmTabs: arr.concat(list)
      });
    }
  }, [alarmTypes]);
  var columns = [{
    title: '报警日期',
    dataIndex: 'startDate',
    key: 'startDate'
  }, {
    title: '报警垃圾类型',
    dataIndex: 'level',
    key: 'level'
  }, {
    title: '报警类型',
    dataIndex: 'name',
    key: 'name',
    ellipsis: true
  }];
  var pageChange = function pageChange(page) {
    setState({
      pagination: Object.assign(Object.assign({}, pagination), {
        current: page
      })
    });
  };
  var tableProps = {
    pagination: Object.assign(Object.assign({}, pagination), {
      total: detail.total,
      defaultPageSize: 10,
      onChange: pageChange
    }),
    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: alarmTypeCode,
    onChange: function onChange(key) {
      setState({
        alarmTypeCode: 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
