"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _lodash = _interopRequireDefault(require("lodash"));
var _FormModal = _interopRequireDefault(require("../../../../../FormModal"));
var _serviceConfig = _interopRequireDefault(require("../../../../../../_mapUtil/serviceConfig"));
var _config = _interopRequireDefault(require("../../../../../../_mapUtil/config"));
var _PlusOutlined = _interopRequireDefault(require("@ant-design/icons/PlusOutlined"));
var _message = _interopRequireDefault(require("antd/lib/message"));
var _button = _interopRequireDefault(require("antd/lib/button"));
var _config2 = _interopRequireDefault(require("../config"));
var _config3 = _interopRequireDefault(require("../../../../Components/config"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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 _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 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(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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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(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(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(arr) { if (Array.isArray(arr)) return arr; }
var getAddModalValue = _config3["default"].getAddModalValue;
var getOrgList = _serviceConfig["default"].getOrgList,
  getPeopleListByDispose = _serviceConfig["default"].getPeopleListByDispose,
  getUserToPhone = _serviceConfig["default"].getUserToPhone;
var getTreeCheckInfo = _config2["default"].getTreeCheckInfo,
  clearFormData = _config2["default"].clearFormData;
var DeptFormItem = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _props$modalVal = props.modalVal,
    modalVal = _props$modalVal === void 0 ? {} : _props$modalVal,
    type = props.type;
  var myForm = (0, _react.useRef)();
  var _useState = (0, _react.useState)([]),
    _useState2 = _slicedToArray(_useState, 2),
    orgData = _useState2[0],
    setOrgData = _useState2[1]; // 部门数据
  var _useState3 = (0, _react.useState)({}),
    _useState4 = _slicedToArray(_useState3, 2),
    peopleData = _useState4[0],
    setPeopleData = _useState4[1]; // 责任人
  var _useState5 = (0, _react.useState)({}),
    _useState6 = _slicedToArray(_useState5, 2),
    otherFormItems = _useState6[0],
    setOtherFormItems = _useState6[1];
  var _useState7 = (0, _react.useState)([]),
    _useState8 = _slicedToArray(_useState7, 2),
    peopleOption = _useState8[0],
    setPeopleOption = _useState8[1]; // 所有责任人下拉选数据（用于二次办理回显）
  var _useState9 = (0, _react.useState)({}),
    _useState10 = _slicedToArray(_useState9, 2),
    deptFormData = _useState10[0],
    setDeptFormData = _useState10[1];
  var _useState11 = (0, _react.useState)(1),
    _useState12 = _slicedToArray(_useState11, 2),
    currentIndex = _useState12[0],
    setCurrentIndex = _useState12[1];
  (0, _react.useEffect)(function () {
    getOrgList(function (data) {
      setOrgData(data);
    }, {
      isLast: true
    });
  }, []);

  // 获取部门下的责任人---办理部门下拉选修改
  var handleDisposeDep = function handleDisposeDep(value, status, changeItme) {
    var peopleParams = getTreeCheckInfo(orgData, value) || {};
    var params = {};
    if ((peopleParams === null || peopleParams === void 0 ? void 0 : peopleParams.nodeType) === 'department') {
      params = {
        departmentId: peopleParams === null || peopleParams === void 0 ? void 0 : peopleParams.value
      };
    } else if ((peopleParams === null || peopleParams === void 0 ? void 0 : peopleParams.nodeType) === 'org') {
      var _peopleParams$attribu;
      params = {
        departmentId: peopleParams === null || peopleParams === void 0 || (_peopleParams$attribu = peopleParams.attributes) === null || _peopleParams$attribu === void 0 ? void 0 : _peopleParams$attribu.departmentId,
        orgId: peopleParams === null || peopleParams === void 0 ? void 0 : peopleParams.value
      };
    }
    clearFormData(myForm, _defineProperty(_defineProperty({}, "disposeDeptDutyMan_".concat(status), undefined), "phone_".concat(status), undefined));
    getPeopleListByDispose(_objectSpread({}, params), function (data) {
      var list = _objectSpread(_objectSpread({}, peopleData), {}, _defineProperty({}, status, data));
      if (changeItme) {
        changeOtherFormItems(data, status);
      }
      setPeopleData(list);
    });
  };

  // 新增部门更新负责人下拉选
  var changeOtherFormItems = function changeOtherFormItems() {
    var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    var status = arguments.length > 1 ? arguments[1] : undefined;
    var currentItem = otherFormItems["".concat(status)];
    (currentItem === null || currentItem === void 0 ? void 0 : currentItem.length) && currentItem.map(function (val) {
      var _val$paramName;
      if ((val === null || val === void 0 || (_val$paramName = val.paramName) === null || _val$paramName === void 0 ? void 0 : _val$paramName.indexOf('disposeDeptDutyMan')) !== -1 && val !== null && val !== void 0 && val.itemProps) {
        val.itemProps.options = data;
        val.itemProps.onChange = function (value) {
          handleDisposeDepPeople(data, value, status);
        };
      }
    });
    otherFormItems["".concat(status)] = _toConsumableArray(currentItem);
    setOtherFormItems(_objectSpread({}, otherFormItems));
  };

  // 办理人更改
  var handleDisposeDepPeople = function handleDisposeDepPeople() {
    var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    var value = arguments.length > 1 ? arguments[1] : undefined;
    var status = arguments.length > 2 ? arguments[2] : undefined;
    var staffIdItem = options.find(function (val) {
      return (val === null || val === void 0 ? void 0 : val.value) === value;
    }) || {};
    getUserToPhone({
      staffId: staffIdItem === null || staffIdItem === void 0 ? void 0 : staffIdItem.staffId
    }, function (data) {
      var _myForm$current, _myForm$current2;
      (myForm === null || myForm === void 0 || (_myForm$current = myForm.current) === null || _myForm$current === void 0 || (_myForm$current = _myForm$current.form) === null || _myForm$current === void 0 ? void 0 : _myForm$current.setFieldsValue) && (myForm === null || myForm === void 0 || (_myForm$current2 = myForm.current) === null || _myForm$current2 === void 0 || (_myForm$current2 = _myForm$current2.form) === null || _myForm$current2 === void 0 ? void 0 : _myForm$current2.setFieldsValue(_defineProperty({}, "phone_".concat(status), data)));
    });
  };

  // 办理部门限制
  var getNewOrgData = function getNewOrgData() {
    var _myForm$current3;
    var _orgData = _lodash["default"].cloneDeep(orgData);
    var formItems = (myForm === null || myForm === void 0 || (_myForm$current3 = myForm.current) === null || _myForm$current3 === void 0 || (_myForm$current3 = _myForm$current3.form) === null || _myForm$current3 === void 0 ? void 0 : _myForm$current3.getFieldsValue) && myForm.current.form.getFieldsValue() || {};
    var deptItems = [];
    for (var key in formItems) {
      if (key.indexOf('disposeDeptId') !== -1) {
        deptItems.push(formItems === null || formItems === void 0 ? void 0 : formItems[key]);
      }
    }
    var renderTree = function renderTree(data) {
      var _data$children;
      if (deptItems.includes(data === null || data === void 0 ? void 0 : data.value)) {
        data.disabled = true;
      }
      if (data !== null && data !== void 0 && (_data$children = data.children) !== null && _data$children !== void 0 && _data$children.length) {
        data.children.map(function (val) {
          renderTree(val);
        });
      }
    };
    _orgData.map(function (item) {
      renderTree(item);
    });
    return _orgData;
  };
  var getDeptitems = function getDeptitems(_currentIndex) {
    var _orgData = getNewOrgData();
    return [{
      type: 'treeSelect',
      label: '办理部门',
      paramName: "disposeDeptId_".concat(_currentIndex),
      paramNameStr: "disposeDeptName_".concat(_currentIndex),
      itemProps: {
        disabled: type,
        treeData: _orgData,
        treeDefaultExpandAll: true,
        onChange: function onChange(value) {
          handleDisposeDep(value, _currentIndex, true);
        }
      },
      formItemLayout: {
        rules: [_objectSpread({}, _config["default"].reg.required)]
      }
    }, {
      type: 'select',
      label: '办理人',
      paramName: "disposeDeptDutyMan_".concat(_currentIndex),
      paramNameStr: "disposeDeptDutyManStr_".concat(_currentIndex),
      itemProps: {
        disabled: type,
        options: peopleOption,
        onChange: function onChange(value) {
          handleDisposeDepPeople([], value, _currentIndex);
        }
      },
      formItemLayout: {
        rules: [_objectSpread({}, _config["default"].reg.required)]
      }
    }, {
      type: 'input',
      label: '联系方式',
      paramName: "phone_".concat(_currentIndex),
      itemProps: {
        disabled: true
      },
      weights: type ? 1 : 0.75
    }];
  };

  // 新增处理部门参数
  var onAddDept = function onAddDept() {
    var _myForm$current4;
    var formListValues = (myForm === null || myForm === void 0 || (_myForm$current4 = myForm.current) === null || _myForm$current4 === void 0 || (_myForm$current4 = _myForm$current4.form) === null || _myForm$current4 === void 0 ? void 0 : _myForm$current4.getFieldsValue) && myForm.current.form.getFieldsValue();
    var flag = false;
    for (var key in formListValues) {
      if ((key === null || key === void 0 ? void 0 : key.indexOf('disposeDeptId')) !== -1 && !(formListValues !== null && formListValues !== void 0 && formListValues[key])) {
        flag = true;
      }
    }
    if (flag) {
      return _message["default"].warning('办理部门不能为空！');
    } else {
      var _currentIndex = _lodash["default"].cloneDeep(currentIndex);
      var initItems = getDeptitems(_currentIndex);
      otherFormItems["".concat(_currentIndex)] = _toConsumableArray(initItems);
      setOtherFormItems(_objectSpread({}, otherFormItems));
      setCurrentIndex(_currentIndex + 1);
    }
  };

  // 删除处理部门参数
  var onDeleteDept = function onDeleteDept(status) {
    otherFormItems === null || otherFormItems === void 0 || delete otherFormItems[status];
    peopleData === null || peopleData === void 0 || delete peopleData[status]; // 删除负责人下拉选数据
    setPeopleData(peopleData);
    setOtherFormItems(_objectSpread({}, otherFormItems)); // 删除form表单
  };
  var getOtherFormItems = function getOtherFormItems() {
    var _Object$keys;
    var newItems = [];
    (_Object$keys = Object.keys(otherFormItems)) !== null && _Object$keys !== void 0 && _Object$keys.length ? Object.keys(otherFormItems).map(function (key, index) {
      newItems.push.apply(newItems, _toConsumableArray([].concat(_toConsumableArray(otherFormItems["".concat(key)]), _toConsumableArray(type ? [] : [{
        type: 'blank',
        content: /*#__PURE__*/_react["default"].createElement("div", {
          style: {
            marginTop: '30px'
          },
          onClick: function onClick() {
            return onDeleteDept(key);
          }
        }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
          type: "primary"
        }, "\u5220\u9664")),
        weights: 0.25
      }])) || []));
    }) : [];
    return newItems;
  };
  var items = [{
    type: 'treeSelect',
    label: '办理部门',
    paramName: "disposeDeptId_0",
    paramNameStr: "disposeDeptName_0",
    itemProps: {
      treeData: orgData,
      treeDefaultExpandAll: true,
      onChange: function onChange(value) {
        handleDisposeDep(value, '0');
      }
    },
    formItemLayout: {
      rules: [_objectSpread({}, _config["default"].reg.required)],
      initialValue: deptFormData === null || deptFormData === void 0 ? void 0 : deptFormData.disposeDeptId_0
    }
  }, {
    type: 'select',
    label: '办理人',
    paramName: "disposeDeptDutyMan_0",
    paramNameStr: "disposeDeptDutyManStr_0",
    itemProps: {
      options: type ? peopleOption : (peopleData === null || peopleData === void 0 ? void 0 : peopleData['0']) || [],
      onChange: function onChange(value) {
        handleDisposeDepPeople(peopleData === null || peopleData === void 0 ? void 0 : peopleData['0'], value, '0');
      }
    },
    formItemLayout: {
      rules: [_objectSpread({}, _config["default"].reg.required)],
      initialValue: deptFormData === null || deptFormData === void 0 ? void 0 : deptFormData.disposeDeptDutyMan_0
    }
  }, {
    type: 'input',
    label: '联系方式',
    paramName: "phone_0",
    itemProps: {
      disabled: true
    },
    formItemLayout: {
      initialValue: deptFormData === null || deptFormData === void 0 ? void 0 : deptFormData.phone_0
    },
    weights: type ? 1 : 0.75
  }, {
    type: 'blank',
    content: /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        marginTop: '30px'
      },
      onClick: onAddDept
    }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
      type: "primary"
    }, /*#__PURE__*/_react["default"].createElement(_PlusOutlined["default"], null), "\u65B0\u589E")),
    weights: 0.25
  }].concat(_toConsumableArray(getOtherFormItems() || []));

  // 获取表单提交时处理部门信息
  var getDeptForm = function getDeptForm() {
    var _myForm$current5;
    var vals = (myForm === null || myForm === void 0 || (_myForm$current5 = myForm.current) === null || _myForm$current5 === void 0 || (_myForm$current5 = _myForm$current5.form) === null || _myForm$current5 === void 0 ? void 0 : _myForm$current5.getFieldsValue) && myForm.current.form.getFieldsValue() || {};
    var deptFormParams = getAddModalValue({
      formItems: items,
      formValue: vals
    });
    var disposeDeptList = [];
    for (var key in deptFormParams) {
      if (key.indexOf('_') !== -1) {
        var _disposeDeptList;
        var _key$split = key.split('_'),
          _key$split2 = _slicedToArray(_key$split, 2),
          paramName = _key$split2[0],
          index = _key$split2[1];
        if (!((_disposeDeptList = disposeDeptList) !== null && _disposeDeptList !== void 0 && _disposeDeptList[index])) {
          disposeDeptList[index] = {};
        }
        disposeDeptList[index][paramName] = deptFormParams === null || deptFormParams === void 0 ? void 0 : deptFormParams[key];
        deptFormParams === null || deptFormParams === void 0 || delete deptFormParams[key];
      }
    }
    disposeDeptList = disposeDeptList.filter(function (val) {
      return val;
    }) || [];
    return disposeDeptList;
  };
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      myForm: myForm,
      getDeptForm: getDeptForm
    };
  });
  return /*#__PURE__*/_react["default"].createElement(_FormModal["default"], {
    column: 3,
    visible: true,
    ref: myForm,
    title: "",
    items: items,
    isShowModal: false,
    footerShow: false,
    formStyle: {
      width: '100%'
    }
  });
});
var _default = exports["default"] = DeptFormItem;
//# sourceMappingURL=DeptFormItem.js.map
