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 { useSetState } from 'ahooks';
import { Space } from 'antd';
import dayjs from 'dayjs';
import sumBy from 'lodash/sumBy';
import { useEffect } from 'react';
import { VmFilter, VmTable, VmWrapper } from "../../../_components";
import useSettings from "../../../_hooks/useSettings";
import { ExeceptionService } from "../../api";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default function AlarmRecord(props) {
  var _useSettings = useSettings(),
    sDate = _useSettings.startDate,
    eDate = _useSettings.endDate;
  var id = props.id;
  var _useSetState = useSetState({
      alarmTypeCode: 'all',
      alarmTabs: [{
        value: 'all',
        label: '全部'
      }],
      startDate: sDate ? sDate : dayjs().startOf('M').format('YYYY-MM-DD'),
      endDate: eDate ? eDate : dayjs().format('YYYY-MM-DD'),
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      },
      sort: ''
    }),
    _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,
    sort = state.sort;
  var _ExeceptionService$us = ExeceptionService.useAlarmTrend(id, startDate, endDate),
    alarmTypes = _ExeceptionService$us.alarmTypes;
  var _ExeceptionService$us2 = ExeceptionService.useAlarmRecord(id, alarmTypeCode === 'all' ? '' : alarmTypeCode, "".concat(startDate, " 00:00:00"), "".concat(endDate, " 23:59:59"), pagination.current - 1, sort),
    detail = _ExeceptionService$us2.detail;
  // 格式化报警类型数据
  var formatAlarmType = function formatAlarmType(dataList) {
    var list = [];
    var totalCount = 0; // 报警类型总数
    totalCount = sumBy(dataList, function (o) {
      return o.count;
    });
    dataList.forEach(function (ele) {
      var obj = {
        value: ele.typeCode,
        label: "".concat(ele.typeName, " (").concat(ele.count, ")")
      };
      list.push(obj);
    });
    return {
      list: list,
      totalCount: totalCount
    };
  };
  useEffect(function () {
    var arr = [{
      value: 'all',
      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: 'submitDate',
    key: 'submitDate',
    sorter: true
  }, {
    title: '异常类型',
    dataIndex: 'abnormalTypeStr',
    key: 'abnormalTypeStr'
  }, {
    title: '上报车辆',
    dataIndex: 'carCode',
    key: 'carCode'
  }, {
    title: '处理状态',
    dataIndex: 'dealStatusStr',
    key: 'dealStatusStr',
    render: function render(text, record) {
      return /*#__PURE__*/_jsxs("span", {
        style: {
          color: getColor(record.dealStatus)
        },
        children: [/*#__PURE__*/_jsx("div", {
          style: {
            display: 'inline-block',
            width: '8px',
            height: '8px',
            borderRadius: '50%',
            marginRight: '6px',
            background: getColor(record.dealStatus)
          }
        }), text]
      });
    }
  }];
  var getColor = function getColor(code) {
    var color = '';
    switch (code) {
      case 1:
        color = '#1FEFA9';
        break;
      case 0:
        color = '#FF6A65';
        break;
      default:
        color = '#FFBD1E';
        break;
    }
    return color;
  };
  var pageChange = function pageChange(page) {
    setState({
      pagination: _objectSpread(_objectSpread({}, pagination), {}, {
        current: page
      })
    });
  };
  var handleTableChange = function handleTableChange(sorter) {
    setState({
      sort: "collectDay,".concat(sorter.order === 'ascend' ? 'ASC' : 'DESC')
    });
  };
  var tableProps = {
    pagination: _objectSpread(_objectSpread({}, pagination), {}, {
      total: detail.total,
      defaultPageSize: 10,
      onChange: pageChange
    }),
    handleTableChange: handleTableChange,
    height: 130,
    dataSource: detail.dataSource
  };
  return /*#__PURE__*/_jsx("div", {
    className: "alarm-record",
    children: /*#__PURE__*/_jsx(VmWrapper, {
      title: "\u5F02\u5E38\u8BB0\u5F55",
      extra: /*#__PURE__*/_jsxs(Space, {
        children: [/*#__PURE__*/_jsx(VmFilter.VMSelect, {
          value: alarmTypeCode,
          onChange: function onChange(val) {
            setState({
              alarmTypeCode: val
            });
          },
          options: alarmTabs,
          style: {
            width: 150
          },
          placeholder: "\u8BF7\u9009\u62E9",
          allowClear: false
        }), /*#__PURE__*/_jsx(VmFilter.VMRangePicker, {
          value: [dayjs(startDate), dayjs(endDate)],
          onChange: function onChange(dates) {
            return dates && setState({
              startDate: dayjs(dates[0]).format('YYYY-MM-DD'),
              endDate: dayjs(dates[1]).format('YYYY-MM-DD')
            });
          }
        })]
      }),
      children: /*#__PURE__*/_jsx("div", {
        className: "alarm-record-table",
        children: /*#__PURE__*/_jsx(VmTable, _objectSpread({
          columns: columns
        }, tableProps))
      })
    })
  });
}