"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"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _ahooks = require("ahooks");

var _antd = require("antd");

var _moment = _interopRequireDefault(require("moment"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _useSettings2 = _interopRequireDefault(require("../vm-hooks/useSettings"));

var _vmFilter = require("../vm-filter");

var _vmChart = _interopRequireDefault(require("../vm-chart"));

var _api = require("./api");

var _vmSubTabs = _interopRequireWildcard(require("../vm-sub-tabs"));

require("./style");

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 = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var Option = _antd.Select.Option;

var WorkConditionContent = function WorkConditionContent(props) {
  var _selectList$3, _selectList$4, _state$deviceList, _state$deviceList$, _selectList$5, _state$deviceList2, _state$deviceList3, _selectList$6, _state$deviceList4, _state$deviceList4$;

  var id = props.id,
      seed = props.seed;

  var _useSetState = (0, _ahooks.useSetState)({
    startTime: (0, _moment["default"])().subtract(1, 'd'),
    endTime: (0, _moment["default"])(),
    selectKey: '',
    activeKey: '',
    deviceList: [] // 左上角切换数据

  }),
      _useSetState2 = _slicedToArray(_useSetState, 2),
      state = _useSetState2[0],
      setState = _useSetState2[1];

  var selectList = _api.WorkCondition.useSelectList(id); // 监测点下拉数据


  (0, _react.useEffect)(function () {
    var _selectList$, _selectList$2;

    if (typeof (selectList === null || selectList === void 0 ? void 0 : (_selectList$ = selectList[0]) === null || _selectList$ === void 0 ? void 0 : _selectList$.id) !== 'undefined') _api.WorkCondition.useDeviceList(selectList === null || selectList === void 0 ? void 0 : (_selectList$2 = selectList[0]) === null || _selectList$2 === void 0 ? void 0 : _selectList$2.id).then(function (res) {
      if (res.result === 0) {
        setState({
          deviceList: res.data
        });
      }
    });
  }, [selectList === null || selectList === void 0 ? void 0 : (_selectList$3 = selectList[0]) === null || _selectList$3 === void 0 ? void 0 : _selectList$3.id]);
  (0, _react.useEffect)(function () {
    if (seed) setState({
      selectKey: null
    });
  }, [seed]);

  var handleDateChange = function handleDateChange(startTime, endTime) {
    setState({
      startTime: startTime,
      endTime: endTime
    });
  };

  var handleSelectChange = function handleSelectChange(selectKey) {
    setState({
      selectKey: selectKey
    });

    _api.WorkCondition.useDeviceList(selectKey).then(function (res) {
      if (res.result === 0) {
        setState({
          deviceList: res.data
        });
      }
    });
  };

  var handleTabChange = function handleTabChange(activeKey) {
    setState({
      activeKey: activeKey
    });
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "work-content"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "top-bar"
  }, /*#__PURE__*/_react["default"].createElement(_antd.Space, {
    className: "left"
  }, /*#__PURE__*/_react["default"].createElement(_vmFilter.VMSelect, {
    value: state.selectKey || (selectList === null || selectList === void 0 ? void 0 : (_selectList$4 = selectList[0]) === null || _selectList$4 === void 0 ? void 0 : _selectList$4.id) || '',
    style: {
      width: 120
    },
    onChange: handleSelectChange
  }, selectList && selectList.length > 0 && selectList.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(Option, {
      key: item.id,
      value: item.id
    }, item.name);
  })), /*#__PURE__*/_react["default"].createElement(_vmSubTabs["default"], {
    onTabClick: handleTabChange,
    activeKey: state.activeKey || ((_state$deviceList = state.deviceList) === null || _state$deviceList === void 0 ? void 0 : (_state$deviceList$ = _state$deviceList[0]) === null || _state$deviceList$ === void 0 ? void 0 : _state$deviceList$.code) || ''
  }, (state.selectKey || (selectList === null || selectList === void 0 ? void 0 : (_selectList$5 = selectList[0]) === null || _selectList$5 === void 0 ? void 0 : _selectList$5.id) || '') !== '' && ((_state$deviceList2 = state.deviceList) === null || _state$deviceList2 === void 0 ? void 0 : _state$deviceList2.length) > 0 && ((_state$deviceList3 = state.deviceList) === null || _state$deviceList3 === void 0 ? void 0 : _state$deviceList3.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_vmSubTabs.SubTabPane, {
      key: item.code,
      tab: item.name
    });
  })))), /*#__PURE__*/_react["default"].createElement(_antd.Space, {
    className: "right"
  }, /*#__PURE__*/_react["default"].createElement(_vmFilter.VMDatePicker, {
    defaultValue: [state.startTime, state.endTime],
    onChange: function onChange(dates) {
      return dates && handleDateChange(dates[0], dates[1]);
    }
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "chart-container"
  }, /*#__PURE__*/_react["default"].createElement(TrendChart, {
    startTime: state.startTime.format('YYYY-MM-DD'),
    endTime: state.endTime.format('YYYY-MM-DD'),
    deviceId: state.selectKey || (selectList === null || selectList === void 0 ? void 0 : (_selectList$6 = selectList[0]) === null || _selectList$6 === void 0 ? void 0 : _selectList$6.id) || '',
    monitorTypeCodes: state.activeKey || ((_state$deviceList4 = state.deviceList) === null || _state$deviceList4 === void 0 ? void 0 : (_state$deviceList4$ = _state$deviceList4[0]) === null || _state$deviceList4$ === void 0 ? void 0 : _state$deviceList4$.code) || ''
  })));
};

