"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 _ahooks = require("ahooks");
var _antd = require("antd");
var _dayjs = _interopRequireDefault(require("dayjs"));
var _moment = _interopRequireDefault(require("moment"));
require("moment/locale/zh-cn");
var _zh_CN = _interopRequireDefault(require("antd/es/date-picker/locale/zh_CN"));
require("../index.less");
var _utils = require("@vtx/utils");
var _echartsForReact = _interopRequireDefault(require("echarts-for-react"));
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; }
var RangePicker = _antd.DatePicker.RangePicker;
var getMonitorType = function getMonitorType(params) {
  return _utils.request.get('/cloud/zhgc-reborn/api/kanban/getMonitorType', {
    data: params
  });
};
var monitorItemEcharts = function monitorItemEcharts(params) {
  return _utils.request.get('/cloud/zhgc-reborn/api/kanban/monitorItemEcharts', {
    data: params
  });
};
var Color = ['#58E076', '#F56753', '#AB7324', '#3FB21C', '#00B7EE', '#FFF45C', '#745EDF', '#3A6BDB'];
var Monitor = function Monitor(props) {
  var _a, _b;
  var detail = props.detail,
    _props$theme = props.theme,
    theme = _props$theme === void 0 ? 'light' : _props$theme;
  var _useSetState = (0, _ahooks.useSetState)({
      monitorTypeList: [],
      monitorTypeCode: '',
      frequency: '',
      startDate: '',
      endDate: '',
      echartsData: {
        legends: [],
        names: [],
        values: [],
        maxData: 0,
        levelData: []
      }
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var monitorTypeList = state.monitorTypeList,
    monitorTypeCode = state.monitorTypeCode,
    frequency = state.frequency,
    startDate = state.startDate,
    endDate = state.endDate,
    echartsData = state.echartsData;
  (0, _react.useEffect)(function () {
    detail.id && getMonitorType({
      facilityId: detail.id
    }).then(function (res) {
      var _a, _b;
      if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
        var _monitorTypeCode = (_a = res.data[0]) === null || _a === void 0 ? void 0 : _a.monitorTypeCode;
        setState({
          monitorTypeList: res.data,
          monitorTypeCode: (_b = res.data[0]) === null || _b === void 0 ? void 0 : _b.monitorTypeCode,
          frequency: _monitorTypeCode === 'GC_PERSON_NUM' || _monitorTypeCode === 'GC_ELECTRICITY_USE' || _monitorTypeCode === 'GC_WATER_USE' ? 'hour' : 'day',
          startDate: (0, _dayjs["default"])().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: (0, _dayjs["default"])().format('YYYY-MM-DD HH:mm:ss')
        });
      }
    });
  }, [detail.id]);
  var frequencyListA = [{
    name: '日',
    value: 'hour'
  }, {
    name: '月',
    value: 'month'
  }];
  var frequencyListB = [{
    name: '日',
    value: 'day'
  }, {
    name: '昨日',
    value: 'yesterday'
  }, {
    name: '前日',
    value: 'yesterdayB'
  }];
  var getFrequency = function getFrequency() {
    if (monitorTypeCode === 'GC_PERSON_NUM' || monitorTypeCode === 'GC_ELECTRICITY_USE' || monitorTypeCode === 'GC_WATER_USE') {
      return frequencyListA;
    } else {
      return frequencyListB;
    }
  };
  (0, _react.useEffect)(function () {
    switch (frequency) {
      case 'hour':
        setState({
          startDate: (0, _dayjs["default"])().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: (0, _dayjs["default"])().format('YYYY-MM-DD HH:mm:ss')
        });
        break;
      case 'month':
        setState({
          startDate: (0, _dayjs["default"])().startOf('month').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: (0, _dayjs["default"])().format('YYYY-MM-DD HH:mm:ss')
        });
        break;
      case 'day':
        setState({
          startDate: (0, _dayjs["default"])().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: (0, _dayjs["default"])().format('YYYY-MM-DD HH:mm:ss')
        });
        break;
      case 'yesterday':
        setState({
          startDate: (0, _dayjs["default"])().subtract(1, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: (0, _dayjs["default"])().subtract(1, 'day').endOf('day').format('YYYY-MM-DD HH:mm:ss')
        });
        break;
      case 'yesterdayB':
        setState({
          startDate: (0, _dayjs["default"])().subtract(2, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: (0, _dayjs["default"])().subtract(2, 'day').endOf('day').format('YYYY-MM-DD HH:mm:ss')
        });
        break;
    }
  }, [frequency]);
  (0, _react.useEffect)(function () {
    if (monitorTypeCode) {
      if (monitorTypeCode === 'GC_PERSON_NUM' || monitorTypeCode === 'GC_ELECTRICITY_USE' || monitorTypeCode === 'GC_WATER_USE') {
        setState({
          frequency: 'hour',
          startDate: (0, _dayjs["default"])().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: (0, _dayjs["default"])().format('YYYY-MM-DD HH:mm:ss')
        });
      } else {
        setState({
          frequency: 'day',
          startDate: (0, _dayjs["default"])().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: (0, _dayjs["default"])().format('YYYY-MM-DD HH:mm:ss')
        });
      }
    }
  }, [monitorTypeCode]);
  (0, _react.useEffect)(function () {
    startDate && endDate && getData();
  }, [startDate, endDate]);
  var getData = function getData() {
    monitorItemEcharts({
      facilityId: detail.id,
      startDate: startDate,
      endDate: endDate,
      monitorTypeCode: monitorTypeCode,
      frequency: frequency === 'month' ? 'month' : 'hour'
    }).then(function (res) {
      if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
        setState({
          echartsData: res.data
        });
      }
    });
  };
  var option = {
    title: {
      text: (_a = monitorTypeList.filter(function (item) {
        return item.monitorTypeCode === monitorTypeCode;
      })[0]) === null || _a === void 0 ? void 0 : _a.monitorTypeName,
      left: 'center',
      textStyle: {
        color: theme === 'blue' ? '#fff' : '#333'
      }
    },
    legend: {
      data: echartsData.legends,
      bottom: 10,
      textStyle: {
        color: theme === 'blue' ? '#fff' : '#333'
      }
    },
    grid: {
      top: 50,
      bottom: 50,
      right: 10,
      left: 30,
      containLabel: true
    },
    toolbox: {
      show: true,
      feature: {
        magicType: {
          type: ['bar', 'line']
        }
      },
      iconStyle: {
        color: theme === 'blue' ? '#fff' : '#333'
      }
    },
    tooltip: {
      trigger: 'axis',
      formatter: function formatter(params) {
        var _a;
        var relVal = params[0].name;
        for (var i = 0, l = params.length; i < l; i++) {
          relVal += "<br/>".concat(params[i].marker).concat(params[i].seriesName, "\uFF1A").concat(params[i].value || 0).concat(((_a = monitorTypeList.filter(function (item) {
            return item.monitorTypeCode === monitorTypeCode;
          })[0]) === null || _a === void 0 ? void 0 : _a.unit) || '');
        }
        return relVal;
      }
    },
    xAxis: {
      type: 'category',
      data: echartsData.names,
      axisLabel: {
        color: theme === 'blue' ? '#fff' : '#333'
      }
    },
    yAxis: {
      type: 'value',
      name: (_b = monitorTypeList.filter(function (item) {
        return item.monitorTypeCode === monitorTypeCode;
      })[0]) === null || _b === void 0 ? void 0 : _b.unit,
      max: echartsData.maxData,
      axisLabel: {
        color: theme === 'blue' ? '#fff' : '#333'
      }
    },
    series: echartsData.legends.map(function (item, index) {
      return {
        name: item,
        type: 'bar',
        data: echartsData.values[index],
        markLine: {
          label: {
            normal: {
              formatter: '{b}'
            }
          },
          data: echartsData.levelData.map(function (item, index) {
            return {
              yAxis: item.value,
              lineStyle: {
                normal: {
                  color: Color[index % Color.length]
                }
              },
              name: item.name
            };
          })
        }
      };
    })
  };
  var clearSearch = function clearSearch() {
    if (monitorTypeCode === 'GC_PERSON_NUM' || monitorTypeCode === 'GC_ELECTRICITY_USE' || monitorTypeCode === 'GC_WATER_USE') {
      setState({
        frequency: 'hour',
        startDate: (0, _dayjs["default"])().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
        endDate: (0, _dayjs["default"])().format('YYYY-MM-DD HH:mm:ss')
      });
    } else {
      setState({
        frequency: 'day',
        startDate: (0, _dayjs["default"])().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
        endDate: (0, _dayjs["default"])().format('YYYY-MM-DD HH:mm:ss')
      });
    }
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: 'monitor'
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: 'searchType'
  }, /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      marginRight: '10px',
      whiteSpace: 'nowrap'
    }
  }, "\u76D1\u63A7\u7C7B\u578B"), /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    value: monitorTypeCode,
    onChange: function onChange(e) {
      return setState({
        monitorTypeCode: e.target.value,
        frequency: '',
        startDate: '',
        endDate: ''
      });
    }
  }, monitorTypeList.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_antd.Radio.Button, {
      key: item.monitorTypeCode,
      style: {
        marginTop: '2px'
      },
      value: item.monitorTypeCode
    }, item.monitorTypeName);
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: 'searchType'
  }, /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      marginRight: '10px',
      whiteSpace: 'nowrap'
    }
  }, "\u7EDF\u8BA1\u5468\u671F"), /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    value: frequency,
    onChange: function onChange(e) {
      return setState({
        frequency: e.target.value
      });
    }
  }, getFrequency().map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_antd.Radio.Button, {
      key: item.value,
      value: item.value
    }, item.name);
  })), startDate && (frequency === 'month' ? /*#__PURE__*/_react["default"].createElement(RangePicker, {
    style: {
      marginLeft: '12px'
    },
    locale: _zh_CN["default"],
    picker: "month",
    disabledDate: function disabledDate(current) {
      return current && current > (0, _dayjs["default"])().endOf('day');
    },
    allowClear: false,
    value: [(0, _moment["default"])(startDate), (0, _moment["default"])(endDate)],
    onChange: function onChange(date) {
      return setState({
        startDate: (0, _dayjs["default"])(date[0]).startOf('month').format('YYYY-MM-DD HH:mm:ss'),
        endDate: (0, _dayjs["default"])(date[1]).endOf('month').format('YYYY-MM-DD HH:mm:ss')
      });
    },
    popupClassName: theme === 'blue' ? 'popupDate' : ''
  }) : /*#__PURE__*/_react["default"].createElement(_antd.DatePicker, {
    style: {
      marginLeft: '12px'
    },
    locale: _zh_CN["default"],
    disabledDate: function disabledDate(current) {
      return current && current > (0, _dayjs["default"])().endOf('day');
    },
    allowClear: false,
    value: (0, _moment["default"])(startDate),
    onChange: function onChange(date) {
      return setState({
        startDate: (0, _dayjs["default"])(date).startOf('day').format('YYYY-MM-DD HH:mm:ss'),
        endDate: (0, _dayjs["default"])(date).endOf('day').format('YYYY-MM-DD HH:mm:ss')
      });
    },
    popupClassName: theme === 'blue' ? 'popupDate' : ''
  })), "\u2003", /*#__PURE__*/_react["default"].createElement(_antd.Button, {
    type: 'primary',
    onClick: getData
  }, "\u67E5\u8BE2"), "\u2002", /*#__PURE__*/_react["default"].createElement(_antd.Button, {
    onClick: clearSearch
  }, "\u6E05\u7A7A")), /*#__PURE__*/_react["default"].createElement("div", {
    className: 'searchType',
    style: {
      justifyContent: 'space-between'
    }
  }, /*#__PURE__*/_react["default"].createElement("div", null, "\u516C\u5395\u540D\u79F0\uFF1A", detail.name), /*#__PURE__*/_react["default"].createElement("div", null, "\u6743\u5C5E\u5355\u4F4D\uFF1A", detail.belongUnitName), /*#__PURE__*/_react["default"].createElement("div", null, "\u7537\u5395\u5751\u6570\uFF1A", detail.pitTotalMale), /*#__PURE__*/_react["default"].createElement("div", null, "\u5973\u5395\u5751\u6570\uFF1A", detail.pitTotalFemale)), /*#__PURE__*/_react["default"].createElement("div", {
    className: "charts"
  }, /*#__PURE__*/_react["default"].createElement(_echartsForReact["default"], {
    option: option,
    notMerge: true,
    lazyUpdate: true,
    style: {
      height: '100%',
      width: '100%'
    }
  })));
};
var _default = exports["default"] = Monitor;
//# sourceMappingURL=Monitor.js.map
