"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _checkbox = _interopRequireDefault(require("@alifd/next/lib/checkbox"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var React = _interopRequireWildcard(require("react"));
var _api = require("../api");
var _sortablejs = _interopRequireDefault(require("sortablejs"));
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; }
var CheckboxDialog = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(CheckboxDialog, _React$Component);
  function CheckboxDialog(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    _this.save = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var _this$props, tableId, pageInfo;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _this$props = _this.props, tableId = _this$props.tableId, pageInfo = _this$props.pageInfo;
            _context.prev = 1;
            _context.next = 4;
            return (0, _api.saveUserTableHeader)({
              tableId: tableId,
              pageCode: pageInfo.code,
              jsonData: {
                userColumns: _this.state.dataList
              }
            });
          case 4:
            _this.props.onSave && _this.props.onSave(_this.state.dataList);
            _context.next = 10;
            break;
          case 7:
            _context.prev = 7;
            _context.t0 = _context["catch"](1);
            console.log(_context.t0);
          case 10:
          case "end":
            return _context.stop();
        }
      }, _callee, null, [[1, 7]]);
    }));
    _this.change = function (checked, item) {
      item.checked = checked;
      _this.setState({
        dataList: _this.state.dataList
      });
    };
    _this.sortableGroupDecorator = function (componentBackingInstance) {
      // console.log(componentBackingInstance)
      if (componentBackingInstance) {
        var options = {
          draggable: '.li',
          // animation: "150",
          onEnd: function onEnd(event) {
            var oldIndex = event.oldIndex,
              newIndex = event.newIndex;
            // console.log(event)
            var list = _this.state.dataList;
            var source = list[oldIndex];
            list.splice(oldIndex, 1);
            list.splice(newIndex, 0, source);
            _this.setState({
              dataList: list
            });
            // console.log(this.state.dataList)
            //   let children = evt.to.children;
          }
        };

        _sortablejs["default"].create(componentBackingInstance, options);
      }
    };
    _this.state = {
      dataList: JSON.parse(JSON.stringify(_this.props.dataSource))
    };
    return _this;
  }
  var _proto = CheckboxDialog.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var onClose = this.props.onClose;
    var dataList = this.state.dataList;
    return /*#__PURE__*/React.createElement("div", {
      className: "common-table-balloon"
    }, /*#__PURE__*/React.createElement("ul", {
      ref: this.sortableGroupDecorator
    }, dataList.map(function (item) {
      return /*#__PURE__*/React.createElement("li", {
        className: "li",
        key: item.fieldName
      }, /*#__PURE__*/React.createElement(_icon["default"], {
        size: "xxs",
        type: "list",
        className: "list-icon"
      }), /*#__PURE__*/React.createElement(_checkbox["default"], {
        onChange: function onChange(checked) {
          _this2.change(checked, item);
        },
        checked: item.checked
        //   defaultChecked={item.checked}
        ,
        label: item.displayName,
        value: item.fieldName
      }));
    })), /*#__PURE__*/React.createElement("div", {
      className: "balloon-btn"
    }, /*#__PURE__*/React.createElement(_button["default"], {
      size: "small",
      className: "btn",
      onClick: function onClick() {
        return onClose && onClose();
      }
    }, "\u53D6\u6D88"), /*#__PURE__*/React.createElement(_button["default"], {
      size: "small",
      className: "btn",
      type: "primary",
      onClick: function onClick() {
        return _this2.save();
      }
    }, "\u4FDD\u5B58")));
  };
  return CheckboxDialog;
}(React.Component);
var _default = CheckboxDialog;
exports["default"] = _default;