"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 _message = _interopRequireDefault(require("antd/lib/message"));
var _form = _interopRequireDefault(require("antd/lib/form"));
var _Filtrate = _interopRequireDefault(require("../../Filtrate"));
var _MyTable = _interopRequireDefault(require("../../MyTable"));
var _Modal = _interopRequireDefault(require("../../FormMaking/Modal"));
var _ahooks = require("ahooks");
var _renderColumn = require("@/utils/renderColumn");
var _vtxPageLayout = _interopRequireDefault(require("@vtx/components/lib/vtx-page-layout"));
var _PublicService = _interopRequireDefault(require("../../../_mapUtil/PublicService"));
var _http = _interopRequireDefault(require("../../../_util/http"));
var _config = _interopRequireDefault(require("../../../_mapUtil/config"));
var _moment = _interopRequireDefault(require("moment"));
var _propTypes = _interopRequireDefault(require("prop-types"));
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; } /**
 * @return 任务配置页面结构
 */
var TableLayout = _vtxPageLayout["default"].TableLayout;
var TabPage = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _props$deleteUrl = props.deleteUrl,
    deleteUrl = _props$deleteUrl === void 0 ? '' : _props$deleteUrl,
    _props$searchParam = props.searchParam,
    searchParam = _props$searchParam === void 0 ? {} : _props$searchParam,
    missionType = props.missionType,
    searchBefore = props.searchBefore;
  var _Form$useForm = _form["default"].useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    form = _Form$useForm2[0];
  var _useState = (0, _react.useState)({
      modalShow: false,
      modalTitle: '',
      modalType: ''
    }),
    _useState2 = _slicedToArray(_useState, 2),
    modalConfig = _useState2[0],
    setModalConfig = _useState2[1];
  var _useState3 = (0, _react.useState)({}),
    _useState4 = _slicedToArray(_useState3, 2),
    formData = _useState4[0],
    setFormData = _useState4[1];
  var _useState5 = (0, _react.useState)({
      modalLoading: false,
      modalBtnLoading: false
    }),
    _useState6 = _slicedToArray(_useState5, 2),
    loadingConfig = _useState6[0],
    setLoadingConfig = _useState6[1];
  var _useState7 = (0, _react.useState)({
      modalShow: false,
      formConfig: ''
    }),
    _useState8 = _slicedToArray(_useState7, 2),
    previewModal = _useState8[0],
    setPreviewModal = _useState8[1]; // 预览弹窗

  var editAccess = _PublicService["default"].checkAuth('EDIT'); // 修改
  var operationAccess = _PublicService["default"].checkAuth('DELETE'); // 删除
  editAccess = true;
  operationAccess = true;
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      form: form,
      onSubmit: search.submit
    };
  });
  // 列表分页查询
  var getTableData = function getTableData(_ref, formData) {
    var current = _ref.current,
      pageSize = _ref.pageSize;
    var _props$searchUrl = props.searchUrl,
      searchUrl = _props$searchUrl === void 0 ? '' : _props$searchUrl;
    var params = formData;
    if (searchBefore) {
      params = searchBefore(params);
    }
    return _http["default"].get(searchUrl, {
      body: _objectSpread(_objectSpread({
        current: current,
        size: pageSize
      }, params), searchParam)
    }).then(function (res) {
      var tableData = [],
        pageTotal = 0;
      if (String(res === null || res === void 0 ? void 0 : res.result) === '0' && res !== null && res !== void 0 && res.data) {
        var _res$data = res.data,
          _res$data$records = _res$data.records,
          records = _res$data$records === void 0 ? [] : _res$data$records,
          total = _res$data.total;
        if (records && records.length) {
          tableData = records;
        }
        pageTotal = Number(total);
      }
      return {
        list: tableData,
        total: pageTotal
      };
    });
  };
  var _useAntdTable = (0, _ahooks.useAntdTable)(getTableData, {
      defaultPageSize: 10,
      form: form
    }),
    refresh = _useAntdTable.refresh,
    run = _useAntdTable.run,
    params = _useAntdTable.params,
    tableProps = _useAntdTable.tableProps,
    search = _useAntdTable.search,
    loading = _useAntdTable.loading;
  var setLoading = function setLoading(loading) {
    loadingConfig.modalLoading = loading;
    loadingConfig.modalBtnLoading = loading;
    setLoadingConfig(_objectSpread({}, loadingConfig));
  };
  var onModalShow = function onModalShow(type, record) {
    modalConfig.modalShow = true;
    modalConfig.modalTitle = type;
    var businessTypeId = record.businessTypeId,
      businessTypeChildId = record.businessTypeChildId;
    if (businessTypeId === '3') {
      modalConfig.modalType = {
        '4': 'repair',
        '5': 'maintain'
      }[businessTypeChildId];
    } else {
      modalConfig.modalType = missionType;
    }
    setModalConfig(_objectSpread({}, modalConfig));
    if (record && record.id) {
      getDetails(record);
    }
  };
  var getDetails = function getDetails(record) {
    var _props$detailUrl = props.detailUrl,
      detailUrl = _props$detailUrl === void 0 ? '' : _props$detailUrl;
    setLoading(true);
    _http["default"].get(detailUrl, {
      body: {
        id: record.id
      }
    }).then(function (res) {
      if (String(res === null || res === void 0 ? void 0 : res.result) === '0' && res !== null && res !== void 0 && res.data) {
        var _res$data2;
        if (res !== null && res !== void 0 && (_res$data2 = res.data) !== null && _res$data2 !== void 0 && (_res$data2 = _res$data2.files) !== null && _res$data2 !== void 0 && _res$data2.length) {
          res.data.files.map(function (item) {
            item.url = item.fileUrl;
            item.name = item.fileName;
            return item;
          });
        }
        res.data = _objectSpread(_objectSpread({}, record), res.data);
        setFormData(_objectSpread({}, res.data));
      }
      setLoading(false);
    });
  };
  var submit = search.submit,
    reset = search.reset;
  function renderFiltrate() {
    var _props$items = props.items,
      items = _props$items === void 0 ? [] : _props$items;
    return /*#__PURE__*/_react["default"].createElement(_Filtrate["default"], {
      items: items,
      form: form,
      Form: _form["default"],
      onSubmit: submit,
      onReset: reset
    });
  }
  function satrtOrStop(record) {
    _http["default"].post("".concat(_config["default"].publicUrlXCGL, "/api/taskConfig/setState"), {
      body: {
        id: record.id,
        state: record.state === 1 ? 0 : 1
      }
    }).then(function (res) {
      if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
        reset();
      }
    });
  }
  function renderTable() {
    var _props$columnsConfig = props.columnsConfig,
      columnsConfig = _props$columnsConfig === void 0 ? [] : _props$columnsConfig,
      hideDeleteBtn = props.hideDeleteBtn,
      hideAddBtn = props.hideAddBtn,
      toolbarTilte = props.toolbarTilte,
      _props$showEdit = props.showEdit,
      showEdit = _props$showEdit === void 0 ? true : _props$showEdit,
      _props$showStrtOrStop = props.showStrtOrStop,
      showStrtOrStop = _props$showStrtOrStop === void 0 ? true : _props$showStrtOrStop;
    var columns = _toConsumableArray((0, _renderColumn.renderColumns)([].concat(_toConsumableArray(columnsConfig), [['操作', 'action', {
      renderButtons: function renderButtons(text, record) {
        return [{
          name: '详情',
          onClick: function onClick() {
            return onModalShow('详情', record);
          }
        }].concat(_toConsumableArray(showEdit ? [{
          name: '修改',
          onClick: function onClick() {
            return onModalShow('修改', record);
          },
          visible: editAccess
        }] : []), _toConsumableArray(showStrtOrStop ? [{
          name: record.state === 1 ? '禁用' : '启用',
          popconfirm: {
            title: "\u662F\u5426\u786E\u8BA4".concat(record.state === 1 ? '禁用' : '启用', "\uFF1F"),
            okButtonProps: {
              danger: true
            },
            confirm: function confirm() {
              satrtOrStop(record);
            }
          },
          visible: operationAccess
        }] : []), [{
          name: '复制',
          onClick: function onClick() {
            return onModalShow('复制', record);
          }
        }]);
      },
      width: 160
    }]])));
    return /*#__PURE__*/_react["default"].createElement(_MyTable["default"], {
      toolbarTilte: toolbarTilte,
      columns: columns,
      loading: loading,
      tableProps: tableProps,
      params: params,
      run: run,
      refresh: refresh,
      onModalShow: onModalShow,
      hideDeleteBtn: hideDeleteBtn,
      hideAddBtn: hideAddBtn,
      deleteUrl: deleteUrl,
      isAccess: false
    });
  }
  function renderModal() {
    var renderAddModal = props.renderAddModal,
      renderDetailModal = props.renderDetailModal;
    var modalLoading = loadingConfig.modalLoading,
      modalBtnLoading = loadingConfig.modalBtnLoading;
    var modalShow = modalConfig.modalShow,
      modalTitle = modalConfig.modalTitle,
      modalType = modalConfig.modalType;
    var onCancel = function onCancel() {
      modalConfig.modalShow = false;
      modalConfig.modalTitle = '';
      setFormData({});
      setModalConfig(_objectSpread({}, modalConfig));
    };

    // 设置表单数据
    var setPreviewData = function setPreviewData(config) {
      var isTemplate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
      var _config$list = config.list,
        list = _config$list === void 0 ? [] : _config$list;
      if (list && list.length) {
        var loopData = function loopData(data) {
          data.map(function (i, index) {
            if (!i.key) {
              i.key = "".concat(i.type, "_").concat(index);
            }
            i.isTemplate = isTemplate && i.fieldType && i.fieldType !== 'CUSTOM';
            var initialValue = i.initialValue,
              type = i.type;

            // 处理时间
            if (initialValue && ['timePicker', 'datePicker', 'monthPicker', 'yearPicker', 'rangePicker'].includes(type)) {
              if (type !== 'rangePicker') {
                i.initialValue = i.initialValue && (0, _moment["default"])(i.initialValue);
              }
            }
            if (i.columns && i.columns.length) {
              i.columns.map(function (c) {
                if (c.list && c.list.length) {
                  loopData(c.list);
                }
              });
            }
          });
        };
        loopData(list);
      }
      var data = _objectSpread(_objectSpread({}, config), {}, {
        key: "formConfig_".concat(Date.now())
      });
      previewModal.formConfig = data;
      setPreviewModal(_objectSpread({}, previewModal));
    };

    // 表单预览
    var onPreview = function onPreview(id) {
      if (id) {
        previewModal.modalShow = true;
        setPreviewModal(_objectSpread({}, previewModal));
        _http["default"].get("".concat(_config["default"].publicUrlXCGL, "/api/customForm/customForm/").concat(id)).then(function (res) {
          if (String(res === null || res === void 0 ? void 0 : res.result) === '0') {
            var _formJson;
            var formJson = res.data.formJson;
            formJson = (_formJson = formJson) !== null && _formJson !== void 0 && _formJson.length ? JSON.parse(formJson) : {};
            setPreviewData(formJson);
          } else {
            _message["default"].error((res === null || res === void 0 ? void 0 : res.msg) || '操作失败');
          }
        });
      }
    };
    var modalProps = {
      modalTitle: modalTitle,
      refresh: refresh,
      submit: submit,
      formData: formData,
      setLoading: setLoading,
      onCancel: onCancel,
      modalLoading: modalLoading,
      modalBtnLoading: modalBtnLoading,
      visible: modalShow,
      onPreview: onPreview,
      modalType: modalType,
      missionType: missionType,
      handleFormData: function handleFormData(data) {
        setFormData(data);
      }
    };
    if (modalShow) {
      if (modalTitle === '详情') {
        modalProps.missionType = modalType;
        return renderDetailModal && renderDetailModal(modalProps);
      } else {
        return renderAddModal && renderAddModal(modalProps);
      }
    }
  }
  function renderPreviewModal() {
    var modalShow = previewModal.modalShow,
      formConfig = previewModal.formConfig;
    if (modalShow) {
      return /*#__PURE__*/_react["default"].createElement(_Modal["default"], {
        formConfig: formConfig,
        webModalShow: true,
        mobileModalShow: false,
        onCancel: function onCancel() {
          previewModal.modalShow = false;
          previewModal.formConfig = '';
          setPreviewModal(_objectSpread({}, previewModal));
        }
      });
    }
  }
  return /*#__PURE__*/_react["default"].createElement(TableLayout.Page, null, renderFiltrate(), renderTable(), renderModal(), renderPreviewModal());
});
TabPage.propTypes = {
  deleteUrl: _propTypes["default"].string,
  searchParam: _propTypes["default"].object,
  missionType: _propTypes["default"].any,
  searchBefore: _propTypes["default"].any,
  columnsConfig: _propTypes["default"].array,
  hideDeleteBtn: _propTypes["default"].any,
  hideAddBtn: _propTypes["default"].any,
  toolbarTilte: _propTypes["default"].string,
  showEdit: _propTypes["default"].bool,
  showStrtOrStop: _propTypes["default"].bool,
  renderAddModal: _propTypes["default"].any,
  renderDetailModal: _propTypes["default"].any,
  detailUrl: _propTypes["default"].string,
  items: _propTypes["default"].array,
  searchUrl: _propTypes["default"].string
};
var _default = exports["default"] = TabPage;
//# sourceMappingURL=index.js.map
