"use strict";

var _interopRequireDefault = require("F:/GIT/opu-components-vue/node_modules/@babel/runtime/helpers/interopRequireDefault.js").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("core-js/modules/es.object.keys.js");

require("core-js/modules/es.number.constructor.js");

require("core-js/modules/es.array.join.js");

require("core-js/modules/es.array.map.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/web.dom-collections.for-each.js");

require("core-js/modules/es.array.find-index.js");

require("core-js/modules/es.regexp.exec.js");

require("core-js/modules/es.string.replace.js");

var _objectSpread2 = _interopRequireDefault(require("F:/GIT/opu-components-vue/node_modules/@babel/runtime/helpers/esm/objectSpread2.js"));

var _dataTable = require("../../dataTable");

var _vxeTable = require("vxe-table");

var _init = require("../../init");

var _setColumns = _interopRequireDefault(require("../../dataTable/src/setColumns"));

var _conf = _interopRequireDefault(require("../../dataTable/conf"));

function renderInput(h, _vm) {
  var onInputFocus = _vm.onInputFocus,
      onInputKeDown = _vm.onInputKeDown,
      onInputChangeBefore = _vm.onInputChangeBefore,
      text = _vm.text,
      selectValueText = _vm.selectValueText,
      disabled = _vm.disabled,
      inputProps = _vm.inputProps,
      onInputKeyUp = _vm.onInputKeyUp;
  return h("a-input", {
    props: (0, _objectSpread2.default)((0, _objectSpread2.default)({
      placeholder: selectValueText,
      disabled: disabled,
      allowClear: true
    }, inputProps), {}, {
      value: text
    }),
    ref: "input",
    on: {
      focus: onInputFocus,
      keydown: onInputKeDown,
      keyup: onInputKeyUp,
      change: onInputChangeBefore
    }
  });
}

