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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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(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(arr) { if (Array.isArray(arr)) return arr; }
import { request } from '@vtx/utils';
import { useSetState } from 'ahooks';
import { Alert, Tabs } from 'antd';
import dayjs from 'dayjs';
import { useEffect, useState } from 'react';
import { VmLineCharts, VmTable } from "../../../_components";
import { VMDatePicker, VMRangePicker, VMSelect } from "../../../_components/vm-filter";
import Jbxx from "./Jbxx";
import Ptss from "./Ptss";

//公厕详情
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var getDetail = function getDetail(params) {
  return request.get('/cloud/zhgc-reborn/api/gis/pop/getById', {
    data: params
  });
};
//公厕配置字段
var getFields = function getFields(params) {
  return request.get('/cloud/jcss-reborn/facility/type/view/detail', {
    data: params
  });
};

//监测指标
var getMonitorType = function getMonitorType(params) {
  return request.get('/cloud/zhgc-reborn/api/kanban/getMonitorType', {
    data: params
  });
};
var monitorItemEcharts = function monitorItemEcharts(params) {
  return request.get('/cloud/zhgc-reborn/api/kanban/monitorItemEcharts', {
    data: params
  });
};
var getAlarmList = function getAlarmList(params) {
  return request.post('/cloud/device/alarmInfo/list', {
    data: JSON.stringify(params)
  });
};
export default (function (props) {
  var _monitorTypeList$filt;
  var theme = props.theme,
    id = props.id,
    detail = props.detail;
  var _useState = useState({}),
    _useState2 = _slicedToArray(_useState, 2),
    gcDetail = _useState2[0],
    setGcDetail = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    fields = _useState4[0],
    setFields = _useState4[1];
  var _useState5 = useState('data'),
    _useState6 = _slicedToArray(_useState5, 2),
    activeKey = _useState6[0],
    setActiveKey = _useState6[1];
  var _useSetState = useSetState({
      monitorTypeList: [],
      monitorTypeCode: '',
      frequency: '',
      startDate: '',
      endDate: '',
      echartsData: {
        legends: [],
        names: [],
        values: [],
        maxData: 0,
        levelData: []
      },
      date: dayjs().format('YYYY-MM-DD'),
      alarmList: []
    }),
    _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,
    date = state.date,
    alarmList = state.alarmList;
  useEffect(function () {
    if (id) {
      getDetail({
        id: id
      }).then(function (res) {
        if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
          setGcDetail(res.data || {});
        }
      });
      getFields({
        code: 'wr'
      }).then(function (res) {
        if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
          setFields(res.data.baseFields.concat(res.data.customFields));
        }
      });
      getMonitorType({
        facilityId: id
      }).then(function (res) {
        if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
          if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
            var _res$data$, _res$data$2;
            var _monitorTypeCode = (_res$data$ = res.data[0]) === null || _res$data$ === void 0 ? void 0 : _res$data$.monitorTypeCode;
            setState({
              monitorTypeList: res.data,
              monitorTypeCode: (_res$data$2 = res.data[0]) === null || _res$data$2 === void 0 ? void 0 : _res$data$2.monitorTypeCode,
              frequency: _monitorTypeCode === 'GC_PERSON_NUM' || _monitorTypeCode === 'GC_ELECTRICITY_USE' || _monitorTypeCode === 'GC_WATER_USE' ? 'hour' : 'day',
              startDate: dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
              endDate: dayjs().format('YYYY-MM-DD HH:mm:ss')
            });
          }
        }
      });
    }
  }, [id]);
  var store = _objectSpread(_objectSpread({}, props), {}, {
    gcDetail: gcDetail,
    fields: fields
  });
  var columns = [{
    title: '监测指标',
    dataIndex: 'factorName',
    key: 'factorName',
    width: 100
  }, {
    title: '报警等级',
    dataIndex: 'alarmLevelName',
    key: 'alarmLevelName',
    width: 100
  }, {
    title: '监测开始时间',
    dataIndex: 'alarmStartTime',
    key: 'alarmStartTime',
    width: 100
  }, {
    title: '监测结束时间',
    dataIndex: 'alarmEndTime',
    key: 'alarmEndTime',
    width: 100
  }];
  var tableProps = {
    columns: columns,
    height: 270,
    isIndex: false,
    pagination: false,
    dataSource: alarmList
  };
  var items = [{
    label: '监测数据',
    key: 'data',
    children: /*#__PURE__*/_jsx(VmLineCharts, {
      height: 300,
      xAxis: echartsData.names || [],
      dataList: (echartsData.values || []).map(function (item, index) {
        return {
          name: echartsData.legends[index],
          data: item,
          smooth: true
        };
      }),
      unit: ((_monitorTypeList$filt = monitorTypeList.filter(function (item) {
        return item.monitorTypeCode === monitorTypeCode;
      })[0]) === null || _monitorTypeList$filt === void 0 ? void 0 : _monitorTypeList$filt.unit) || ''
    })
  },
  // 务必填写 key
  {
    label: '报警列表',
    key: 'list',
    children: /*#__PURE__*/_jsx("div", {
      style: {
        width: '700px',
        margin: '0 auto'
      },
      children: /*#__PURE__*/_jsx(VmTable, _objectSpread({}, tableProps))
    })
  }];
  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;
    }
  };
  useEffect(function () {
    switch (frequency) {
      case 'hour':
        setState({
          startDate: dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: dayjs().format('YYYY-MM-DD HH:mm:ss')
        });
        break;
      case 'month':
        setState({
          startDate: dayjs().startOf('month').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: dayjs().format('YYYY-MM-DD HH:mm:ss')
        });
        break;
      case 'day':
        setState({
          startDate: dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: dayjs().format('YYYY-MM-DD HH:mm:ss')
        });
        break;
      case 'yesterday':
        setState({
          startDate: dayjs().subtract(1, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: dayjs().subtract(1, 'day').endOf('day').format('YYYY-MM-DD HH:mm:ss')
        });
        break;
      case 'yesterdayB':
        setState({
          startDate: dayjs().subtract(2, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: dayjs().subtract(2, 'day').endOf('day').format('YYYY-MM-DD HH:mm:ss')
        });
        break;
    }
  }, [frequency]);
  useEffect(function () {
    if (monitorTypeCode) {
      if (monitorTypeCode === 'GC_PERSON_NUM' || monitorTypeCode === 'GC_ELECTRICITY_USE' || monitorTypeCode === 'GC_WATER_USE') {
        setState({
          frequency: 'hour',
          startDate: dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: dayjs().format('YYYY-MM-DD HH:mm:ss')
        });
      } else {
        setState({
          frequency: 'day',
          startDate: dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
          endDate: dayjs().format('YYYY-MM-DD HH:mm:ss')
        });
      }
    }
  }, [monitorTypeCode]);
  useEffect(function () {
    startDate && endDate && activeKey === 'data' && (gcDetail === null || gcDetail === void 0 ? void 0 : gcDetail.id) && getData();
  }, [startDate, endDate, activeKey, gcDetail]);
  var getData = function getData() {
    monitorItemEcharts({
      facilityId: gcDetail.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 getList = function getList() {
    return getAlarmList({
      facilityIds: [gcDetail.id],
      alarmStatuses: ['OCCURRING'],
      alarmTypes: ['FACTOR'],
      alarmStartTimeBegin: dayjs(date).startOf('day').format('YYYY-MM-DD HH:mm:ss'),
      alarmStartTimeEnd: dayjs(date).endOf('day').format('YYYY-MM-DD HH:mm:ss')
    }).then(function (res) {
      if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
        setState({
          alarmList: res.data
        });
      }
    });
  };
  useEffect(function () {
    date && activeKey === 'list' && (gcDetail === null || gcDetail === void 0 ? void 0 : gcDetail.id) && getList();
  }, [date, activeKey, gcDetail]);
  return /*#__PURE__*/_jsxs("div", {
    className: "wr-base-content",
    children: [detail.fallDownAlarm && /*#__PURE__*/_jsx("div", {
      className: "wr-base-alart",
      children: /*#__PURE__*/_jsx(Alert, {
        message: /*#__PURE__*/_jsxs("div", {
          children: [/*#__PURE__*/_jsx("img", {
            src: require("./assets/alert.png")
          }), "\u2002 ", detail.fallDownAlarm]
        }),
        type: "warning",
        closable: true
      })
    }), /*#__PURE__*/_jsxs("div", {
      className: "wr-base-left",
      children: [/*#__PURE__*/_jsx("div", {
        className: "wr-base-title",
        children: "\u57FA\u672C\u4FE1\u606F"
      }), /*#__PURE__*/_jsx(Jbxx, _objectSpread({}, store))]
    }), /*#__PURE__*/_jsxs("div", {
      className: "wr-base-right",
      children: [/*#__PURE__*/_jsxs("div", {
        className: "wr-base-sjjg",
        style: {
          height: gcDetail.hasDevice ? '270px' : '100%'
        },
        children: [/*#__PURE__*/_jsx("div", {
          className: "wr-base-title",
          children: "\u914D\u5957\u8BBE\u65BD"
        }), /*#__PURE__*/_jsx(Ptss, _objectSpread({}, store))]
      }), gcDetail.hasDevice && /*#__PURE__*/_jsxs("div", {
        className: "wr-base-jcbj",
        children: [/*#__PURE__*/_jsx(Tabs, {
          items: items,
          onChange: function onChange(value) {
            return setActiveKey(value);
          }
        }), activeKey === 'data' && /*#__PURE__*/_jsxs("div", {
          className: "wr-jcbj-search",
          children: [getFrequency().map(function (item) {
            return /*#__PURE__*/_jsx("div", {
              style: {
                cursor: 'pointer',
                color: item.value === frequency ? theme === 'blue' ? '#009DFF' : '#1677FF' : theme === 'blue' ? '#fff' : '#1f1f1f'
              },
              onClick: function onClick() {
                return setState({
                  frequency: item.value
                });
              },
              children: item.name
            }, item.value);
          }), startDate && (frequency === 'month' ? /*#__PURE__*/_jsx(VMRangePicker, {
            style: {
              width: 200
            },
            picker: "month",
            value: [dayjs(startDate), dayjs(endDate)],
            disabledDate: function disabledDate(current) {
              return current && current > dayjs().endOf('month');
            },
            onChange: function onChange(date) {
              return setState({
                startDate: dayjs(date[0]).startOf('month').format('YYYY-MM-DD HH:mm:ss'),
                endDate: dayjs(date[1]).endOf('month').format('YYYY-MM-DD HH:mm:ss')
              });
            }
          }) : /*#__PURE__*/_jsx(VMDatePicker, {
            style: {
              width: 140
            },
            value: dayjs(startDate),
            disabledDate: function disabledDate(current) {
              return current && current > dayjs().endOf('day');
            },
            onChange: function onChange(date) {
              return setState({
                startDate: dayjs(date).startOf('day').format('YYYY-MM-DD HH:mm:ss'),
                endDate: dayjs(date).endOf('day').format('YYYY-MM-DD HH:mm:ss')
              });
            }
          })), /*#__PURE__*/_jsx(VMSelect, {
            style: {
              width: 140
            },
            options: monitorTypeList.map(function (item) {
              return {
                label: item.monitorTypeName,
                value: item.monitorTypeCode
              };
            }),
            value: monitorTypeCode,
            onChange: function onChange(value) {
              return setState({
                monitorTypeCode: value,
                frequency: '',
                startDate: '',
                endDate: ''
              });
            }
          })]
        }), activeKey === 'list' && /*#__PURE__*/_jsx("div", {
          className: "wr-jcbj-search",
          children: /*#__PURE__*/_jsx(VMDatePicker, {
            style: {
              width: 140
            },
            value: dayjs(date),
            disabledDate: function disabledDate(current) {
              return current && current > dayjs().endOf('day');
            },
            onChange: function onChange(_, dateStr) {
              return setState({
                date: dateStr
              });
            }
          })
        })]
      })]
    })]
  });
});