var TrendChart = function TrendChart(props) {
  var _data$;

  var startTime = props.startTime,
      endTime = props.endTime,
      deviceId = props.deviceId,
      monitorTypeCodes = props.monitorTypeCodes;

  var _useSettings = (0, _useSettings2["default"])(),
      isDark = _useSettings.isDark;

  var collectFrequency = startTime === endTime ? 'realTime' : 'day';
  var data = deviceId && monitorTypeCodes && _api.WorkCondition.useEchartsTrend("".concat(startTime, " 00:00:00"), "".concat(endTime, " 23:59:59"), collectFrequency, deviceId, monitorTypeCodes) || [];
  var values = ((_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$.values) || []; // const color

  var option = {
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: (values === null || values === void 0 ? void 0 : values.length) > 0 && (values === null || values === void 0 ? void 0 : values.map(function (item) {
        return item === null || item === void 0 ? void 0 : item.x;
      })) || [],
      axisLabel: {
        fontFamily: 'PingFangSC-Regular',
        color: isDark ? '#fff8' : 'rgba(0, 0, 0, 0.5)'
      },
      axisLine: {
        lineStyle: {
          type: 'dashed',
          color: isDark ? '#fff8' : 'rgba(0, 0, 0, 0.5)'
        }
      }
    },
    yAxis: [{
      type: 'value',
      nameTextStyle: {
        fontFamily: 'PingFangSC-Regular',
        color: isDark ? '#fff8' : '#0008'
      },
      axisLabel: {
        fontFamily: 'PingFangSC-Regular',
        color: isDark ? '#fff8' : '#0008'
      },
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: isDark ? '#fff4' : '#0004'
        }
      }
    }],
    series: data.map(function (item) {
      var _item$values, _item$values2;

      return {
        name: item.name,
        type: 'line',
        data: (item === null || item === void 0 ? void 0 : (_item$values = item.values) === null || _item$values === void 0 ? void 0 : _item$values.length) > 0 && ((_item$values2 = item.values) === null || _item$values2 === void 0 ? void 0 : _item$values2.map(function (item) {
          return parseFloat(item === null || item === void 0 ? void 0 : item.y);
        })) || []
      };
    })
  };
  return /*#__PURE__*/_react["default"].createElement(_vmChart["default"], {
    option: option,
    height: 500,
    width: "100%",
    defaultOption: false
  });
};

WorkConditionContent.propTypes = {
  id: _propTypes["default"].string,
  startTime: _propTypes["default"].string,
  endTime: _propTypes["default"].string,
  deviceId: _propTypes["default"].string,
  monitorTypeCodes: _propTypes["default"].string,
  seed: _propTypes["default"].string
};
TrendChart.propTypes = {
  startTime: _propTypes["default"].string,
  endTime: _propTypes["default"].string,
  deviceId: _propTypes["default"].string,
  monitorTypeCodes: _propTypes["default"].string
};
var _default = WorkConditionContent;
exports["default"] = _default;
//# sourceMappingURL=workCondition.js.map
