"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.number.constructor.js");

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

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

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

var _interface = require("ant-design-vue/lib/date-picker/interface");

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

var _datePicker = _interopRequireDefault(require("./datePicker"));

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

var _default2 = {
  name: "ARangePickerSplit",
  components: {
    OpuDatePicker: _datePicker.default
  },
  props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (0, _interface.DatePickerProps)()), {}, {
    value: [Number, String, Object, Array],
    min: [String, Object],
    max: [String, Object],
    // 是否限制开始于结束日期选择
    hasLimit: {
      type: Boolean,
      required: false,
      default: true
    },
    separator: [String, Function],
    startDisabled: {
      type: Boolean,
      default: false
    },
    endDisabled: {
      type: Boolean,
      default: false
    },
    startDisabledDate: {
      type: Function,
      default: null
    },
    endDisabledDate: {
      type: Function,
      default: null
    },
    startProps: {
      type: Object,
      default: function _default() {
        return {};
      }
    },
    endProps: {
      type: Object,
      default: function _default() {
        return {};
      }
    }
  }),
  model: {
    prop: "value",
    event: "update"
  },
  data: function data() {
    return {
      minDate: "",
      maxDate: ""
    };
  },
  computed: {
    startValue: function startValue() {
      var start = "";

      if (this.value && this.value.length) {
        start = this.value[0];
      }

      return start;
    },
    endValue: function endValue() {
      var end = "";

      if (this.value && this.value.length > 1) {
        end = this.value[1];
      }

      return end;
    }
  },
  watch: {
    value: function value(val) {
      if (val && val.length) {
        this.minDate = val[0];
      }

      if (val && val.length > 1) {
        this.maxDate = val[1];
      }
    }
  },
  created: function created() {
    if (this.value && this.value.length) {
      this.minDate = this.value[0];
    }

    if (this.value && this.value.length > 1) {
      this.maxDate = this.value[1];
    }
  },
  methods: {
    onChange: function onChange(type) {
      var value = [this.minDate, this.maxDate];
      this.$emit("update", value);
      this.$emit("change", value);
      this.$emit("".concat(type, "Change"), value);
    },
    onStartChange: function onStartChange(e) {
      if (this.maxDate && (0, _moment.default)(this.maxDate).isBefore(e)) {
        this.maxDate = e;
      }

      this.minDate = e;
      this.onChange("start");
    },
    onEndChange: function onEndChange(e) {
      if (this.minDate && e && !(0, _moment.default)(this.minDate).isBefore(e)) {
        this.minDate = e;
      }

      this.maxDate = e;
      this.onChange("end");
    },
    startDisabledDateFuc: function startDisabledDateFuc(current) {
      if (this.startDisabledDate && this.startDisabledDate(current, this.minDate, this.maxDate)) {
        return true;
      } else if (this.maxDate && current) {
        return current > this.maxDate || this.min && current < this.min;
      } else if (this.min || this.max) {
        return current < this.min || current > this.max.endOf("day");
      }
    },
    endDisabledDateFuc: function endDisabledDateFuc(current) {
      if (this.endDisabledDate && this.endDisabledDate(current, this.minDate, this.maxDate)) {
        return true;
      } else if (this.minDate && current) {
        return current < this.minDate || this.max && current > this.max;
      } else if (this.min || this.max) {
        return current < this.min || current > this.max.endOf("day");
      }
    },
    focus: function focus() {
      this.$refs.startDatePicker.focus();
    }
  },
  render: function render(h) {
    var _this = this;

    var hasLimit = this.hasLimit,
        separator = this.separator,
        $listeners = this.$listeners;

    var propsData = _utils.default.clone(this.$options.propsData);

    delete propsData.value;
    var ons = {};

    _utils.default.each($listeners, function (cb, type) {
      ons[type] = function () {
        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        _this.$emit.apply(_this, [type].concat(args));
      };
    });

    var start = {
      ref: "startDatePicker",
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, propsData), this.startProps),
      style: {
        width: "100%"
      },
      on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
        change: function change(e) {
          _this.onStartChange(e);
        },
        inputPressEnter: function inputPressEnter() {
          setTimeout(function () {
            _this.$refs.endDatePicker.focus();
          }, 100);
        }
      })
    };

    if (this.startValue) {
      start.props.value = this.startValue;
    }

    if (this.startDisabled) {
      start.props.disabled = true;
    }

    var end = {
      ref: "endDatePicker",
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, propsData), this.endProps),
      style: {
        width: "100%"
      },
      on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
        change: function change(e) {
          _this.onEndChange(e);
        },
        inputPressEnter: function inputPressEnter(e) {
          _this.$emit("inputPressEnter", e);
        }
      })
    };

    if (this.endValue) {
      end.props.value = this.endValue;
    }

    if (this.endDisabled) {
      end.props.disabled = true;
    } // 限制


    if (hasLimit) {
      start.props.disabledDate = function (e) {
        return _this.startDisabledDateFuc(e);
      };

      end.props.disabledDate = function (e) {
        return _this.endDisabledDateFuc(e);
      };
    } else {
      if (start.props.startDisabledDate) {
        start.props.disabledDate = this.startDisabledDate;
      }

      if (end.props.endDisabledDate) {
        end.props.disabledDate = this.endDisabledDate;
      }

      start.scopedSlots = {
        dateRender: function dateRender(current) {
          var outside = false;

          if (_this.maxDate && current) {
            outside = current > _this.maxDate || _this.min && current < _this.min;
          } else if (_this.min || _this.max) {
            outside = current < _this.min || current > _this.max;
          }

          var dateClass = outside ? "outside" : "";
          return h("div", {
            class: ["ant-calendar-date", dateClass]
          }, [current.date()]);
        }
      };
      end.scopedSlots = {
        dateRender: function dateRender(current) {
          var outside = false;

          if (_this.minDate && current) {
            outside = current < _this.minDate || _this.max && current > _this.max;
          } else if (_this.min || _this.max) {
            outside = current < _this.min || current > _this.max;
          }

          var dateClass = outside ? "outside" : "";
          return h("div", {
            class: ["ant-calendar-date", dateClass]
          }, [current.date()]);
        }
      };
    }

    var separatorText = "-";

    if (separator && _utils.default.isFunction(separator)) {
      separatorText = separator();
    } else if (separator) {
      separatorText = separator;
    }

    return h("div", {
      "class": "date-range-split"
    }, [h("div", {
      "class": "item"
    }, [h("opu-date-picker", (0, _babelHelperVueJsxMergeProps.default)([{}, start]))]), h("div", {
      "class": "fh"
    }, [" ", separatorText, " "]), h("div", {
      "class": "item"
    }, [h("opu-date-picker", (0, _babelHelperVueJsxMergeProps.default)([{}, end]))])]);
  }
};
exports.default = _default2;