"use strict";

var _interopRequireDefault = require("E:/OPU/\u524D\u7AEF\u5F00\u53D1\u9879\u76EE/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("E:/OPU/\u524D\u7AEF\u5F00\u53D1\u9879\u76EE/opu-components-vue/node_modules/@babel/runtime/helpers/esm/objectSpread2.js"));

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

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

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

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
  },
  props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _vxeTable.Pulldown.props), {}, {
    table: Object,
    valueField: {
      type: String,
      default: "id"
    },
    textField: {
      type: String,
      default: "name"
    },
    searchBefore: Function,
    searchField: {
      type: String,
      default: "keyword"
    },
    value: {
      type: [Object, Array, String, Number]
    },
    disabled: Boolean,
    inputProps: {
      type: [Object]
    },
    //是否focus时保留原值搜索
    retainSearchValue: {
      type: Boolean,
      default: true
    },
    // 允许输入值
    allowInputValue: Boolean
  }),
  data: function data() {
    return {
      multiple: false,
      tableData: [],
      selectValue: "",
      currentValue: "",
      inputChangeValue: "",
      timer: null,
      checkboxIndex: -1
    };
  },
  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.textField];
        }).join(",");
      } else if (selectValue && _init.utils.isObject(selectValue)) {
        text = selectValue[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.textField];
        }).join(",");
      } else if (currentValue && _init.utils.isObject(currentValue)) {
        text = currentValue[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)({
          modelValue: true,
          transfer: true,
          destroyOnClose: true
        }, propsData)
      });
      props.ref = "pulldownTable";
      props.class = "pulldown";
      props.on = {
        "hide-panel": this.onPulldownHide
      };
      return props;
    },
    tableProps: function tableProps() {
      var that = this;
      var table = that.table,
          tableData = that.tableData,
          $scopedSlots = that.$scopedSlots,
          onTableRowSelect = that.onTableRowSelect,
          onTableCheckboxChange = that.onTableCheckboxChange; //是否存在多选  若存在  多选checkbox索引

      that.checkboxIndex = table.props.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: table.props.columns
        }),
        on: {
          "cell-click": onTableRowSelect,
          "checkbox-change": onTableCheckboxChange,
          "checkbox-all": onTableCheckboxChange
        },
        style: (0, _objectSpread2.default)({
          background: "#fff"
        }, table.style),
        class: "pulldown-table",
        slot: "dropdown"
      };

      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;
    }
  },
  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) {
        var text = row[this.textField];
        this.selectValue = row;
        this.currentValue = row;
        this.$emit("input", text);
        this.$emit("change", text, row);
        this.$refs.pulldownTable.hidePanel();
      }
    },
    onTableCheckboxChange: function onTableCheckboxChange(_ref2) {
      var records = _ref2.records;
      this.selectValue = records;
      this.currentValue = records;
      this.$emit("change", this.selectValue);
    },
    onInputFocus: function onInputFocus(e) {
      var that = this;
      var table = that.table,
          searchBefore = that.searchBefore,
          searchField = that.searchField,
          retainSearchValue = that.retainSearchValue;
      that.$refs.pulldownTable.showPanel();
      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 = {};
          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);
        });
      } 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;
        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.$emit("inputChange", e);
      }, 300);
    },
    onClear: function onClear(value) {
      this.selectValue = "";
      this.currentValue = "";
      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.$refs.pulldownTable.hidePanel();
      this.$emit("inputPressEnter", e);
    },
    onInputKeDown: function onInputKeDown(e) {
      var key = e.key;

      if (key == "Tab") {
        this.$refs.pulldownTable.hidePanel();
      }

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

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

      if (!isVisible && key == "ArrowDown") {
        onInputFocus();
      }
    },
    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;
        });
      }
    }
  },
  render: function render(h) {
    var tableProps = this.tableProps,
        pulldownProps = this.pulldownProps;
    return h("vxe-pulldown", (0, _objectSpread2.default)((0, _objectSpread2.default)({}, pulldownProps), {
      on: (0, _objectSpread2.default)({}, pulldownProps.on)
    }), [renderInput(h, this), h("data-table", (0, _objectSpread2.default)((0, _objectSpread2.default)({}, tableProps), {
      on: (0, _objectSpread2.default)({}, tableProps.on)
    }))]);
  }
};
exports.default = _default;