"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 _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");
var _vmContainer = _interopRequireDefault(require("../vm-container"));
var _vmSelectDevice = _interopRequireDefault(require("../vm-select-device"));
require("./style");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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; } /**
 * 分析对比
 */
// 对比
var defaultCompareTime = [(0, _moment["default"])().subtract(1, 'day'), (0, _moment["default"])()];
// 参照
var dates = [(0, _moment["default"])().subtract(2, 'day'), (0, _moment["default"])().subtract(1, 'day')];
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(2, 'day'), (0, _moment["default"])().subtract(1, 'day')],
  7: [(0, _moment["default"])().subtract(14, 'day'), (0, _moment["default"])().subtract(7, 'day')],
  30: [(0, _moment["default"])().subtract(60, 'day'), (0, _moment["default"])().subtract(30, 'day')]
};
//对比时间
var compareTimeObj = {
  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,
    compareTimeChange = props.compareTimeChange,
    analysisFun = props.analysisFun,
    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];
  var _useState5 = (0, _react.useState)(defaultCompareTime),
    _useState6 = _slicedToArray(_useState5, 2),
    compareTime = _useState6[0],
    setCompareTime = _useState6[1];
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "radio-time"
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "title"
  }, " \u65F6\u95F4\u8DE8\u5EA6\uFF1A"), /*#__PURE__*/_react["default"].createElement(_vmRadio.VmRadioGroup, {
    style: {
      marginLeft: '20px'
    },
    defaultValue: typeArr[0],
    onChange: function onChange(e) {
      var value = e.target.value;
      setType(value);
      if (dateObj[value]) {
        setDate(dateObj[value]);
        setCompareTime(compareTimeObj[value]);
        dateChange && dateChange(dateObj[value]);
        compareTimeChange && compareTimeChange(compareTimeObj[value]);
      }
    }
  }, radioList.map(function (v, index) {
    return /*#__PURE__*/_react["default"].createElement(_vmRadio["default"], {
      key: index,
      value: v.value
    }, v.title);
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      display: 'flex',
      alignItems: 'center',
      flexWrap: 'wrap'
    }
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("span", {
    className: "title"
  }, " \u53C2\u7167\u65F6\u95F4\uFF1A"), /*#__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'
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      margin: '0 10px'
    }
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "title"
  }, "\u5BF9\u6BD4\u65F6\u95F4\uFF1A"), /*#__PURE__*/_react["default"].createElement(_vmFilter.VMRangePicker, {
    onChange: function onChange(e) {
      setDate(e);
      compareTimeChange && compareTimeChange(e);
    },
    disabled: typeArr.includes(type),
    value: compareTime,
    format: format,
    style: {
      width: '350px'
    }
  })), isRelateDevice ? (/*#__PURE__*/_react["default"].createElement("div", {
    style: {
      marginTop: '10px'
    }
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "title"
  }, " \u76D1\u6D4B\u8BBE\u5907\uFF1A"), /*#__PURE__*/_react["default"].createElement(_vmSelectDevice["default"], {
    selectStyle: {
      width: 340
    },
    id: id,
    getSelectValue: getSelectValue,
    className: true
  }))) : null, /*#__PURE__*/_react["default"].createElement(_antd.Button, {
    onClick: function onClick() {
      analysisFun && analysisFun();
    },
    type: "primary",
    style: {
      marginLeft: '30px'
    }
  }, "\u5206\u6790")));
};
function VmAnalysisComparison(props) {
  var _useState7 = (0, _react.useState)(dates),
    _useState8 = _slicedToArray(_useState7, 2),
    date = _useState8[0],
    setDate = _useState8[1]; // 参照时间
  var _useState9 = (0, _react.useState)(defaultCompareTime),
    _useState0 = _slicedToArray(_useState9, 2),
    compareTime = _useState0[0],
    setCompareTime = _useState0[1]; // 对比时间
  var _useState1 = (0, _react.useState)([]),
    _useState10 = _slicedToArray(_useState1, 2),
    checkboxValue = _useState10[0],
    setCheckboxValue = _useState10[1]; // 选中复选框数据
  var _useState11 = (0, _react.useState)([]),
    _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),
    compareXdate = _useState20[0],
    setCompareXdate = _useState20[1]; // 对比图表X轴数据
  var _useState21 = (0, _react.useState)([]),
    _useState22 = _slicedToArray(_useState21, 2),
    compareData = _useState22[0],
    setCompareData = _useState22[1]; // 对比图表数据
  var _useState23 = (0, _react.useState)(''),
    _useState24 = _slicedToArray(_useState23, 2),
    deviceId = _useState24[0],
    setDeviceId = _useState24[1]; // 获取设施关联设备id
  var _useState25 = (0, _react.useState)(false),
    _useState26 = _slicedToArray(_useState25, 2),
    loading = _useState26[0],
    setLoading = _useState26[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, null, true);
      }
    } else {
      getMonitorData(null, null, true);
    }
  }, [id, deviceId]);
  // 设备id
  var getSelectValue = function getSelectValue(value) {
    setDeviceId(value);
  };
  // 分析
  var analysisFun = function analysisFun() {
    setLineData([]);
    getMonitorData(null, null, false);
  };
  // 获取图表数据 time-参照时间 time1-对比时间
  var getMonitorData = function getMonitorData(time, time1, first) {
    setLoading(true);
    var newTime = time || date;
    var compareTimes = time1 || compareTime;
    _api.Service.getMonitorData({
      id: id,
      publicUrlJCSS: publicUrlJCSS,
      startTime: newTime[0].format(format),
      endTime: newTime[1].format(format),
      compareStartTime: compareTimes[0].format(format),
      compareEndTime: compareTimes[1].format(format),
      filterParams: filterParams,
      isRelateDevice: isRelateDevice,
      deviceId: deviceId,
      isDevice: isDevice
    }).then(function (res) {
      var _a;
      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,
        _ref$compareData = _ref.compareData,
        compareData = _ref$compareData === void 0 ? [] : _ref$compareData,
        _ref$compareXdate = _ref.compareXdate,
        compareXdate = _ref$compareXdate === void 0 ? [] : _ref$compareXdate;
      setCheckboxOptions(checkboxOptions);
      setXdate(xdate);
      setChartData(data);
      setCompareData(compareData);
      setCompareXdate(compareXdate);
      if (first) {
        setCheckboxValue([(_a = checkboxOptions === null || checkboxOptions === void 0 ? void 0 : checkboxOptions[0]) === null || _a === void 0 ? void 0 : _a.value]);
        if (data && data.length) {
          setLineData([data[0], compareData[0] || []]);
        } else if (compareData && compareData.length) {
          setLineData([[], compareData[0] || []]);
        }
      } else {
        handleData(data, compareData, checkboxValue);
      }
    });
  };
  // data:参照 compareData：对比
  var handleData = function handleData(data, compareData, checkValue) {
    var newData = [];
    checkValue.map(function (v) {
      var data1 = data.find(function (item) {
        return item.factorCode === v;
      });
      var data2 = compareData.find(function (item) {
        return item.factorCode === v;
      });
      if (data1) {
        newData.push(data1);
      }
      if (data2) {
        newData.push(data2);
      }
    });
    setLineData(newData);
  };
  // 参照时间
  var dateChange = function dateChange(e) {
    setDate(e);
  };
  // 对比时间
  var compareTimeChange = function compareTimeChange(e) {
    setCompareTime(e);
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "analysis-comparison"
  }, /*#__PURE__*/_react["default"].createElement(_vmContainer["default"], {
    style: {
      marginBottom: '5px'
    }
  }, /*#__PURE__*/_react["default"].createElement(DateSelect, Object.assign({
    defaultDate: dates,
    dateChange: dateChange,
    compareTimeChange: compareTimeChange,
    analysisFun: analysisFun,
    getSelectValue: getSelectValue
  }, 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, compareData, e);
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      height: '260px'
    }
  }, lineData && lineData.length ? (/*#__PURE__*/_react["default"].createElement(_index.Line, {
    data: lineData,
    xAxisConfig: [{
      type: 'category',
      boundaryGap: false,
      data: xdate,
      axisLabel: {
        show: false
        // color: '#DAF1FF',
        // interval: 0,
      },
      axisTick: {
        show: false
      },
      axisLine: {
        show: false
      },
      axisPointer: {
        // show: false,
      },
      onZero: false,
      z: 10
    }],
    xAxis: compareXdate,
    exportShow: true,
    dataZoomShow: true
  })) : (/*#__PURE__*/_react["default"].createElement(_vmEmpty["default"], {
    type: "chart"
  }))))));
}
var _default = exports["default"] = VmAnalysisComparison;
//# sourceMappingURL=index.js.map
