"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;

var _babelHelperVueJsxMergeProps = _interopRequireDefault(require("@vue/babel-helper-vue-jsx-merge-props"));

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

var _typeof2 = _interopRequireDefault(require("E:/OPU/\u524D\u7AEF\u5F00\u53D1\u9879\u76EE/opu-components-vue/node_modules/@babel/runtime/helpers/esm/typeof.js"));

var _toConsumableArray2 = _interopRequireDefault(require("E:/OPU/\u524D\u7AEF\u5F00\u53D1\u9879\u76EE/opu-components-vue/node_modules/@babel/runtime/helpers/esm/toConsumableArray.js"));

var _defineProperty2 = _interopRequireDefault(require("E:/OPU/\u524D\u7AEF\u5F00\u53D1\u9879\u76EE/opu-components-vue/node_modules/@babel/runtime/helpers/esm/defineProperty.js"));

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

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

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

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

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

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

require("core-js/modules/es.function.name.js");

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

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

require("core-js/modules/web.dom-collections.iterator.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.to-string.js");

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

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

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

var _moment = _interopRequireDefault(require("moment"));

var _lodash = require("lodash");

var _utils = _interopRequireDefault(require("../../../utils"));

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

function handlefieldOptionsDataField(item, json) {
  var optionData = json;

  if (item) {
    var df = item.dataField != undefined ? item.dataField : _conf.default.getSelectOptions.dataField;
    optionData = _utils.default.getObjData(df, json);
  }

  return optionData;
} //编辑插槽 是否禁用


function editSlotPropInit(row, props, key, field) {
  var defaultKey = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
  return !props[key] && props[key] != 0 ? defaultKey : typeof props[key] == "function" ? props[key](row, field) : props[key];
}

