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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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(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(arr) { if (Array.isArray(arr)) return arr; }
import TabsList from "./TabsList";
import { useSetState } from 'ahooks';
import { Checkbox, Col, Row, Space } from 'antd';
import classnames from 'classnames';
import dayjs from 'dayjs';
import { keyBy } from 'lodash';
import { useState } from 'react';
import { VmTable, VmTag, VmWrapper } from "../../../_components";
import useSettings from "../../../_hooks/useSettings";
import { BaseService } from "../../api";
import "./index.less";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var tabList = [{
  label: '日',
  value: 'day'
}, {
  label: '月',
  value: 'month'
}, {
  label: '年',
  value: 'year'
}, {
  label: '自定义',
  value: 'self'
}];
var filterTypeMap = {
  day: 'datePicker',
  month: 'monthPicker',
  year: 'yearPicker',
  self: 'rangePicker'
};
var AlarmAnalysis = function AlarmAnalysis(_ref) {
  var id = _ref.id,
    info = _ref.info;
  var _useSettings = useSettings(),
    isDark = _useSettings.isDark;
  var _useSetState = useSetState({
      startDate: dayjs().startOf('date'),
      endDate: dayjs().endOf('date'),
      dateType: 'day',
      checkCurrent: false,
      summaryType: '',
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      }
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var startDate = state.startDate,
    endDate = state.endDate,
    pagination = state.pagination,
    dateType = state.dateType,
    checkCurrent = state.checkCurrent,
    summaryType = state.summaryType;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    monitorItemId = _useState2[0],
    setMonitorItemId = _useState2[1];
  var pageChange = function pageChange(page) {
    setState({
      pagination: _objectSpread(_objectSpread({}, pagination), {}, {
        current: page
      })
    });
  };
  var alarmStartTimeBegin = dayjs(startDate).format('YYYY-MM-DD 00:00:00');
  var alarmStartTimeEnd = dayjs(endDate).format('YYYY-MM-DD 23:59:59');
  var _BaseService$useGetAl = BaseService.useGetAlarmPage({
      page: pagination.current - 1,
      alarmTypes: 'FACTOR',
      alarmStartTimeBegin: alarmStartTimeBegin,
      alarmStartTimeEnd: alarmStartTimeEnd,
      alarmStatuses: checkCurrent ? 'OCCURRING' : '',
      deviceIds: id,
      monitorItemId: monitorItemId
    }),
    alarmRecord = _BaseService$useGetAl.alarmRecord,
    loading = _BaseService$useGetAl.loading;
  var _BaseService$useGetAl2 = BaseService.useGetAlarmPage({
      alarmTypes: 'FACTOR',
      alarmStartTimeBegin: alarmStartTimeBegin,
      alarmStartTimeEnd: alarmStartTimeEnd,
      alarmStatuses: 'OCCURRING',
      deviceIds: id
    }),
    currentAlarmRecord = _BaseService$useGetAl2.alarmRecord;
  var _BaseService$useGetAl3 = BaseService.useGetAlarmPage({
      alarmTypes: 'FACTOR',
      alarmStartTimeBegin: alarmStartTimeBegin,
      alarmStartTimeEnd: alarmStartTimeEnd,
      deviceIds: id
    }),
    historyAlarmRecord = _BaseService$useGetAl3.alarmRecord;
  var _BaseService$useGetAl4 = BaseService.useGetAlarmSummary({
      alarmStartTimeBegin: alarmStartTimeBegin,
      alarmStartTimeEnd: alarmStartTimeEnd,
      deviceIds: id,
      alarmTypes: 'FACTOR'
    }),
    _BaseService$useGetAl5 = _BaseService$useGetAl4.alarmSummary,
    alarmSummary = _BaseService$useGetAl5 === void 0 ? [] : _BaseService$useGetAl5;
  var alarmSummaryMap = keyBy(alarmSummary, 'key');
  var tableProps = {
    pagination: _objectSpread(_objectSpread({}, pagination), {}, {
      total: alarmRecord.total || 0,
      defaultPageSize: 10,
      onChange: pageChange
    }),
    height: '100%',
    dataSource: alarmRecord.rows || [],
    loading: loading,
    onChange: function onChange(_pagination, _filters, sorter) {
      if (sorter.order) {
        var order = sorter.order === 'ascend' ? 'asc' : 'desc';
        setState({
          sort: "".concat(sorter.field, ",").concat(order)
        });
      } else {
        setState({
          sort: ''
        });
      }
    }
  };
  var columns = [{
    title: '报警名称',
    dataIndex: 'alarmName',
    key: 'alarmName'
  }, {
    title: '报警等级',
    dataIndex: 'alarmLevelName',
    key: 'alarmLevelName'
  }, {
    title: '报警状态',
    dataIndex: 'alarmStatusName',
    key: 'alarmStatusName'
  }, {
    title: '报警开始时间',
    dataIndex: 'alarmStartTime',
    key: 'alarmStartTime'
  }, {
    title: '报警处理状态',
    dataIndex: 'alarmHandleStatusName',
    key: 'alarmHandleStatusName'
  }];
  var summaryList = [{
    label: '当前报警',
    value: (currentAlarmRecord === null || currentAlarmRecord === void 0 ? void 0 : currentAlarmRecord.total) || 0,
    key: 'current'
  }, {
    label: '历史报警',
    value: (historyAlarmRecord === null || historyAlarmRecord === void 0 ? void 0 : historyAlarmRecord.total) || 0,
    key: 'history'
  }];
  return /*#__PURE__*/_jsx(VmWrapper, {
    title: '报警分析',
    filterType: filterTypeMap[dateType],
    filterChange: function filterChange(date) {
      setState({
        startDate: date[0],
        endDate: date[1]
      });
    },
    dateValue: dateType === 'self' ? [state.startDate, state.endDate] : startDate,
    extraLeft: /*#__PURE__*/_jsxs(Space, {
      children: [/*#__PURE__*/_jsx(Checkbox, {
        style: {
          color: 'var(--modal-title-color)'
        },
        checked: checkCurrent,
        onChange: function onChange(e) {
          setState({
            checkCurrent: e.target.checked
          });
        },
        children: "\u4EC5\u770B\u5F53\u524D\u62A5\u8B66"
      }), /*#__PURE__*/_jsx(TabsList, {
        list: tabList,
        activeKey: dateType,
        setActiveKey: function setActiveKey(key) {
          setState({
            dateType: key
          });
          if (key !== 'self') {
            var _endDate = dayjs().endOf(key);
            setState({
              startDate: dayjs().startOf(key),
              endDate: _endDate
            });
          }
        }
      })]
    }),
    children: /*#__PURE__*/_jsxs(Row, {
      gutter: [30, 30],
      children: [/*#__PURE__*/_jsx(Col, {
        flex: '368px',
        children: summaryList.map(function (item) {
          return /*#__PURE__*/_jsxs("div", {
            className: classnames('vtx-device-alarm-summary', {
              dark: isDark,
              active: item.key === summaryType
            }),
            onClick: function onClick() {
              var newSummaryType = item.key === summaryType ? '' : item.key;
              setState({
                summaryType: newSummaryType,
                checkCurrent: newSummaryType === 'current'
              });
            },
            children: [/*#__PURE__*/_jsx("div", {
              className: 'vtx-device-alarm-summary-label',
              children: item.label
            }), /*#__PURE__*/_jsx("div", {
              className: 'vtx-device-alarm-summary-value',
              children: item.value
            })]
          }, item.label);
        })
      }), /*#__PURE__*/_jsxs(Col, {
        flex: '1',
        style: {
          minWidth: 0
        },
        children: [/*#__PURE__*/_jsxs(Space, {
          style: {
            marginBottom: 16
          },
          size: 12,
          children: [/*#__PURE__*/_jsx("div", {
            style: {
              cursor: 'pointer'
            },
            onClick: function onClick() {
              return setMonitorItemId('');
            },
            children: /*#__PURE__*/_jsx(VmTag, {
              title: "\u5168\u90E8(".concat(alarmRecord.total || 0, ")"),
              type: monitorItemId === '' ? 'blue' : 'black'
            })
          }), (info.monitorItems || []).map(function (item) {
            var _alarmSummaryMap$item;
            return /*#__PURE__*/_jsx("div", {
              style: {
                cursor: 'pointer'
              },
              onClick: function onClick() {
                return setMonitorItemId(item.key);
              },
              children: /*#__PURE__*/_jsx(VmTag, {
                title: "".concat(item.value, "(").concat(((_alarmSummaryMap$item = alarmSummaryMap[item.key]) === null || _alarmSummaryMap$item === void 0 ? void 0 : _alarmSummaryMap$item.value) || 0, ")"),
                type: item.key === monitorItemId ? 'blue' : 'black'
              })
            }, item.key);
          })]
        }), /*#__PURE__*/_jsx(VmTable, _objectSpread({
          columns: columns,
          rowKey: "id"
        }, tableProps))]
      })]
    })
  });
};
export default AlarmAnalysis;