"use strict";

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); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _tableExcel = _interopRequireDefault(require("../table-excel"));

var _hooks = require("./hooks");

var _hooks2 = require("../../utils/hooks");

var _popupSelect = _interopRequireDefault(require("../popup-select"));

var _utils = require("./utils");

var _krWeight = _interopRequireDefault(require("./components/kr-weight"));

var _okrAlign = _interopRequireDefault(require("./components/okr-align"));

var _okrCycle = _interopRequireDefault(require("./components/okr-cycle"));

var _okrPrincipal = _interopRequireDefault(require("./components/okr-principal"));

var _krConfidence = _interopRequireDefault(require("./components/kr-confidence"));

var _okrClassify = _interopRequireDefault(require("./components/okr-classify"));

var _okrType = _interopRequireDefault(require("./components/okr-type"));

var _okrVisibility = _interopRequireDefault(require("./components/okr-visibility"));

var _okrName = _interopRequireDefault(require("./components/okr-name"));

require("ag-grid-community/styles/ag-grid.css");

require("ag-grid-community/styles/ag-theme-material.css");

require("./index.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var defaultColumnOption = {
  resizable: true,
  editable: false
};
var defaultSpanOption = {
  rowSpan: function rowSpan(_ref) {
    var _ref$data$rowSpan = _ref.data.rowSpan,
        _rowSpan = _ref$data$rowSpan === void 0 ? 0 : _ref$data$rowSpan;

    return _rowSpan;
  },
  cellClassRules: {
    'cell-span': 'data.rowSpan > 1'
  }
};
var defaultColSpanOption = {
  colSpan: function colSpan(_ref2) {
    var _ref2$data$colSpan = _ref2.data.colSpan,
        _colSpan = _ref2$data$colSpan === void 0 ? 0 : _ref2$data$colSpan;

    return _colSpan;
  },
  cellClassRules: {
    'cell-span': 'data.colSpan > 1'
  }
};
var dicData = (0, _hooks.dic)();
var preCls = 'tita-okr-excel-create';
var initDatas = [_utils.initData, _objectSpread(_objectSpread({}, JSON.parse(JSON.stringify(_utils.initData))), {}, {
  workId: 1
}), _objectSpread(_objectSpread({}, JSON.parse(JSON.stringify(_utils.initData))), {}, {
  workId: 2
})];