var editRender = {
  components: {},
  props: {
    onEditOptionsLoadBefore: {
      type: Function,
      default: null
    },
    editSize: {
      type: String,
      default: ""
    },
    onOptionsLoadAfter: {
      type: Function,
      default: null
    },
    onOptionsAllLoad: {
      type: Function,
      default: null
    }
  },
  data: function data() {
    return {
      editType: ["AInput", "AInputNumber", "ASelect", "AAutoComplete", "ADatePicker", "ATimePicker", "ASwitch", "ACheckbox"],
      editOptions: {},
      editApiList: [],
      editDefaultOption: {},
      editFieldList: {}
    };
  },
  computed: {
    editItemSize: function editItemSize() {
      var editSize = this.editSize;

      if (editSize === "") {
        editSize = _conf.default.editSize;
      }

      return editSize;
    }
  },
  methods: {
    // 下划线转换驼峰
    toHump: function toHump(name) {
      // eslint-disable-next-line no-useless-escape
      name = name.replace(/\-(\w)/g, function (all, letter) {
        return letter.toUpperCase();
      });
      return name.charAt(0).toUpperCase() + name.slice(1);
    },
    // 驼峰转换下划线
    toLine: function toLine(name) {
      return name.replace(/([A-Z])/g, "-$1").toLowerCase();
    },
    setFieldsOptions: function setFieldsOptions(data) {
      var _this = this;

      var editOptions = this.editOptions,
          editFieldList = this.editFieldList;

      var _loop = function _loop(key) {
        var item = data[key];

        if (editFieldList[key]) {
          // editOptions[key] = item;
          editOptions[key] = item.map(function (p) {
            return _this.optionDataRender(p, key);
          });
        }
      };

      for (var key in data) {
        _loop(key);
      }

      this.editOptions = editOptions;
      this.$refs.dataGrid.updateData();
    },
    getFieldsOptions: function getFieldsOptions() {
      return this.editOptions;
    },
    getEditDefaultOption: function getEditDefaultOption() {
      return this.editDefaultOption;
    },
    editColumnsRender: function editColumnsRender(data, filterCallback) {
      var _this2 = this;

      var h = this.$createElement;
      var editType = this.editType,
          editOptions = this.editOptions,
          toHump = this.toHump,
          editDefaultOption = this.editDefaultOption;
      var apiList = [];
      var editFieldList = {};
      var getSelectOptions = _conf.default.getSelectOptions;
      data = data.filter(function (p) {
        if (filterCallback && !filterCallback(p)) {
          return false;
        }

        if (p.itemRender) {
          var name = toHump(p.itemRender.name || editType[0]);

          if (editType.indexOf(toHump(p.itemRender.name || editType[0])) > -1) {
            if (!p.slots) p.slots = {};

            p.slots.default = function (e) {
              return [h("div", {
                "style": "display:flex;align-items: center;"
              }, [p.itemRender.before && p.itemRender.before(e), h("div", {
                "style": "flex:1"
              }, [_this2.editSlotRender(name)(e)]), p.itemRender.after && p.itemRender.after(e)])];
            };

            if (name == "ASwitch" || name == "ACheckbox") p.align = "center";
            var props = p.itemRender.props || {};

            if (["ASelect", "AAutoComplete"].includes(name) && p.itemRender.props) {
              var field = p.field;
              var valueField = props.valueField || getSelectOptions.valueField;
              var labelField = props.labelField || getSelectOptions.labelField;
              var childrenField = props.childrenField || getSelectOptions.childrenField;
              var dataField = props.dataField || getSelectOptions.dataField;
              var defaultField = props.defaultField || getSelectOptions.defaultField;
              var searchFields = props.searchFields || _conf.default.defaultProps.select.searchFields || [];
              editFieldList[field] = {
                valueField: valueField,
                labelField: labelField,
                childrenField: childrenField,
                defaultField: defaultField,
                searchFields: searchFields
              };

              if (p.itemRender.props.options) {
                editOptions[field] = (0, _lodash.cloneDeep)(p.itemRender.props.options).map(function (o) {
                  o = _this2.optionDataRender(o, field, "", (0, _defineProperty2.default)({}, field, {
                    valueField: valueField,
                    labelField: labelField,
                    childrenField: childrenField
                  }));

                  if (o[defaultField]) {
                    editDefaultOption[field] = p.value;
                  }

                  return o;
                });
              } else if (!p.itemRender.props.optionsField && !editOptions[field] && (props.api || props.dataField || props.param)) {
                apiList.push({
                  field: field,
                  api: props.api || getSelectOptions.api,
                  valueField: valueField,
                  labelField: labelField,
                  childrenField: childrenField,
                  dataField: dataField,
                  defaultField: defaultField,
                  param: props.param || {}
                });
              }
            }
          }
        }

        return true;
      });
      this.editDefaultOption = editDefaultOption;
      this.editOptions = editOptions;
      this.editApiList = apiList;
      this.editFieldList = editFieldList;
      this.fetchItemPropsOptionsApiList();
      return data;
    },
    optionDataRender: function optionDataRender(o, field) {
      var _this3 = this;

      var pValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "";
      var editFieldList = arguments.length > 3 ? arguments[3] : undefined;
      if (!editFieldList) editFieldList = this.editFieldList;

      if (editFieldList[field]) {
        var _editFieldList$field = editFieldList[field],
            labelField = _editFieldList$field.labelField,
            valueField = _editFieldList$field.valueField,
            childrenField = _editFieldList$field.childrenField;

        if (o.value && !o[_conf.default.originalValueKey]) {
          o[_conf.default.originalValueKey || "originalValueKey"] = o.value;
        }

        if (labelField != "label") {
          o.label = o[labelField];
        }

        if (valueField != "value") {
          o.value = o[valueField];
        }

        if (pValue) {
          o._pValue = pValue;
        }

        if (childrenField != "children" && o[childrenField].length > 0) {
          o.children = o[childrenField].map(function (p) {
            return _this3.optionDataRender(p, field, o.value);
          });
        }
      }

      return o;
    },
    fetchItemPropsOptionsApiList: function fetchItemPropsOptionsApiList() {
      var _this4 = this;

      var that = this;
      var onEditOptionsLoadBefore = that.onEditOptionsLoadBefore,
          list = that.editApiList;

      if (onEditOptionsLoadBefore) {
        var beforeRes = onEditOptionsLoadBefore(list);

        if (beforeRes === false) {
          return false;
        } else if (beforeRes) {
          list = beforeRes;
        }
      }

      var promises = list.map(function (p) {
        return p.api(p.param);
      });
      Promise.all(promises).then(function (res) {
        var editOptions = that.editOptions,
            editDefaultOption = that.editDefaultOption,
            onOptionsLoadAfter = that.onOptionsLoadAfter;
        list.forEach(function (item, index) {
          var defaultField = item.defaultField; // 字段单独配置api的可选数据的处理

          var optionsData = handlefieldOptionsDataField(item, res[index], that);
          editOptions[item.field] = optionsData.map(function (p) {
            p = _this4.optionDataRender(p, item.field);

            if (p[defaultField]) {
              editDefaultOption[item.field] = p.value;
            }

            return p;
          });
        });

        if (that.onOptionsAllLoad) {
          editOptions = that.onOptionsAllLoad(editOptions);
        }

        that.editOptions = editOptions;
        that.editDefaultOption = editDefaultOption;

        if (onOptionsLoadAfter) {
          onOptionsLoadAfter(editOptions, editDefaultOption);
        }

        that.$refs.dataGrid.updateData();
      }).catch(function () {});
    },
    selectFilterRender: function selectFilterRender(props, field) {
      if (this.editFieldList[field] && props.showSearch && !props.filterOption) {
        var _this$editFieldList$f = this.editFieldList[field],
            labelField = _this$editFieldList$f.labelField,
            valueField = _this$editFieldList$f.valueField,
            searchFields = _this$editFieldList$f.searchFields;
        var optionsData = props.options || [];

        props.filterOption = function (input, option) {
          var value = option.componentOptions.propsData.value;
          var objIndex = optionsData.findIndex(function (p) {
            return p[valueField].toString() === value;
          });
          var obj = optionsData[objIndex];
          var is = false;
          var searchFieldList = [labelField].concat((0, _toConsumableArray2.default)(searchFields)); // vF,

          for (var i = 0; i < searchFieldList.length; i++) {
            var key = searchFieldList[i];

            if (obj[key]) {
              if (obj[key].toString().toLowerCase().indexOf(input.toLowerCase()) >= 0) {
                is = true;
                break;
              }
            }
          }

          return is;
        };
      }

      return props;
    },
    autoCompleteFilterRender: function autoCompleteFilterRender(props, row, field) {
      var h = this.$createElement;
      var value = props.value;
      var dataSource = props.dataSource;
      delete props.dataSource;
      var _this$editFieldList$f2 = this.editFieldList[field],
          labelField = _this$editFieldList$f2.labelField,
          valueField = _this$editFieldList$f2.valueField;

      if (props.search) {
        dataSource = props.search(value, dataSource, row, field);
      } else {
        var searchFields = props.searchFields || [];
        dataSource = dataSource.filter(function (p) {
          if (!value) return true;
          var is = false;

          if (Object.prototype.toString.call(p) === "[object Object]") {
            var searchFieldList = [labelField].concat((0, _toConsumableArray2.default)(searchFields)); // vF,

            for (var i = 0; i < searchFieldList.length; i++) {
              var key = searchFieldList[i];
              var optionValue = p[key] || p;

              if (value) {
                if (optionValue.toString().toLowerCase().indexOf(value.toLowerCase()) >= 0) {
                  is = true;
                  break;
                }
              }
            }
          } else if (p.toString().toLowerCase().indexOf(value.toLowerCase()) >= 0) {
            is = true;
          }

          return is;
        });
      }

      var selectOptions = dataSource.map(function (p) {
        return h("a-select-option", {
          "key": p[valueField] || p
        }, [p[labelField] || p]);
      });
      return {
        props: props,
        selectOptions: selectOptions,
        dataSource: dataSource,
        valueField: valueField,
        labelField: labelField
      };
    },
    findOptionRow: function findOptionRow(options, value, valueField) {
      return options.find(function (p) {
        return p[valueField] == value;
      }) || {};
    },
    //插槽class  style 等重写
    editSlotItemRender: function editSlotItemRender(name, e) {
      if (typeof name == "function") {
        return name(e);
      } else {
        return name;
      }
    },
    editSlotRender: function editSlotRender(name) {
      var _this5 = this;

      var h = this.$createElement;
      return function (event) {
        var columnIndex = event.columnIndex,
            row = event.row;
        var item = _this5.tableColumns[columnIndex];
        var itemRender = item.itemRender || {};
        var props = itemRender.props || {};
        var disabled = editSlotPropInit(row, props, "disabled", item.field);
        var element;

        if ((0, _typeof2.default)(disabled) == "object") {
          return [disabled];
        } else {
          var field = item.field;
          event.field = field;
          props = (0, _objectSpread2.default)((0, _objectSpread2.default)({
            size: _this5.editItemSize
          }, props), {}, {
            value: _utils.default.getObjData(field, row),
            disabled: disabled
          });
          var attr = {
            class: _this5.editSlotItemRender(itemRender.class, event),
            style: _this5.editSlotItemRender(itemRender.style, event)
          };
          var optionsField, options, trueValue, falseValue;
          var ons = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, itemRender.on), {}, {
            change: function change(e) {
              _utils.default.setObjData(field, row, e); // row[field] = e;


              row.ISEDIT = true;

              if (itemRender.on && itemRender.on.change) {
                itemRender.on.change(e, event);
              }
            },
            blur: function blur(e) {
              if (itemRender.on && itemRender.on.blur) {
                itemRender.on.blur(e, event);
              }
            }
          });

          switch (name) {
            case "AInput":
              element = h("a-input", (0, _babelHelperVueJsxMergeProps.default)([{}, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attr), {}, {
                props: props,
                on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
                  change: function change(e) {
                    var value = e.target.value;

                    _utils.default.setObjData(field, row, value); // row[field] = value;


                    row.ISEDIT = true;

                    if (itemRender.on && itemRender.on.change) {
                      itemRender.on.change(e, event);
                    }
                  },
                  keydown: function keydown(e) {
                    if (itemRender.on && itemRender.on.keydown) {
                      itemRender.on.keydown(e, event);
                    }
                  }
                })
              })]));
              break;

            case "AInputNumber":
              element = h("a-input-number", (0, _babelHelperVueJsxMergeProps.default)([{}, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attr), {}, {
                props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
                  max: editSlotPropInit(row, props, "max", field, Infinity),
                  min: editSlotPropInit(row, props, "min", field, -Infinity)
                }),
                on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
                  change: function change(e) {
                    var value = e;

                    _utils.default.setObjData(field, row, value); // row[field] = value;


                    row.ISEDIT = true;

                    if (itemRender.on && itemRender.on.change) {
                      itemRender.on.change(value, event);
                    }
                  }
                })
              })]));
              break;

            case "ASelect":
              optionsField = props && props.optionsField || "";
              options = optionsField ? row[optionsField] : _this5.editOptions[field];

              if (props.optionsFilter) {
                options = props.optionsFilter((0, _lodash.cloneDeep)(options), props.value);
              }

              props = _this5.selectFilterRender((0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
                options: options
              }), field);
              element = h("a-select", (0, _babelHelperVueJsxMergeProps.default)([{}, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attr), {}, {
                props: props,
                style: (0, _objectSpread2.default)({
                  width: "100%"
                }, attr.style),
                on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
                  change: function change(value) {
                    _utils.default.setObjData(field, row, value); // row[field] = value;


                    row.ISEDIT = true;

                    if (itemRender.on && itemRender.on.change) {
                      var valueField = _this5.editFieldList[field].valueField;

                      var _row = options.find(function (p) {
                        return p[valueField] == value;
                      });

                      var pRow;

                      if (_row && _row._pValue) {
                        pRow = options.find(function (p) {
                          return p[valueField] == _row._pValue;
                        });
                      }

                      itemRender.on.change(value, _row, event, pRow);
                    }
                  }
                })
              })]));
              break;

            case "AAutoComplete":
              optionsField = props && props.optionsField || "";
              options = optionsField ? row[optionsField] : _this5.editOptions[field];

              if (props.optionsFilter) {
                options = props.optionsFilter((0, _lodash.cloneDeep)(options), props.value);
              }

              var filterData = _this5.autoCompleteFilterRender((0, _objectSpread2.default)((0, _objectSpread2.default)({
                size: _this5.editItemSize
              }, props), {}, {
                dataSource: options || [],
                disabled: disabled
              }), row, field);

              var selectOptions = filterData.selectOptions;
              var dataSource = filterData.dataSource;
              props = filterData.props;
              element = h("a-auto-complete", (0, _babelHelperVueJsxMergeProps.default)([{}, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attr), {}, {
                props: props,
                style: (0, _objectSpread2.default)({
                  width: "100%"
                }, itemRender.style),
                on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
                  select: function select(value) {
                    var optionRow = _this5.findOptionRow(dataSource, value, filterData.valueField);

                    _utils.default.setObjData(field, row, optionRow[filterData.labelField] || value); // row[field] = optionRow[filterData.labelField] || value;


                    row.ISEDIT = true;

                    if (itemRender.on && itemRender.on.select) {
                      itemRender.on.select(value, optionRow, event);
                    }
                  }
                })
              })]), [h("template", (0, _babelHelperVueJsxMergeProps.default)([{}, {
                slot: "dataSource"
              }]), [selectOptions])]);
              break;

            case "ADatePicker":
              var dateValue = !props.value ? null : props.value.format ? props.value : (0, _moment.default)(props.value);
              element = h("a-date-picker", (0, _babelHelperVueJsxMergeProps.default)([{}, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attr), {}, {
                props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
                  value: dateValue
                }),
                on: (0, _objectSpread2.default)({}, ons)
              })]));
              break;

            case "ATimePicker":
              element = h("a-time-picker", (0, _babelHelperVueJsxMergeProps.default)([{}, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attr), {}, {
                props: (0, _objectSpread2.default)((0, _objectSpread2.default)({
                  clearIcon: true
                }, props), {}, {
                  disabled: disabled
                }),
                on: (0, _objectSpread2.default)({}, ons)
              })]));
              break;

            case "ASwitch":
              trueValue = props.trueValue ? props.trueValue : true;
              falseValue = props.falseValue ? props.falseValue : false;
              if (editSlotPropInit(row, props, "hidden", field)) return "";
              element = h("a-switch", (0, _babelHelperVueJsxMergeProps.default)([{}, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attr), {}, {
                props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
                  checked: row[field] == trueValue
                }),
                on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
                  change: function change(e) {
                    var value = e ? trueValue : falseValue;

                    _utils.default.setObjData(field, row, value); // row[field] = value;


                    row.ISEDIT = true;

                    if (itemRender.on && itemRender.on.change) {
                      itemRender.on.change(e, event);
                    }
                  }
                })
              })]));
              break;

            case "ACheckbox":
              trueValue = props.trueValue ? props.trueValue : true;
              falseValue = props.falseValue ? props.falseValue : false;
              if (editSlotPropInit(row, props, "hidden", field)) return "";
              element = h("a-checkbox", (0, _babelHelperVueJsxMergeProps.default)([{}, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attr), {}, {
                props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
                  checked: row[field] == trueValue
                }),
                on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
                  change: function change(e) {
                    var value = e.target.checked ? trueValue : falseValue;

                    _utils.default.setObjData(field, row, value); // row[field] = value;


                    row.ISEDIT = true;

                    if (itemRender.on && itemRender.on.change) {
                      itemRender.on.change(e, event);
                    }
                  }
                })
              })]));
              break;
          }
        }

        return [element];
      };
    }
  }
};
var _default = editRender;
exports.default = _default;