"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"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _antd = require("antd");
var _vmFilter = require("../vm-filter");
var _vmCheckbox = require("../vm-checkbox");
var _vmSubTabs = _interopRequireWildcard(require("../vm-sub-tabs"));
var _vmTable = _interopRequireDefault(require("../vm-table"));
var _index = require("../vm-charts/index");
var _vmRadio = _interopRequireWildcard(require("../vm-radio"));
var _moment = _interopRequireDefault(require("moment"));
var _vmEmpty = _interopRequireDefault(require("../vm-empty"));
var _api = require("./api");
require("./style");
var _vmContainer = _interopRequireDefault(require("../vm-container"));
var _vmSelectDevice = _interopRequireDefault(require("../vm-select-device"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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; } /**
 * 实时监测  isRelateDevice 判断设施下是否绑定设备
 */
var dates = [(0, _moment["default"])().subtract(1, 'day'), (0, _moment["default"])()];
var format = 'YYYY-MM-DD HH:mm:ss';
var radioList = [{
  title: '1天',
  value: '1'
}, {
  title: '7天',
  value: '7'
}, {
  title: '30天',
  value: '30'
}, {
  title: '自定义',
  value: '0'
}];
var typeArr = ['1', '7', '30'];
var dateObj = {
  1: [(0, _moment["default"])().subtract(1, 'day'), (0, _moment["default"])()],
  7: [(0, _moment["default"])().subtract(7, 'day'), (0, _moment["default"])()],
  30: [(0, _moment["default"])().subtract(30, 'day'), (0, _moment["default"])()]
};
// 时间选择
var DateSelect = function DateSelect(props) {
  var defaultDate = props.defaultDate,
    dateChange = props.dateChange,
    id = props.id,
    isRelateDevice = props.isRelateDevice,
    getSelectValue = props.getSelectValue;
  var _useState = (0, _react.useState)(typeArr[0]),
    _useState2 = _slicedToArray(_useState, 2),
    type = _useState2[0],
    setType = _useState2[1];
  var _useState3 = (0, _react.useState)(defaultDate),
    _useState4 = _slicedToArray(_useState3, 2),
    date = _useState4[0],
    setDate = _useState4[1];
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "radio-time"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "time"
  }, "\u65F6\u95F4\u8DE8\u5EA6\uFF1A", /*#__PURE__*/_react["default"].createElement(_vmRadio.VmRadioGroup, {
    style: {
      marginLeft: '20px'
    },
    defaultValue: type,
    onChange: function onChange(e) {
      var value = e.target.value;
      setType(value);
      if (dateObj[value]) {
        setDate(dateObj[value]);
        dateChange && dateChange(dateObj[value]);
      }
    }
  }, radioList.map(function (v, index) {
    return /*#__PURE__*/_react["default"].createElement(_vmRadio["default"], {
      key: index,
      value: v.value
    }, v.title);
  })), /*#__PURE__*/_react["default"].createElement(_vmFilter.VMRangePicker, {
    onChange: function onChange(e) {
      setDate(e);
      dateChange && dateChange(e);
    },
    disabled: typeArr.includes(type),
    value: date,
    format: format,
    style: {
      width: '350px'
    }
  }))), isRelateDevice ? (/*#__PURE__*/_react["default"].createElement("div", {
    style: {
      display: 'flex',
      alignItems: 'center'
    }
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("span", {
    className: "title"
  }, " \u76D1\u6D4B\u8BBE\u5907\uFF1A"), /*#__PURE__*/_react["default"].createElement(_vmSelectDevice["default"], {
    id: id,
    getSelectValue: getSelectValue,
    className: true,
    selectStyle: {
      width: 340
    }
  })))) : null);
};
function VmRealtimeMonitor(props) {
  var _useState5 = (0, _react.useState)(''),
    _useState6 = _slicedToArray(_useState5, 2),
    tabKey = _useState6[0],
    setTabKey = _useState6[1];
  var _useState7 = (0, _react.useState)(dates),
    _useState8 = _slicedToArray(_useState7, 2),
    date = _useState8[0],
    setDate = _useState8[1]; // 时间
  var _useState9 = (0, _react.useState)(null),
    _useState10 = _slicedToArray(_useState9, 2),
    checkboxValue = _useState10[0],
    setCheckboxValue = _useState10[1]; // 选中复选框数据
  var _useState11 = (0, _react.useState)(null),
    _useState12 = _slicedToArray(_useState11, 2),
    lineData = _useState12[0],
    setLineData = _useState12[1]; // 当前展示图表数据
  var _useState13 = (0, _react.useState)([]),
    _useState14 = _slicedToArray(_useState13, 2),
    checkboxOptions = _useState14[0],
    setCheckboxOptions = _useState14[1]; // 复选框数据
  var _useState15 = (0, _react.useState)([]),
    _useState16 = _slicedToArray(_useState15, 2),
    chartData = _useState16[0],
    setChartData = _useState16[1]; // 所有图表数据
  var _useState17 = (0, _react.useState)([]),
    _useState18 = _slicedToArray(_useState17, 2),
    xdate = _useState18[0],
    setXdate = _useState18[1]; // 图表X轴数据
  var _useState19 = (0, _react.useState)([]),
    _useState20 = _slicedToArray(_useState19, 2),
    dataSource = _useState20[0],
    setDataSource = _useState20[1]; // 表格数据
  var _useState21 = (0, _react.useState)(''),
    _useState22 = _slicedToArray(_useState21, 2),
    currentName = _useState22[0],
    setCurrentName = _useState22[1];
  var _useState23 = (0, _react.useState)(1),
    _useState24 = _slicedToArray(_useState23, 2),
    current = _useState24[0],
    setCurrent = _useState24[1];
  var _useState25 = (0, _react.useState)(10),
    _useState26 = _slicedToArray(_useState25, 2),
    size = _useState26[0],
    setSize = _useState26[1];
  var _useState27 = (0, _react.useState)(0),
    _useState28 = _slicedToArray(_useState27, 2),
    total = _useState28[0],
    setTotal = _useState28[1];
  var _useState29 = (0, _react.useState)(''),
    _useState30 = _slicedToArray(_useState29, 2),
    deviceId = _useState30[0],
    setDeviceId = _useState30[1]; // 获取设施关联设备id
  var _useState31 = (0, _react.useState)(false),
    _useState32 = _slicedToArray(_useState31, 2),
    loading = _useState32[0],
    setLoading = _useState32[1];
  var id = props.id,
    publicUrlJCSS = props.publicUrlJCSS,
    filterParams = props.filterParams,
    isRelateDevice = props.isRelateDevice,
    isDevice = props.isDevice;
  (0, _react.useEffect)(function () {
    // 判断是否存在绑定设备
    if (isRelateDevice) {
      if (deviceId) {
        getMonitorData(null, true);
      }
    } else {
      getMonitorData(null, true);
    }
  }, [id, deviceId]);
  // 获取图表数据
  var getMonitorData = function getMonitorData(time, first) {
    setLoading(true);
    setLineData([]);
    var newTime = time || date;
    _api.Service.getMonitorData({
      id: id,
      publicUrlJCSS: publicUrlJCSS,
      startTime: newTime[0].format(format),
      endTime: newTime[1].format(format),
      filterParams: filterParams,
      isRelateDevice: isRelateDevice,
      deviceId: deviceId,
      isDevice: isDevice
    }).then(function (res) {
      var _a, _b, _c, _d;
      setLoading(false);
      var _ref = res || {},
        _ref$xdate = _ref.xdate,
        xdate = _ref$xdate === void 0 ? [] : _ref$xdate,
        _ref$data = _ref.data,
        data = _ref$data === void 0 ? [] : _ref$data,
        _ref$checkboxOptions = _ref.checkboxOptions,
        checkboxOptions = _ref$checkboxOptions === void 0 ? [] : _ref$checkboxOptions;
      setCheckboxOptions(checkboxOptions);
      setXdate(xdate);
      setChartData(data);
      var factorCode = tabKey;
      if (first) {
        setCheckboxValue([(_a = checkboxOptions === null || checkboxOptions === void 0 ? void 0 : checkboxOptions[0]) === null || _a === void 0 ? void 0 : _a.value]);
        setCurrentName((_b = checkboxOptions === null || checkboxOptions === void 0 ? void 0 : checkboxOptions[0]) === null || _b === void 0 ? void 0 : _b.title);
        setTabKey((_c = checkboxOptions === null || checkboxOptions === void 0 ? void 0 : checkboxOptions[0]) === null || _c === void 0 ? void 0 : _c.key);
        factorCode = (_d = checkboxOptions === null || checkboxOptions === void 0 ? void 0 : checkboxOptions[0]) === null || _d === void 0 ? void 0 : _d.value;
        if (data && data.length) {
          setLineData([data[0]]);
        }
      } else {
        handleData(data, checkboxValue);
      }
      setCurrent(1);
      getTableData({
        time: newTime,
        pageCurrent: 1,
        pageSize: size,
        factorCode: factorCode
      });
    });
  };
  var getTableData = function getTableData(_ref2) {
    var time = _ref2.time,
      pageCurrent = _ref2.pageCurrent,
      pageSize = _ref2.pageSize,
      factorCode = _ref2.factorCode;
    _api.Service.getTableData({
      id: id,
      publicUrlJCSS: publicUrlJCSS,
      startTime: time[0].format(format),
      endTime: time[1].format(format),
      current: pageCurrent,
      size: pageSize,
      factorCode: factorCode,
      filterParams: filterParams,
      isRelateDevice: isRelateDevice,
      deviceId: deviceId,
      isDevice: isDevice
    }).then(function (res) {
      var _ref3 = res || {},
        _ref3$tableData = _ref3.tableData,
        tableData = _ref3$tableData === void 0 ? [] : _ref3$tableData,
        total = _ref3.total;
      setDataSource(tableData);
      setTotal(total);
    });
  };
  var handleData = function handleData(data, checkValue) {
    var newData = data ? data.filter(function (item) {
      return checkValue.includes(item.factorCode);
    }) : [];
    setLineData(newData);
  };
  var dateChange = function dateChange(e) {
    setDate(e);
    getMonitorData(e, false);
  };
  var columns = [{
    title: '时间',
    dataIndex: 'timeDesc',
    key: 'timeDesc',
    sortType: 'time'
  }, {
    title: currentName,
    dataIndex: 'value',
    key: 'value',
    sortType: 'number'
  }];
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "realtime-monitor"
  }, /*#__PURE__*/_react["default"].createElement(DateSelect, Object.assign({
    defaultDate: dates,
    dateChange: dateChange,
    getSelectValue: setDeviceId
  }, props)), /*#__PURE__*/_react["default"].createElement(_antd.Spin, {
    spinning: loading
  }, /*#__PURE__*/_react["default"].createElement(_vmContainer["default"], {
    contentStyle: {
      overflowY: 'hidden'
    }
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_vmCheckbox.VmCheckboxGroup, {
    options: checkboxOptions,
    value: checkboxValue,
    onChange: function onChange(e) {
      setCheckboxValue(e);
      handleData(chartData, e);
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      height: '260px'
    }
  }, lineData && lineData.length ? (/*#__PURE__*/_react["default"].createElement(_index.Line, {
    data: lineData,
    xAxis: xdate,
    exportShow: true,
    dataZoomShow: true
  })) : (/*#__PURE__*/_react["default"].createElement(_vmEmpty["default"], {
    type: "chart"
  }))))), /*#__PURE__*/_react["default"].createElement(_vmContainer["default"], {
    style: {
      marginTop: '5px',
      padding: 0
    }
  }, /*#__PURE__*/_react["default"].createElement(_vmSubTabs["default"], {
    onChange: function onChange(key) {
      var _checkboxOptions$find = checkboxOptions.find(function (v) {
          return v.key === key;
        }),
        title = _checkboxOptions$find.title;
      setCurrentName(title);
      setTabKey(key);
      setCurrent(1);
      getTableData({
        time: date,
        pageCurrent: 1,
        pageSize: size,
        factorCode: key
      });
    },
    activeKey: tabKey,
    style: {
      marginBottom: '20px'
    }
  }, checkboxOptions.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_vmSubTabs.SubTabPane, {
      tab: item.title,
      key: item.key
    });
  })), /*#__PURE__*/_react["default"].createElement(_vmTable["default"], {
    dataSource: dataSource,
    columns: columns,
    height: 500,
    pagination: {
      current: current,
      pageSize: size,
      total: total,
      onChange: function onChange(current, size) {
        setCurrent(current);
        setSize(size);
        getTableData({
          time: date,
          pageCurrent: current,
          pageSize: size,
          factorCode: tabKey
        });
      }
    }
  })));
}
var _default = exports["default"] = VmRealtimeMonitor;
//# sourceMappingURL=index.js.map