var OKRExcelCreate = function OKRExcelCreate() {
  var data = (0, _react.useRef)(initDatas);

  var _useState = (0, _react.useState)((0, _utils.formatDataToOKRExcelData)(data.current)),
      _useState2 = _slicedToArray(_useState, 2),
      excelData = _useState2[0],
      setExcelData = _useState2[1];

  var update = (0, _react.useCallback)(function (_ref3) {
    var type = _ref3.type,
        itemData = _ref3.itemData;

    if (type === 'AddKR') {
      var index = data.current.findIndex(function (item) {
        return item.workId === itemData.workId;
      });
      var krs = data.current[index].krs;
      krs.splice(krs.length - 2, 0, _objectSpread(_objectSpread({}, JSON.parse(JSON.stringify(_utils.krItemData))), {}, {
        mileStoneId: krs.length - 1
      }));
    }

    setExcelData((0, _utils.formatDataToOKRExcelData)(JSON.parse(JSON.stringify(data.current))));
  }, []);
  var OkrColumnOptions = (0, _react.useMemo)(function () {
    return [_objectSpread(_objectSpread(_objectSpread({}, defaultColumnOption), defaultSpanOption), {}, {
      field: dicData['目标（O）'],
      width: 400,
      valueGetter: 'data.name',
      pinned: 'left',
      // autoHeight: true,
      wrapText: true,
      cellRenderer: (0, _okrName["default"])(update, true)
    }), _objectSpread(_objectSpread(_objectSpread({}, defaultColumnOption), defaultSpanOption), {}, {
      field: dicData['目标类型'],
      valueGetter: 'data.okrType',
      hide: false,
      width: 100,
      cellRenderer: (0, _okrType["default"])(update)
    }), _objectSpread(_objectSpread(_objectSpread({}, defaultColumnOption), defaultSpanOption), {}, {
      field: dicData['目标类别'],
      valueGetter: 'data.okrClassify',
      width: 120,
      cellRenderer: (0, _okrClassify["default"])(update)
    }), _objectSpread(_objectSpread(_objectSpread({}, defaultColumnOption), defaultSpanOption), {}, {
      field: dicData['目标周期'],
      valueGetter: 'data.cycle',
      width: 120,
      cellRenderer: (0, _okrCycle["default"])(update)
    }), _objectSpread(_objectSpread(_objectSpread({}, defaultColumnOption), defaultSpanOption), {}, {
      field: dicData['对齐目标'],
      valueGetter: 'data.parentOKR.principalUser.name',
      width: 100,
      hide: false,
      cellRenderer: (0, _okrAlign["default"])(update)
    }), _objectSpread(_objectSpread(_objectSpread({}, defaultColumnOption), defaultSpanOption), {}, {
      field: dicData['目标负责人'],
      valueGetter: 'data.principalUser.name',
      hide: false,
      width: 100,
      cellRenderer: (0, _okrPrincipal["default"])(update)
    }), _objectSpread(_objectSpread(_objectSpread({}, defaultColumnOption), defaultSpanOption), {}, {
      field: dicData['可见范围'],
      valueGetter: 'data.visibility',
      width: 100,
      hide: false,
      cellRenderer: (0, _okrVisibility["default"])(update)
    }), _objectSpread(_objectSpread(_objectSpread({}, defaultColumnOption), defaultColSpanOption), {}, {
      field: dicData['关键成果（KR）'],
      valueGetter: 'data.kr.name',
      width: 400,
      cellRenderer: (0, _okrName["default"])(update, false)
    }), _objectSpread(_objectSpread({}, defaultColumnOption), {}, {
      field: dicData['KR 负责人'],
      valueGetter: 'data.kr.principalUser.name',
      width: 100,
      hide: false,
      cellRenderer: (0, _okrPrincipal["default"])(update)
    }), _objectSpread(_objectSpread({}, defaultColumnOption), {}, {
      field: dicData['KR 权重'],
      valueGetter: 'data.kr.weight',
      width: 80,
      hide: false,
      cellRenderer: (0, _krWeight["default"])(update)
    }), _objectSpread(_objectSpread({}, defaultColumnOption), {}, {
      field: dicData['信心指数'],
      valueGetter: 'data.kr.weight',
      width: 80,
      hide: false,
      cellRenderer: (0, _krConfidence["default"])(update)
    })];
  }, []);

  var _useOkrColumns = (0, _hooks.useOkrColumns)(OkrColumnOptions),
      columnDefs = _useOkrColumns.columnDefs,
      onColumnMoved = _useOkrColumns.onColumnMoved,
      onColumnResized = _useOkrColumns.onColumnResized,
      columnCtlData = _useOkrColumns.columnCtlData,
      onColumnsHideOrShow = _useOkrColumns.onColumnsHideOrShow;

  var getRowId = (0, _react.useMemo)(function () {
    return function (params) {
      var _params$data, _params$data$kr;

      return (_params$data = params.data) === null || _params$data === void 0 ? void 0 : (_params$data$kr = _params$data.kr) === null || _params$data$kr === void 0 ? void 0 : _params$data$kr.mileStoneId;
    };
  }, []);
  var onColumnMovedHandler = (0, _react.useCallback)(function (event) {
    var moveField = event.column.colId;
    var toIndex = event.toIndex;
    onColumnMoved(moveField, toIndex);
  }, [onColumnMoved]);
  var onColumnResizedHandler = (0, _hooks2.useDebounceFn)(function (event) {
    var _event$column = event.column,
        actualWidth = _event$column.actualWidth,
        colId = _event$column.colId;
    onColumnResized(colId, actualWidth);
  }, {
    wait: 200
  });
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: preCls
  }, /*#__PURE__*/_react["default"].createElement(_popupSelect["default"], {
    menuData: columnCtlData,
    popupPlacement: "bottomLeft",
    action: "click",
    mode: "checkbox",
    height: 292,
    onChange: onColumnsHideOrShow,
    autoClose: false
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__column-ctl")
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "tu-icon-APP-shezhi"
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "ag-theme-material"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "ag-theme-alpine"
  }, /*#__PURE__*/_react["default"].createElement(_tableExcel["default"], {
    headerHeight: 40,
    rowHeight: 38,
    rowData: excelData,
    columnDefs: columnDefs,
    getRowId: getRowId,
    onColumnMoved: onColumnMovedHandler,
    onColumnResized: onColumnResizedHandler.run,
    suppressRowTransform: true,
    enableRangeSelection: true,
    detailRowAutoHeight: true,
    domLayout: "autoHeight"
  }))));
};

var _default = OKRExcelCreate;
exports["default"] = _default;