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

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

var _button = require("../button");

var _form = _interopRequireDefault(require("../form"));

var _localStorage = require("../../utils/local-storage");

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

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

var Fields = _form["default"].Fields;
var preCls = "tita-filter__form";
var useLocalStorage = new _localStorage.LocalStorage();

var getOkrTypeList = function getOkrTypeList() {
  return [{
    label: (0, _getLocale.getLocale)("Mod_all"),
    value: ""
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Pop_Company"),
    value: 3
  }, {
    label: (0, _getLocale.getLocale)("Mod_Department"),
    value: 2
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Text_Team"),
    value: 4
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Pop_Individual"),
    value: 1
  }];
};

var getOkrClassifyList = function getOkrClassifyList() {
  return [{
    label: (0, _getLocale.getLocale)("Mod_all"),
    value: ""
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Es_Commitment"),
    value: 1
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Es_Visiontype"),
    value: 2
  }];
};

var getOrderTypeList = function getOrderTypeList() {
  return [{
    label: (0, _getLocale.getLocale)('OKR_MyO_Text_CustomSorting'),
    value: 0,
    toolTip: (0, _getLocale.getLocale)('OKR_MyO_tips_Clickdragdrop')
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Text_Firstcreated"),
    value: 2,
    toolTip: (0, _getLocale.getLocale)("OKR_MyO_Text_First")
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Text_Latecreation"),
    value: 1,
    toolTip: (0, _getLocale.getLocale)("OKR_MyO_Text_Created")
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Text_Earliest"),
    value: 12,
    toolTip: (0, _getLocale.getLocale)("OKR_MyO_Text_Update")
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Text_Lateest"),
    value: 11,
    toolTip: (0, _getLocale.getLocale)("OKR_MyO_Text_Latest")
  }, {
    label: (0, _getLocale.getLocale)("OKR_MyO_Text_Listed"),
    value: 13
  }];
};

var Filter = function Filter(_ref) {
  var _getBSGlobal;

  var onSearch = _ref.onSearch,
      closeFilterDrawer = _ref.closeFilterDrawer,
      setFilterNum = _ref.setFilterNum;

  var _useState = (0, _react.useState)(function () {
    var values = useLocalStorage.getItem("okr-filter-values") || {
      okrType: "",
      okrClassify: "",
      orderType: 0
    };
    return values;
  }),
      _useState2 = _slicedToArray(_useState, 2),
      formFieldValues = _useState2[0],
      setFormFieldValues = _useState2[1];

  var _useState3 = (0, _react.useState)(useLocalStorage.getItem("okr-filter-isRemeber") || {
    bol: false
  }),
      _useState4 = _slicedToArray(_useState3, 2),
      isRemeber = _useState4[0],
      setIsRemeber = _useState4[1];

  var setting = (_getBSGlobal = (0, _bsGlobal.getBSGlobal)("OkrAdvancedSetting")) === null || _getBSGlobal === void 0 ? void 0 : _getBSGlobal.KRSetting;
  var openOkrType = setting.OkrTypeSetting;
  var openClassify = setting.OkrClassify;
  var formContainerRef = (0, _react.useRef)();
  var formRef = (0, _react.useRef)();

  var onFieldsChangeHandler = function onFieldsChangeHandler(fields) {
    setFormFieldValues(_objectSpread({}, fields));
  };

  var handleClose = function handleClose() {
    if (closeFilterDrawer) {
      closeFilterDrawer();
    }
  };

  var handleRemember = function handleRemember(checked) {
    setIsRemeber({
      bol: checked
    });
  };

  var handleSubmit = function handleSubmit() {
    var num = 0;

    if (isRemeber.bol) {
      useLocalStorage.setItem("okr-filter-values", formFieldValues);
    }

    if (!isRemeber.bol) {
      useLocalStorage.setItem("okr-filter-values", {
        okrType: "",
        okrClassify: "",
        orderType: 0
      });
    }

    if (onSearch) {
      onSearch({
        okrType: formFieldValues.okrType,
        okrClassify: formFieldValues.okrClassify,
        orderType: formFieldValues.orderType
      });
    }

    if (formFieldValues.okrType) {
      num += 1;
    }

    if (formFieldValues.okrClassify) {
      num += 1;
    }

    if (setFilterNum) {
      setFilterNum(num);
    }

    if (closeFilterDrawer) {
      closeFilterDrawer();
    }

    useLocalStorage.setItem("okr-filter-isRemeber", isRemeber);
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__content")
  }, /*#__PURE__*/_react["default"].createElement(_form["default"], {
    ref: formRef,
    getFormContainer: function getFormContainer() {
      return formContainerRef.current;
    },
    onFieldsChange: onFieldsChangeHandler
  }, openOkrType ? /*#__PURE__*/_react["default"].createElement(Fields.Radios, {
    name: "okrType",
    label: (0, _getLocale.getLocale)("OKR_MyO_Pop_Objectivetype"),
    radios: getOkrTypeList(),
    value: formFieldValues.okrType,
    radioClass: "".concat(preCls, "-radio")
  }) : /*#__PURE__*/_react["default"].createElement("span", null), openClassify ? /*#__PURE__*/_react["default"].createElement(Fields.Radios, {
    name: "okrClassify",
    label: (0, _getLocale.getLocale)("OKR_MyO_Text_Category"),
    radios: getOkrClassifyList(),
    value: formFieldValues.okrClassify,
    radioClass: "".concat(preCls, "-radio")
  }) : /*#__PURE__*/_react["default"].createElement("span", null), /*#__PURE__*/_react["default"].createElement(Fields.Radios, {
    name: "orderType",
    label: (0, _getLocale.getLocale)("OKR_MyO_Text_Sorting"),
    radios: getOrderTypeList(),
    value: formFieldValues.orderType,
    radioClass: "".concat(preCls, "-radio")
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__footer")
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(preCls, "__footer-checkbox")
  }, /*#__PURE__*/_react["default"].createElement(_checkbox["default"], {
    checked: isRemeber.bol,
    onChange: handleRemember
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(preCls, "__footer-checkbox-text")
  }, (0, _getLocale.getLocale)("OKR_MyO_Butt_Remember"))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__footer-btns")
  }, /*#__PURE__*/_react["default"].createElement(_button.RectBtn, {
    type: "cancel",
    className: "".concat(preCls, "__footer-reset"),
    onClick: handleClose
  }, (0, _getLocale.getLocale)("Mod_Cancel")), /*#__PURE__*/_react["default"].createElement(_button.RectBtn, {
    className: "".concat(preCls, "__footer-sure"),
    type: "primary",
    onClick: handleSubmit
  }, (0, _getLocale.getLocale)("OKR_MyO_Butt_Determine")))));
};

Filter.defaultProps = {
  onSearch: function onSearch() {},
  closeFilterDrawer: function closeFilterDrawer() {},
  setFilterNum: function setFilterNum() {}
};
var _default = Filter;
exports["default"] = _default;