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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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(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; }
import React, { useContext, useEffect, useState } from 'react';
import './style/index.less';
import { BizGlobalDataContext, BizSearchInput, BizTable, ajax, BizDatePicker, util } from '@zgfe/business-lib';
import { message, Empty } from 'antd';
import apis from '../../constants/api';
import { getInitDate, getColumns, getSdk } from './utils';
import DataMeasureDetails from './details';
import _ from 'lodash';
export var classPrefix = 'data-measure';
var DataMeasure = function DataMeasure() {
  var _basicConfig$privateC, _basicConfig$privateC2, _basicConfig$publicCo, _basicConfig$publicCo2;
  var _useState = useState({}),
    _useState2 = _slicedToArray(_useState, 2),
    tableSearch = _useState2[0],
    setTableSearch = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    loading = _useState4[0],
    setLoading = _useState4[1];
  var _useState5 = useState(0),
    _useState6 = _slicedToArray(_useState5, 2),
    total = _useState6[0],
    setTotal = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    open = _useState8[0],
    setOpen = _useState8[1];
  // 当前时间段
  var _useState9 = useState(getInitDate(false)),
    _useState0 = _slicedToArray(_useState9, 2),
    time = _useState0[0],
    setTime = _useState0[1];
  // 当前页数
  var _useState1 = useState(1),
    _useState10 = _slicedToArray(_useState1, 2),
    current = _useState10[0],
    setCurrent = _useState10[1];
  // 当前页码
  var _useState11 = useState(10),
    _useState12 = _slicedToArray(_useState11, 2),
    pageSize = _useState12[0],
    setPageSize = _useState12[1];
  // 列表数据
  var _useState13 = useState([]),
    _useState14 = _slicedToArray(_useState13, 2),
    taskList = _useState14[0],
    setTaskList = _useState14[1];
  // 详情数据
  var _useState15 = useState({}),
    _useState16 = _slicedToArray(_useState15, 2),
    deatilsData = _useState16[0],
    setDeatilsData = _useState16[1];
  var _useState17 = useState(0),
    _useState18 = _slicedToArray(_useState17, 2),
    timer = _useState18[0],
    setTimer = _useState18[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp,
    authority = _useContext.authority,
    basicConfig = _useContext.basicConfig;
  useEffect(function () {
    if (timer) {
      clearTimeout(timer);
    }
    if (authority[1709285576]) {
      setTimer(setTimeout(function () {
        getList();
      }, 400));
    }
  }, [tableSearch, current, pageSize, time, authority]);
  // 获取列表数据
  var getList = function getList() {
    setLoading(true);
    ajax(apis.dataMeasureList, {
      method: 'post',
      data: _objectSpread({
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        beginDate: time.begin,
        endDate: time.end,
        page: current,
        size: pageSize,
        sdk: 0
      }, tableSearch)
    }).then(function (res) {
      var _res$data;
      if (res && res.data && ((_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.list)) {
        var data = res.data;
        setTaskList(data.list.filter(function (item) {
          item.key = util.guid();
          return item;
        }));
        setTotal(data.count);
      } else {
        setTaskList([]);
        setTotal(0);
      }
    }).finally(function () {
      setLoading(false);
    });
  };
  // 获取详情数据
  var getDetails = function getDetails(record) {
    var _sdk = getSdk(record.sdk, basicConfig) || 0;
    ajax(apis.dataMeasureDetail, {
      method: 'post',
      data: {
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        sdk: _sdk,
        eventName: record.eventName,
        beginDate: time.begin,
        endDate: time.end
      }
    }).then(function (res) {
      if (!res || !res.data) {
        setDeatilsData({});
        message.error('获取详情信息失败');
        return;
      }
      setDeatilsData(res.data);
      setOpen(true);
    }).finally(function () {
      setLoading(false);
    });
  };
  // 监听分页变化
  var onChangeTable = function onChangeTable(pagination, _filters, sorter) {
    setCurrent(pagination.current);
    setPageSize(pagination.pageSize);
    if (sorter.columnKey) {
      setTableSearch(function (search) {
        var searchData = _.cloneDeep(search);
        var orderKey = {
          allCount: 0,
          successCount: 1,
          errorCount: 2,
          errorProCount: 3
        };
        searchData.orderByfield = orderKey[sorter.columnKey];
        searchData.orderByType = sorter.order === 'ascend' ? 0 : 1;
        return searchData;
      });
    }
  };
  // 数据集类型/状态改变
  var onChangeParams = function onChangeParams(data, type) {
    setCurrent(1);
    setTableSearch(function (search) {
      var searchData = _.cloneDeep(search);
      if (data === '') {
        delete searchData[type];
      } else {
        searchData[type] = data;
      }
      return searchData;
    });
  };
  var timeChange = function timeChange(data) {
    setCurrent(1);
    setTime(data);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-panel"),
    id: "sessionManageTable"
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-panel-search")
  }, /*#__PURE__*/React.createElement(BizSearchInput, {
    placeholder: "\u8BF7\u8F93\u5165\u4E8B\u4EF6\u540D\u79F0",
    onChange: function onChange(txt) {
      return onChangeParams(txt, 'eventName');
    }
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-panel-search-txt")
  }, "\u5171\u641C\u7D22\u5230", /*#__PURE__*/React.createElement("span", {
    className: "".concat(classPrefix, "-panel-search-num")
  }, total), "\u6761\u6570\u636E"), /*#__PURE__*/React.createElement(BizDatePicker, {
    mode: "superSimple",
    onChange: timeChange,
    includeToday: true,
    value: time,
    defaultValue: time,
    selectRange: (basicConfig === null || basicConfig === void 0 ? void 0 : (_basicConfig$privateC = basicConfig.privateConfig) === null || _basicConfig$privateC === void 0 ? void 0 : (_basicConfig$privateC2 = _basicConfig$privateC.dataMeasure) === null || _basicConfig$privateC2 === void 0 ? void 0 : _basicConfig$privateC2.timeRange) || (basicConfig === null || basicConfig === void 0 ? void 0 : (_basicConfig$publicCo = basicConfig.publicConfig) === null || _basicConfig$publicCo === void 0 ? void 0 : (_basicConfig$publicCo2 = _basicConfig$publicCo.dataMeasure) === null || _basicConfig$publicCo2 === void 0 ? void 0 : _basicConfig$publicCo2.timeRange) || 7,
    selectRangeUnit: "days"
  }))), function () {
    if (!authority[1709285576]) {
      return /*#__PURE__*/React.createElement(Empty, {
        image: Empty.PRESENTED_IMAGE_SIMPLE,
        description: "\u6CA1\u6709\u5217\u8868\u67E5\u8BE2\u6743\u9650"
      });
    }
    return /*#__PURE__*/React.createElement(BizTable, {
      className: "".concat(classPrefix, "-table"),
      rowKey: "key",
      loading: loading,
      dataSource: taskList,
      columns: getColumns(classPrefix, onChangeParams, getDetails, basicConfig, authority),
      onChange: onChangeTable,
      scroll: {
        x: 800
      },
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        total: total,
        current: current,
        pageSize: pageSize
      }
    });
  }(), /*#__PURE__*/React.createElement(DataMeasureDetails, {
    open: open,
    setOpen: setOpen,
    deatilsData: deatilsData,
    time: time
  }));
};
export default DataMeasure;