function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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)),
    _useState10 = _slicedToArray(_useState9, 2),
    time = _useState10[0],
    setTime = _useState10[1];
  // 当前页数
  var _useState11 = useState(1),
    _useState12 = _slicedToArray(_useState11, 2),
    current = _useState12[0],
    setCurrent = _useState12[1];
  // 当前页码
  var _useState13 = useState(10),
    _useState14 = _slicedToArray(_useState13, 2),
    pageSize = _useState14[0],
    setPageSize = _useState14[1];
  // 列表数据
  var _useState15 = useState([]),
    _useState16 = _slicedToArray(_useState15, 2),
    taskList = _useState16[0],
    setTaskList = _useState16[1];
  // 详情数据
  var _useState17 = useState({}),
    _useState18 = _slicedToArray(_useState17, 2),
    deatilsData = _useState18[0],
    setDeatilsData = _useState18[1];
  var _useState19 = useState(0),
    _useState20 = _slicedToArray(_useState19, 2),
    timer = _useState20[0],
    setTimer = _useState20[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;