var pulldownPropKeys = Object.keys(_vxeTable.Pulldown.props);
var _default = {
  name: "PulldownTable",
  components: {
    DataTable: _dataTable.DataTable,
    SetColumns: _setColumns.default
  },
  props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _vxeTable.Pulldown.props), {}, {
    table: Object,
    valueField: {
      type: String,
      default: "id"
    },
    textField: {
      type: String,
      default: "name"
    },
    labelField: {
      type: String,
      default: ""
    },
    searchBefore: Function,
    searchField: {
      type: String,
      default: "keyword"
    },
    value: {
      type: [Object, Array, String, Number]
    },
    disabled: Boolean,
    inputProps: {
      type: [Object]
    },
    //是否focus时保留原值搜索
    retainSearchValue: {
      type: Boolean,
      default: true
    },
    //是否focus时保留原查询关键词搜索
    retainSearchKeyword: {
      type: Boolean,
      default: false
    },
    //其他插槽
    otherSlot: {
      type: [Function],
      default: null
    },
    popupClassName: {
      type: String,
      default: ""
    },
    popupStyle: {
      type: String,
      default: ""
    },
    // 允许输入值
    allowInputValue: Boolean
  }),
  data: function data() {
    return {
      visible: false,
      multiple: false,
      tableData: [],
      selectValue: "",
      currentValue: "",
      inputChangeValue: "",
      timer: null,
      checkboxIndex: -1,
      searchKeyword: ""
    };
  },
  computed: {
    selectValueText: function selectValueText() {
      var _this = this;

      var selectValue = this.selectValue;
      var text = "";

      if (selectValue && _init.utils.isArray(selectValue)) {
        text = selectValue.map(function (p) {
          return p[_this.labelField || _this.textField];
        }).join(",");
      } else if (selectValue && _init.utils.isObject(selectValue)) {
        text = selectValue[this.labelField || this.textField];
      } else if (selectValue || selectValue == 0) {
        text = selectValue;
      }

      return text;
    },
    text: function text() {
      var _this2 = this;

      var currentValue = this.currentValue;
      var text = "";

      if (currentValue && _init.utils.isArray(currentValue)) {
        text = currentValue.map(function (p) {
          return p[_this2.labelField || _this2.textField];
        }).join(",");
      } else if (currentValue && _init.utils.isObject(currentValue)) {
        text = currentValue[this.labelField || this.textField];
      } else if (currentValue || currentValue == 0) {
        text = currentValue;
      }

      return text;
    },
    pulldownExtendProps: function pulldownExtendProps() {
      var _this3 = this;

      var rest = {};
      pulldownPropKeys.forEach(function (key) {
        rest[key] = _this3[key];
      });
      return rest;
    },
    pulldownProps: function pulldownProps() {
      var pulldownExtendProps = this.pulldownExtendProps;
      var propsData = this.$options.propsData;
      var props = Object.assign({}, pulldownExtendProps);
      delete propsData.value;
      Object.assign(props, {
        props: (0, _objectSpread2.default)({
          transfer: true,
          destroyOnClose: true
        }, propsData)
      });
      props.ref = "pulldownTable";
      props.class = "pulldown";
      props.on = {
        "hide-panel": this.onPulldownHideBefore
      };
      return props;
    },
    tableProps: function tableProps() {
      var that = this;
      var table = that.table,
          tableData = that.tableData,
          $scopedSlots = that.$scopedSlots,
          onTableRowSelect = that.onTableRowSelect,
          onTableCheckboxChange = that.onTableCheckboxChange,
          onTableCurrentChange = that.onTableCurrentChange,
          onTableSetColumnsShow = that.onTableSetColumnsShow; //是否存在多选  若存在  多选checkbox索引

      var columns = table && table.props && table.props.columns || [];
      that.checkboxIndex = columns.findIndex(function (p) {
        return p.type == "checkbox";
      });
      var props = {
        props: (0, _objectSpread2.default)((0, _objectSpread2.default)({
          highlightHoverRow: true,
          highlightCurrentRow: true,
          defaultSelectFristRow: true,
          "show-overflow": true
        }, table.props), {}, {
          data: tableData,
          columns: columns
        }),
        on: {
          "cell-click": onTableRowSelect,
          "checkbox-change": onTableCheckboxChange,
          "checkbox-all": onTableCheckboxChange,
          "current-change": onTableCurrentChange
        },
        style: (0, _objectSpread2.default)({
          background: "#fff"
        }, table.style),
        class: "pulldown-table",
        slot: "dropdown"
      };

      if (props.props.setcolumnsConfig) {
        props.props.setcolumnsConfig.onShow = onTableSetColumnsShow;
      }

      if (table.props.proxyConfig && table.props.proxyConfig.ajax) {
        props.props.proxyConfig = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, table.props.proxyConfig), {}, {
          ajax: (0, _objectSpread2.default)({}, table.props.proxyConfig.ajax)
        });
      }

      props.scopedSlots = $scopedSlots;
      props.ref = "table";
      return props;
    },
    setColumnsOpt: function setColumnsOpt() {
      var setcolumnsConfig = this.tableProps.props.setcolumnsConfig;

      if (setcolumnsConfig) {
        var modalProps = setcolumnsConfig && setcolumnsConfig.modal && setcolumnsConfig.modal.props ? setcolumnsConfig.modal.props : {};
        var proxyConfigProps = setcolumnsConfig && setcolumnsConfig.proxyConfig && setcolumnsConfig.proxyConfig.props ? setcolumnsConfig.proxyConfig.props : {};
        var proxyConfigAjax = setcolumnsConfig && setcolumnsConfig.proxyConfig && setcolumnsConfig.proxyConfig.ajax ? setcolumnsConfig.proxyConfig.ajax : {};
        var proxyConfigOn = setcolumnsConfig && setcolumnsConfig.proxyConfig && setcolumnsConfig.proxyConfig.on ? setcolumnsConfig.proxyConfig.on : {};
        var tableConfig = setcolumnsConfig && setcolumnsConfig.tableConfig ? setcolumnsConfig.tableConfig : {};
        return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _conf.default.setColumns), {}, {
          modal: {
            props: (0, _objectSpread2.default)((0, _objectSpread2.default)({
              zIndex: 9999,
              transfer: true
            }, _conf.default.setColumns.modal.props), modalProps)
          },
          proxyConfig: {
            params: setcolumnsConfig && setcolumnsConfig.proxyConfig && setcolumnsConfig.proxyConfig.params ? setcolumnsConfig.proxyConfig.params : null,
            props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _conf.default.setColumns.proxyConfig.props), proxyConfigProps),
            ajax: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _conf.default.setColumns.proxyConfig.ajax), proxyConfigAjax),
            on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _conf.default.setColumns.proxyConfig.on), proxyConfigOn)
          },
          tableConfig: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _conf.default.setColumns.tableConfig), tableConfig)
        });
      } else {
        return _conf.default.setColumns;
      }
    }
  },
  watch: {
    value: function value(val) {
      this.currentValue = val;
      this.selectValue = val;
    }
  },
  created: function created() {
    this.onChange = _init.utils.debounce(this.onInputChange, 400);
    this.currentValue = this.value;
    this.selectValue = this.value;
  },
  methods: {
    focus: function focus() {
      this.$refs.input.focus();
    },
    select: function select() {
      this.$refs.input.$refs.input.select();
    },
    onTableRowSelect: function onTableRowSelect(_ref) {
      var row = _ref.row,
          columnIndex = _ref.columnIndex;
      var checkboxIndex = this.checkboxIndex;

      if ((checkboxIndex < 0 || checkboxIndex != columnIndex) && this.selectValue != row && this.visible) {
        var text = row[this.labelField || this.textField];
        this.selectValue = row;
        this.currentValue = row;
        this.$emit("input", text);
        this.$emit("change", text, row);
        this.visible = false;
      }
    },
    onTableCurrentChange: function onTableCurrentChange(_ref2) {
      var row = _ref2.row,
          columnIndex = _ref2.columnIndex;
      var checkboxIndex = this.checkboxIndex;

      if (checkboxIndex < 0 || checkboxIndex != columnIndex) {
        var text = row[this.labelField || this.textField];
        this.$emit("current-change", text, row);
      }
    },
    onTableCheckboxChange: function onTableCheckboxChange(_ref3) {
      var records = _ref3.records;
      this.selectValue = records;
      this.currentValue = records;
      this.$emit("change", this.selectValue);
    },
    onInputFocus: function onInputFocus(e) {
      this.$emit("focus", e);
      this.onInputFocusBefore(e);
    },
    onInputFocusBefore: function onInputFocusBefore(e) {
      var that = this;
      var table = that.table,
          searchBefore = that.searchBefore,
          searchField = that.searchField,
          retainSearchValue = that.retainSearchValue;
      that.visible = true;
      that.$emit("showPanel", e);

      if (!retainSearchValue) {
        that.currentValue = "";
      }

      if (table.props.proxyConfig && table.props.proxyConfig.ajax && table.props.proxyConfig.ajax.query) {
        that.$nextTick(function () {
          var params = {};

          if (that.retainSearchKeyword) {
            params[searchField] = that.searchKeyword;
            that.currentValue = that.searchKeyword;
          } else {
            params[searchField] = retainSearchValue ? that.selectValueText : "";
          }

          if (searchBefore) {
            var searchBeforeRes = searchBefore && searchBefore(params);

            if (searchBeforeRes === false) {
              return false;
            } else if (searchBeforeRes) {
              params = searchBeforeRes;
            }
          }

          var dataTable = that.$refs.table;
          dataTable.onSearchSubmit(params);
          that.searchKeyword = params[searchField];
        });
      } else if (that.tableData.length > 0) {
        var dataTable = that.$refs.table;
        dataTable.setCurrentRow(that.tableData[0]);
      }
    },
    onInputChangeBefore: function onInputChangeBefore(e) {
      if (e.type === "click") {
        this.onClear("");
      } else {
        this.onInputChange(e);
      }
    },
    onInputChange: function onInputChange(e) {
      var _this4 = this;

      var value = e.target.value;
      clearTimeout(this.timer);
      this.timer = setTimeout(function () {
        _this4.currentValue = value;

        if (!_this4.visible) {
          _this4.visible = true;

          _this4.$emit("showPanel", e);
        }

        var pulldown = _this4.$refs.pulldownTable;
        var table = _this4.table,
            searchBefore = _this4.searchBefore,
            searchField = _this4.searchField;

        if (table.props.proxyConfig && table.props.proxyConfig.ajax && table.props.proxyConfig.ajax.query && pulldown && pulldown.isPanelVisible()) {
          var params = {};
          params[searchField] = value.replace(/(^\s*)|(\s*$)/g, "");

          if (searchBefore) {
            var searchBeforeRes = searchBefore && searchBefore(params);

            if (searchBeforeRes === false) {
              return false;
            } else if (searchBeforeRes) {
              params = searchBeforeRes;
            }
          }

          var dataTable = _this4.$refs.table;
          dataTable.onSearchSubmit(params);
          _this4.searchKeyword = params[searchField];
        }

        _this4.$emit("inputChange", e);
      }, 300);
    },
    onClear: function onClear(value) {
      if (this.visible && this.searchKeyword && this.currentValue == this.searchKeyword) {
        this.onInputChange({
          target: {
            value: ""
          }
        });
        return;
      }

      this.selectValue = "";
      this.currentValue = "";
      this.searchKeyword = "";
      this.$emit("input", value);
      this.$emit("change", value, {});
    },
    onInputEnter: function onInputEnter(e) {
      var dataTable = this.$refs.table;
      var pulldown = this.$refs.pulldownTable;
      var tableSelectedRow = dataTable.getCurrentRecord();

      if (tableSelectedRow && pulldown.isPanelVisible()) {
        this.onTableRowSelect({
          row: tableSelectedRow
        });
      }

      this.visible = false;
      this.$emit("inputPressEnter", e);
    },
    onInputKeDown: function onInputKeDown(e) {
      var key = e.key;

      if (key == "Tab") {
        this.visible = false;
      }

      this.$emit("keydown", e);
    },
    // 快捷键上下切换选中行
    onInputKeyUp: function onInputKeyUp(e) {
      var key = e.key;
      var onInputEnter = this.onInputEnter,
          onInputFocusBefore = this.onInputFocusBefore,
          visible = this.visible;

      if (key == "Enter") {
        if (visible) {
          onInputEnter(e);
        }
      }

      if (!visible && key == "ArrowDown") {
        onInputFocusBefore(e);
      }
    },
    onPulldownHideBefore: function onPulldownHideBefore(_ref4) {
      var e = _ref4.$event;
      var that = this;
      that.onPulldownHide(e); // let isContains = false;
      // let event = e || window.event;
      // let target = event.target || event.srcElement;
      // let pathIndex = 0;
      // while (target.parentNode === null) {
      //   pathIndex++;
      //   target = event.path[pathIndex];
      // }
      // //下拉
      // let selectEl = document.getElementsByClassName("ant-select-dropdown");
      // //表头
      // let setTableColumnEl = document.getElementsByClassName(
      //   "set-columns-modal"
      // );
      // //特殊处理数组
      // let specialEl = [...setTableColumnEl, ...selectEl];
      // for (let i = 0; i < specialEl.length; i++) {
      //   let el = specialEl[i];
      //   isContains = el.contains(target);
      //   if (isContains) {
      //     break;
      //   }
      // }
      // if (!isContains) {
      //   that.onPulldownHide();
      // }
    },
    onPulldownHide: function onPulldownHide() {
      var that = this;

      if (that.allowInputValue) {
        if (that.currentValue == that.selectValueText) {
          that.$emit("change", that.selectValue, {});
        } else {
          that.$emit("change", that.currentValue, {});
        }
      } else {
        that.$nextTick(function () {
          that.currentValue = that.selectValue;
        });
      }

      this.visible = false;
    },
    onSetColumnsSubmit: function onSetColumnsSubmit(e) {
      var dataTable = this.$refs.table;

      if (dataTable) {
        dataTable.setTableColumns(e);
      }
    },
    onTableSetColumnsShow: function onTableSetColumnsShow() {
      if (this.$refs.setTableColumnsModal) {
        this.$refs.setTableColumnsModal.show();
      }

      return false;
    }
  },
  render: function render(h) {
    var tableProps = this.tableProps,
        pulldownProps = this.pulldownProps,
        visible = this.visible,
        onSetColumnsSubmit = this.onSetColumnsSubmit,
        setColumnsOpt = this.setColumnsOpt;
    return h("vxe-pulldown", (0, _objectSpread2.default)((0, _objectSpread2.default)({}, pulldownProps), {}, {
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, pulldownProps.props), {}, {
        value: visible
      })
    }, {
      on: (0, _objectSpread2.default)({}, pulldownProps.on)
    }), [renderInput(h, this), h("div", {
      slot: "dropdown",
      class: this.popupClassName,
      style: this.popupStyle
    }, [this.otherSlot && this.otherSlot(), h("data-table", (0, _objectSpread2.default)((0, _objectSpread2.default)({}, tableProps), {
      on: (0, _objectSpread2.default)({}, tableProps.on)
    })), setColumnsOpt.tableConfig && h("set-columns", {
      ref: "setTableColumnsModal",
      props: {
        option: setColumnsOpt,
        columns: tableProps.props.columns
      },
      on: {
        submit: onSetColumnsSubmit
      }
    })])]);
  }
};
exports.default = _default;