"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 _tooltip = _interopRequireDefault(require("../tooltip"));

var _styledComponents = require("./styledComponents");

var _openData = require("../../utils/open-data");

var _search = _interopRequireDefault(require("../../components-v1/search"));

var _select = _interopRequireDefault(require("../../components-v1/select"));

var _UserSelectorPop = _interopRequireDefault(require("../../components-v1/userSelector/UserSelectorPop"));

var _ConfirmPop = _interopRequireDefault(require("../delete-confirm-pop/ConfirmPop"));

var _filterTab = _interopRequireDefault(require("./filterTab"));

var _bsGlobal = require("../../utils/bs-global");

var _request = require("../../utils/request");

var _constant = require("./constant");

var _getLocale = require("../../utils/getLocale");

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

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

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

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var FilterComp = /*#__PURE__*/function (_PureComponent) {
  _inherits(FilterComp, _PureComponent);

  var _super = _createSuper(FilterComp);

  // customizequery/condition/save
  // 500007/500000036/customizequery/condition/get?dataKey=62_500000036&pageId=40001
  function FilterComp(p) {
    var _getBSGlobal;

    var _this;

    _classCallCheck(this, FilterComp);

    _this = _super.call(this, p);
    _this.KRSetting = (_getBSGlobal = (0, _bsGlobal.getBSGlobal)('OkrAdvancedSetting')) === null || _getBSGlobal === void 0 ? void 0 : _getBSGlobal.KRSetting;

    _this.handleDeleteUser = function (id) {
      var _this$props = _this.props,
          onChange = _this$props.onChange,
          users = _this$props.users;
      onChange('users', users.filter(function (u) {
        return u.Id != id;
      }));
    };

    _this.getCycleTypes = function () {
      (0, _request.rget)('v1')('okr/cycleSetting').then(function (res) {
        var data = res.Data;
        var cycles = [{
          title: (0, _getLocale.getLocale)('OKR_MyO_Butt_AllStates'),
          value: '-1'
        }];

        if (data.isOpenYear) {
          cycles.push({
            title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Year'),
            value: '1'
          });
        }

        if (data.isOpenHalfYear) {
          cycles.push({
            title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Firsthalf'),
            value: '5,1'
          });
          cycles.push({
            title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Secondhalf'),
            value: '5,2'
          });
        }

        if (data.isOpenQuarter) {
          cycles.push({
            title: (0, _getLocale.getLocale)('OKR_MyO_Pop_1stQuarter'),
            value: '2,1'
          }, {
            title: (0, _getLocale.getLocale)('OKR_MyO_Pop_2ndQuarter'),
            value: '2,2'
          }, {
            title: (0, _getLocale.getLocale)('OKR_MyO_Pop_3rdQuarter'),
            value: '2,3'
          }, {
            title: (0, _getLocale.getLocale)('OKR_MyO_Pop_4thQuarter'),
            value: '2,4'
          });
        }

        if (data.isOpenBimonthly) {
          cycles.push.apply(cycles, _toConsumableArray((0, _constant.getBimonthlyMonthKeyValue)(data.yearStartMonth)));
        }

        if (data.isOpenMonth) {
          cycles.push.apply(cycles, _toConsumableArray((0, _constant.getMonthKeyValue)()));
        }

        if (data.isOpenCustom) {
          cycles.push({
            title: (0, _getLocale.getLocale)('Mod_Customization'),
            value: '0,0'
          });
        }

        var newOptions = _toConsumableArray(_this.state.OptionsArray);

        newOptions[1] = _objectSpread({}, newOptions[1]);
        newOptions[1].valuesArray = cycles;

        _this.setState({
          OptionsArray: newOptions,
          cycles: cycles
        });
      });
    };

    _this.handleDeleteDepartment = function (id) {
      var _this$props2 = _this.props,
          onChange = _this$props2.onChange,
          departments = _this$props2.departments;
      onChange('departments', departments.filter(function (d) {
        return d.Id != id;
      }));
    };

    _this.handleClear = function () {
      _this.props.onChange('clear');

      var _optionArray = _this.state.OptionsArray.map(function (option) {
        return _objectSpread(_objectSpread({}, option), {}, {
          value: null
        });
      }); // @ts-ignore


      _this.setState({
        OptionsArray: _optionArray
      });
    };

    _this.handlerFilterOptions = function (option) {
      var onChange = _this.props.onChange;
      option == 1 ? onChange('remembered', true) : onChange('remembered', false);
    };

    _this.handleFilterDelete = function (name) {
      var newArray = _this.state.OptionsArray.map(function (option) {
        if (option.name == name) {
          return _objectSpread(_objectSpread({}, option), {}, {
            value: null
          });
        }

        return option;
      });

      _this.props.onChange(name, null);

      _this.setState({
        OptionsArray: newArray
      });
    };

    _this.handleSelected = function (data, value, filed) {
      _this.props.onChange(filed, value);

      var _optionsArray = _this.state.OptionsArray.map(function (option) {
        if (option.name == filed) {
          return _objectSpread(_objectSpread({}, option), {}, {
            valuesArray: data,
            value: value
          });
        }

        return option;
      });

      _this.setState({
        OptionsArray: _optionsArray
      });
    };

    _this.state = {
      OptionsArray: [{
        index: 0,
        title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Year'),
        name: 'year',
        valuesArray: (0, _constant.getYear)(),
        value: p.year
      }, {
        index: 1,
        title: (0, _getLocale.getLocale)('Mod_period'),
        name: 'cycle',
        valuesArray: [],
        value: p.cycle
      }, {
        index: 2,
        title: (0, _getLocale.getLocale)('Mod_States'),
        name: 'status',
        valuesArray: (0, _constant.getStatusType)(),
        value: p.status
      }, {
        index: 3,
        title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Type'),
        name: 'type',
        valuesArray: (0, _constant.getOkrTypes)(),
        value: p.type
      }],
      cycles: []
    };
    return _this;
  }

  _createClass(FilterComp, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      var year = nextProps.year,
          cycle = nextProps.cycle,
          status = nextProps.status,
          type = nextProps.type;
      this.setState({
        OptionsArray: [{
          index: 0,
          title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Year'),
          name: 'year',
          valuesArray: (0, _constant.getYear)(),
          value: year
        }, {
          index: 1,
          title: (0, _getLocale.getLocale)('Mod_period'),
          name: 'cycle',
          valuesArray: this.state.OptionsArray[1].valuesArray,
          value: cycle
        }, {
          index: 2,
          title: (0, _getLocale.getLocale)('Mod_States'),
          name: 'status',
          valuesArray: (0, _constant.getStatusType)(),
          value: status
        }, {
          index: 3,
          title: (0, _getLocale.getLocale)('OKR_MyO_Pop_Type'),
          name: 'type',
          valuesArray: (0, _constant.getOkrTypes)(),
          value: type
        }]
      });
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.getCycleTypes();
    } // NOTE componentWillReceiveProps 会导致筛选条件无法出现 全部周期这种默认选项

  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props3 = this.props,
          year = _this$props3.year,
          cycle = _this$props3.cycle,
          status = _this$props3.status,
          type = _this$props3.type,
          users = _this$props3.users,
          onChange = _this$props3.onChange,
          departments = _this$props3.departments,
          remembered = _this$props3.remembered,
          okrPopType = _this$props3.okrPopType;
      var mapper = {
        year: year,
        cycle: cycle,
        status: status,
        type: type
      };
      var _this$state = this.state,
          OptionsArray = _this$state.OptionsArray,
          cycles = _this$state.cycles;
      return /*#__PURE__*/_react["default"].createElement(_styledComponents.Filter, null, /*#__PURE__*/_react["default"].createElement(_styledComponents.FilterTop, null, /*#__PURE__*/_react["default"].createElement(_styledComponents.FilterLeftWrapper, null, /*#__PURE__*/_react["default"].createElement(_select["default"], {
        container: document.body,
        data: (0, _constant.getYear)(),
        value: year,
        title: (0, _getLocale.getLocale)('Mod_Year'),
        textField: "title",
        valueField: "value",
        maxHeight: "280px",
        type: "old",
        width: "64px",
        onSelect: function onSelect(value) {
          return _this2.handleSelected((0, _constant.getYear)(), value, 'year');
        }
      }), /*#__PURE__*/_react["default"].createElement(_select["default"], {
        container: document.body,
        data: cycles,
        value: cycle,
        title: (0, _getLocale.getLocale)('Mod_period'),
        textField: "title",
        valueField: "value",
        maxHeight: "280px",
        type: "old",
        width: "64px",
        onSelect: function onSelect(value) {
          return _this2.handleSelected(cycles, value, 'cycle');
        }
      }), okrPopType == 1 && /*#__PURE__*/_react["default"].createElement(_select["default"], {
        container: document.body,
        data: (0, _constant.getStatusType)(),
        value: status,
        title: (0, _getLocale.getLocale)('Mod_States'),
        textField: "title",
        valueField: "value",
        maxHeight: "280px",
        type: "old",
        width: "64px",
        onSelect: function onSelect(value) {
          return _this2.handleSelected((0, _constant.getStatusType)(), value, 'status');
        }
      }), this.KRSetting.OkrTypeSetting && /*#__PURE__*/_react["default"].createElement(_select["default"], {
        container: document.body,
        data: (0, _constant.getOkrTypes)(),
        value: type,
        title: (0, _getLocale.getLocale)('Mod_Type'),
        textField: "title",
        valueField: "value",
        maxHeight: "280px",
        type: "old",
        width: "64px",
        onSelect: function onSelect(value) {
          return _this2.handleSelected((0, _constant.getOkrTypes)(), value, 'type');
        }
      }), /*#__PURE__*/_react["default"].createElement(_UserSelectorPop["default"], {
        onSubmit: this.props.onUserChange,
        container: document.body
      }, /*#__PURE__*/_react["default"].createElement(_styledComponents.UserSelectorWrapper, {
        ref: "user"
      }, /*#__PURE__*/_react["default"].createElement("span", null, (0, _getLocale.getLocale)('Mod_Owner')), /*#__PURE__*/_react["default"].createElement("i", {
        className: "tu-icon-sort-up"
      })))), /*#__PURE__*/_react["default"].createElement(_search["default"], {
        placeHolder: (0, _getLocale.getLocale)('OKR_MyO_Pop_Pleaenterwords'),
        onSearch: function onSearch(keywords) {
          return onChange('keywords', keywords);
        }
      })), (OptionsArray.filter(function (option) {
        return option.value !== null;
      }).length > 0 || !!users.length || !!departments.length) && /*#__PURE__*/_react["default"].createElement(_styledComponents.FilterBottom, null, /*#__PURE__*/_react["default"].createElement(_styledComponents.FilterOptionsWrapper, null, OptionsArray.sort(function (a, b) {
        return a.index - b.index;
      }).filter(function (option) {
        return option.value !== null;
      }).map(function (option) {
        return /*#__PURE__*/_react["default"].createElement(_filterTab["default"], {
          title: option.title,
          valuesArray: option.valuesArray,
          name: option.name,
          value: mapper[option.name],
          onClick: _this2.handleFilterDelete
        });
      }), (!!users.length || !!departments.length) && /*#__PURE__*/_react["default"].createElement("span", {
        style: {
          marginRight: 5
        }
      }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Owner')), users.map(function (u) {
        return /*#__PURE__*/_react["default"].createElement(_styledComponents.OptionItem, {
          key: u.Id
        }, /*#__PURE__*/_react["default"].createElement(_openData.OpenUserName, {
          name: u.Name,
          id: u.Id
        }), /*#__PURE__*/_react["default"].createElement("i", {
          className: "tu-icon-cross",
          onClick: function onClick() {
            return _this2.handleDeleteUser(u.Id);
          }
        }));
      }), departments.map(function (u) {
        return /*#__PURE__*/_react["default"].createElement(_styledComponents.OptionItem, {
          key: u.Id
        }, u.DepartmentName, "\u7684\u4EBA\u5458", /*#__PURE__*/_react["default"].createElement("i", {
          className: "tu-icon-cross",
          onClick: function onClick() {
            return _this2.handleDeleteDepartment(u.Id);
          }
        }));
      }), /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
        overlay: (0, _getLocale.getLocale)('Tasks_Tab_MyS_Clearselec')
      }, /*#__PURE__*/_react["default"].createElement("a", {
        onClick: this.handleClear
      }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Clearselected'))), !remembered ? /*#__PURE__*/_react["default"].createElement("a", {
        onClick: function onClick() {
          return _this2.handlerFilterOptions(1);
        }
      }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
        overlay: (0, _getLocale.getLocale)('Tasks_Tab_MyS_Nextcondit')
      }, /*#__PURE__*/_react["default"].createElement("span", null, (0, _getLocale.getLocale)('OKR_MyO_Butt_Remember')))) : /*#__PURE__*/_react["default"].createElement(_ConfirmPop["default"], {
        align: "bottom left",
        confirmTipText: (0, _getLocale.getLocale)('OKR_MyO_Err_Bemembere'),
        onSubmit: function onSubmit() {
          return _this2.handlerFilterOptions(2);
        }
      }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
        overlay: (0, _getLocale.getLocale)('OKR_MyO_Err_Showabove')
      }, /*#__PURE__*/_react["default"].createElement("a", null, (0, _getLocale.getLocale)('OKR_MyO_Pop_Cancelremember')))))));
    }
  }]);

  return FilterComp;
}(_react.PureComponent);

exports["default"] = FilterComp;