"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"] = AlarmTrend;
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _antd = require("antd");
var _api = require("../../api");
var _ahooks = require("ahooks");
var _moment = _interopRequireDefault(require("moment"));
var _vmFilter = require("../../../vm-filter");
var _echartsForReact = _interopRequireDefault(require("echarts-for-react"));
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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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; }
function AlarmTrend(props) {
  var prefix = props.prefix,
    id = props.id;
  var _useSetState = (0, _ahooks.useSetState)({
      timeType: 'day',
      alarmTypes: '',
      alarmStartTime: (0, _moment["default"])().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
      alarmEndTime: (0, _moment["default"])().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
      dataList: [],
      alarmTypeList: [],
      type: 'tb' //同比、环比
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var timeType = state.timeType,
    alarmEndTime = state.alarmEndTime,
    alarmStartTime = state.alarmStartTime,
    alarmTypes = state.alarmTypes,
    dataList = state.dataList,
    alarmTypeList = state.alarmTypeList,
    type = state.type;
  (0, _react.useEffect)(function () {
    _api.AlarmService.enumList('DeviceAlarmTypeEnum').then(function (data) {
      setState({
        alarmTypeList: data
      });
    });
  }, []);
  (0, _react.useEffect)(function () {
    _api.AlarmService.trend({
      facilityIds: [id],
      alarmTypes: alarmTypes ? [alarmTypes] : [],
      alarmStartTimeBegin: (0, _moment["default"])(alarmStartTime).startOf(timeType === 'day' ? 'day' : timeType === 'month' ? 'month' : 'year').format('YYYY-MM-DD HH:mm:ss'),
      alarmStartTimeEnd: (0, _moment["default"])(alarmStartTime).endOf(timeType === 'day' ? 'day' : timeType === 'month' ? 'month' : 'year').format('YYYY-MM-DD HH:mm:ss'),
      dimension: timeType === 'year' ? 'month' : timeType === 'month' ? 'day' : 'hour',
      type: type
    }).then(function (data) {
      setState({
        dataList: data
      });
    });
  }, [timeType, alarmEndTime, alarmStartTime, alarmTypes, type]);
  var options = {
    color: ['#0E85FF', '#49FF81', '#26D4FF', '#F5D629', '#C97CFF'],
    tooltip: {
      trigger: 'axis',
      formatter: function formatter(params) {
        var _a, _b;
        //tongjia
        var unit = '次';
        var str = '';
        if (Array.isArray(params)) {
          str = "<div  style=\"text-align:left\">".concat((_a = params === null || params === void 0 ? void 0 : params[0]) === null || _a === void 0 ? void 0 : _a.axisValue, "<?div>");
          var color = (_b = params === null || params === void 0 ? void 0 : params[0].color) === null || _b === void 0 ? void 0 : _b.colorStops; // 获取系列的颜色
          params.map(function (item, index) {
            var _a;
            str += "<div  style=\"text-align:left\"><span style=\"color:".concat(item.color && typeof item.color == 'string' ? item.color : (_a = color === null || color === void 0 ? void 0 : color[index]) === null || _a === void 0 ? void 0 : _a.color, "\">\u2022</span> ").concat(item.seriesName, " &nbsp;&nbsp;&nbsp;").concat(item.value).concat(unit, "</div>");
          });
        } else {
          var _color = params === null || params === void 0 ? void 0 : params.color; // 获取系列的颜色
          str += "<div style=\"display:flex;align-items:center\"><span style=\"color:".concat(_color, ";font-size:24px \">\u2022</span>&nbsp;").concat(params === null || params === void 0 ? void 0 : params.name, "&nbsp;&nbsp;").concat(params.value).concat(unit, "</div>");
        }
        return str; // 返回带点的字符串
      }
    },
    legend: {
      type: 'scroll',
      top: 0,
      left: 'center',
      textStyle: {
        color: '#fff'
      }
    },
    toolbox: {
      show: true,
      feature: {
        saveAsImage: {
          show: true
        }
      }
    },
    grid: {
      top: 50,
      left: 20,
      right: 20,
      bottom: 20,
      containLabel: true
    },
    xAxis: {
      data: dataList.map(function (item) {
        return item.date;
      }),
      axisLabel: {
        color: '#fff',
        interval: 'auto'
      },
      axisTick: {
        show: false
      },
      axisLine: {
        show: false
      },
      z: 10
    },
    yAxis: {
      name: '次',
      nameTextStyle: {
        color: '#fff'
      },
      splitNumber: 1,
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#000000'
      },
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: 'rgba(255,255,255,0.2)'
        }
      }
    },
    series: [{
      name: '',
      type: 'line',
      smooth: true,
      data: dataList.map(function (item) {
        var percent;
        percent = (item === null || item === void 0 ? void 0 : item.compareResult) && (item === null || item === void 0 ? void 0 : item.compareValue) ? (item.compareResult / item.compareValue * 100).toFixed(0) : '';
        var color = percent > 0 ? 'green' : percent < 0 ? 'red' : 'black';
        return {
          value: item.currentValue,
          label: {
            show: true,
            color: color,
            value: percent,
            formatter: function formatter() {
              return percent;
            }
          }
        };
      })
    }]
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-alarm-card")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-alarm-card-title")
  }, /*#__PURE__*/_react["default"].createElement("span", null, "\u62A5\u8B66\u8D8B\u52BF"), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    options: [{
      label: '日',
      value: 'day'
    }, {
      label: '月',
      value: 'month'
    }, {
      label: '年',
      value: 'year'
    }],
    value: timeType,
    onChange: function onChange(e) {
      setState({
        timeType: e.target.value
      });
    }
  }), /*#__PURE__*/_react["default"].createElement(_vmFilter.VMRangePicker, {
    style: {
      width: 240
    },
    value: [(0, _moment["default"])(alarmStartTime), (0, _moment["default"])(alarmEndTime)],
    format: timeType === 'day' ? 'YYYY-MM-DD' : timeType === 'month' ? 'YYYY-MM' : 'YYYY',
    onChange: function onChange(date) {
      setState({
        alarmStartTime: date.length ? (0, _moment["default"])(date[0]).format('YYYY-MM-DD HH:mm:ss') : '',
        alarmEndTime: date.length ? (0, _moment["default"])(date[1]).format('YYYY-MM-DD HH:mm:ss') : ''
      });
    }
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-alarm-card-content")
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    options: [{
      label: '全部',
      value: ''
    }].concat(_toConsumableArray(alarmTypeList.map(function (item) {
      return {
        label: item.name,
        value: item.code
      };
    }))),
    value: alarmTypes,
    onChange: function onChange(e) {
      setState({
        alarmTypes: e.target.value
      });
    }
  }), /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    options: [{
      label: '同比',
      value: 'tb'
    }, {
      label: '环比',
      value: 'hb'
    }],
    value: type,
    onChange: function onChange(e) {
      setState({
        type: e.target.value
      });
    }
  })), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_echartsForReact["default"], {
    option: options,
    notMerge: true,
    lazyUpdate: true,
    style: {
      height: 280,
      width: '100%'
    }
  }))));
}
//# sourceMappingURL=AlarmTrend.js.map
