"use strict";

var _interopRequireDefault = require("D:/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.includes.js");

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

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

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

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

var _timePicker = require("ant-design-vue/lib/time-picker");

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

var _timePicker2 = _interopRequireDefault(require("./timePicker"));

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

var _lodash = require("lodash");

var _default = {
  name: "ARangeTimePickerSplit",
  components: {
    OpuTimePicker: _timePicker2.default
  },
  props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (0, _timePicker.TimePickerProps)()), {}, {
    value: [Number, String, Object, Array],
    // 是否限制开始于结束日期选择
    hasLimit: {
      type: Boolean,
      required: false,
      default: true
    },
    separator: [String, Function],
    startDisabled: {
      type: Boolean,
      default: false
    },
    startDisabledHours: {
      type: Boolean,
      default: false
    },
    startDisabledMinutes: {
      type: Boolean,
      default: false
    },
    startDisabledSeconds: {
      type: Boolean,
      default: false
    },
    endDisabled: {
      type: Boolean,
      default: false
    },
    endDisabledHours: {
      type: Boolean,
      default: false
    },
    endDisabledMinutes: {
      type: Boolean,
      default: false
    },
    endDisabledSeconds: {
      type: Boolean,
      default: false
    } // isEqual: {
    //   type: Boolean,
    //   default: true
    // }

  }),
  model: {
    prop: "value",
    event: "update"
  },
  data: function data() {
    var hours = [];
    var minutes = [];
    var seconds = [];

    for (var i = 0; i < 60; i++) {
      if (i < 24) {
        hours.push(i);
      }

      minutes.push(i);
      seconds.push(i);
    }

    return {
      hours: hours,
      minutes: minutes,
      seconds: seconds,
      minTime: "",
      maxTime: ""
    };
  },
  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.minTime = val[0];
      }

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

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

      this.onChange("start");
    },
    onEndChange: function onEndChange(e) {
      if (this.minTime && e && !(0, _moment.default)(this.minTime, this.format).isBefore(e)) {
        if (this.hasLimit) {
          this.maxTime = (0, _lodash.cloneDeep)(this.minTime);
        } else {
          this.minTime = (0, _lodash.cloneDeep)(e);
          this.maxTime = e;
        }
      } else {
        this.maxTime = e;
      }

      this.onChange("end");
    },
    startDisabledHoursFuc: function startDisabledHoursFuc() {
      var hoursArr = [];

      if (this.disabledHours) {
        hoursArr = this.disabledHours();
      }

      if (this.startDisabledHours) {
        hoursArr = this.startDisabledHours();
      }

      if (this.hasLimit && this.endValue) {
        var _this$getEndValueSpli = this.getEndValueSplit(),
            endHours = _this$getEndValueSpli.endHours;

        var limitHoursArr = (0, _lodash.cloneDeep)(this.hours);

        for (var i = 0; i < limitHoursArr.length; i++) {
          var item = limitHoursArr[i];

          if (hoursArr.includes(item) || item < endHours || item == endHours) {
            continue;
          }

          hoursArr.push(item);
        }
      }

      return hoursArr;
    },
    startDisabledMinutesFuc: function startDisabledMinutesFuc(selectedHour) {
      var minutesArr = [];

      if (this.disabledMinutes) {
        minutesArr = this.disabledMinutes(selectedHour);
      }

      if (this.startDisabledMinutes) {
        minutesArr = this.startDisabledMinutes(selectedHour);
      }

      if (this.hasLimit && this.endValue) {
        var _this$getEndValueSpli2 = this.getEndValueSplit(),
            endHours = _this$getEndValueSpli2.endHours,
            endMinutes = _this$getEndValueSpli2.endMinutes;

        var limitMinutesArr = (0, _lodash.cloneDeep)(this.minutes);

        if (selectedHour >= endHours) {
          for (var i = 0; i < limitMinutesArr.length; i++) {
            var item = limitMinutesArr[i];
            if (minutesArr.includes(item)) continue;

            if (selectedHour == endHours) {
              if (item < endMinutes) continue;

              if (item == endMinutes) {
                continue;
              }
            }

            minutesArr.push(item);
          }
        }
      }

      return minutesArr;
    },
    startDisabledSecondsFuc: function startDisabledSecondsFuc(selectedHour, selectedMinute) {
      var secondsArr = [];

      if (this.disabledSeconds) {
        secondsArr = this.disabledSeconds(selectedHour, selectedMinute);
      }

      if (this.startDisabledSeconds) {
        secondsArr = this.startDisabledSeconds(selectedHour, selectedMinute);
      }

      if (this.hasLimit && this.endValue) {
        var _this$getEndValueSpli3 = this.getEndValueSplit(),
            endHours = _this$getEndValueSpli3.endHours,
            endMinutes = _this$getEndValueSpli3.endMinutes,
            endSeconds = _this$getEndValueSpli3.endSeconds;

        var limitSecondsArr = (0, _lodash.cloneDeep)(this.seconds);

        if (selectedHour > endHours || selectedHour == endHours && selectedMinute >= endMinutes) {
          for (var i = 0; i < limitSecondsArr.length; i++) {
            var item = limitSecondsArr[i];
            if (secondsArr.includes(item)) continue;

            if (selectedHour == endHours && selectedMinute == endMinutes) {
              if (item < endSeconds) continue;
              if (item == endSeconds) continue;
            }

            secondsArr.push(item);
          }
        }
      }

      return secondsArr;
    },
    endDisabledHoursFuc: function endDisabledHoursFuc() {
      var hoursArr = [];

      if (this.disabledHours) {
        hoursArr = this.disabledHours();
      }

      if (this.endDisabledHours) {
        hoursArr = this.endDisabledHours();
      }

      if (this.hasLimit && this.startValue) {
        var _this$getStartValueSp = this.getStartValueSplit(),
            startHours = _this$getStartValueSp.startHours;

        var limitHoursArr = (0, _lodash.cloneDeep)(this.hours);

        for (var i = 0; i < limitHoursArr.length; i++) {
          var item = limitHoursArr[i];

          if (hoursArr.includes(item) || item >= startHours) {
            continue;
          }

          hoursArr.push(item);
        }
      }

      return hoursArr;
    },
    endDisabledMinutesFuc: function endDisabledMinutesFuc(selectedHour) {
      var minutesArr = [];

      if (this.disabledMinutes) {
        minutesArr = this.disabledMinutes(selectedHour);
      }

      if (this.startDisabledMinutes) {
        minutesArr = this.startDisabledMinutes(selectedHour);
      }

      if (this.hasLimit && this.startValue) {
        var _this$getStartValueSp2 = this.getStartValueSplit(),
            startHours = _this$getStartValueSp2.startHours,
            startMinutes = _this$getStartValueSp2.startMinutes;

        var limitMinutesArr = (0, _lodash.cloneDeep)(this.minutes);

        if (selectedHour <= startHours) {
          for (var i = 0; i < limitMinutesArr.length; i++) {
            var item = limitMinutesArr[i];
            if (minutesArr.includes(item)) continue;
            if (item > startMinutes) continue;
            if (item == startMinutes) continue;
            minutesArr.push(item);
          }
        }
      }

      return minutesArr;
    },
    endDisabledSecondsFuc: function endDisabledSecondsFuc(selectedHour, selectedMinute) {
      var secondsArr = [];

      if (this.disabledSeconds) {
        secondsArr = this.disabledSeconds(selectedHour, selectedMinute);
      }

      if (this.startDisabledSeconds) {
        secondsArr = this.startDisabledSeconds(selectedHour, selectedMinute);
      }

      if (this.hasLimit && this.startValue) {
        var _this$getStartValueSp3 = this.getStartValueSplit(),
            startHours = _this$getStartValueSp3.startHours,
            startMinutes = _this$getStartValueSp3.startMinutes,
            startSeconds = _this$getStartValueSp3.startSeconds;

        var limitSecondsArr = (0, _lodash.cloneDeep)(this.seconds);

        if (selectedHour < startHours || selectedHour == startHours && selectedMinute == startMinutes) {
          for (var i = 0; i < limitSecondsArr.length; i++) {
            var item = limitSecondsArr[i];
            if (secondsArr.includes(item)) continue;

            if (selectedHour == startHours && selectedMinute == startMinutes) {
              if (item > startSeconds) continue;
              if (item == startSeconds) continue;
            }

            secondsArr.push(item);
          }
        }
      }

      return secondsArr;
    },
    getStartValueSplit: function getStartValueSplit() {
      var startHours = Number(this.startValue.format("HH"));
      var startMinutes = Number(this.startValue.format("mm"));
      var startSeconds = Number(this.startValue.format("ss"));
      return {
        startHours: startHours,
        startMinutes: startMinutes,
        startSeconds: startSeconds
      };
    },
    getEndValueSplit: function getEndValueSplit() {
      var endHours = Number(this.endValue.format("HH"));
      var endMinutes = Number(this.endValue.format("mm"));
      var endSeconds = Number(this.endValue.format("ss"));
      return {
        endHours: endHours,
        endMinutes: endMinutes,
        endSeconds: endSeconds
      };
    },
    endItemKeyup: function endItemKeyup(e) {
      this.$emit("inputPressEnter", e);
    },
    focus: function focus() {
      this.$refs.startTimePicker.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: "startTimePicker",
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, propsData), {}, {
        disabledHours: this.startDisabledHoursFuc,
        disabledMinutes: this.startDisabledMinutesFuc,
        disabledSeconds: this.startDisabledSecondsFuc // allowClear: false

      }),
      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.endTimePicker.focus(); // this.$refs.endTimePicker.onFocus();
            // this.$refs.endItem.click();

          }, 100);
        }
      })
    };

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

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

    var end = {
      ref: "endTimePicker",
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, propsData), {}, {
        disabledHours: this.endDisabledHoursFuc,
        disabledMinutes: this.endDisabledMinutesFuc,
        disabledSeconds: this.endDisabledSecondsFuc // allowClear: false

      }),
      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.maxTime && current) {
            outside = current > _this.maxTime || _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.minTime && current) {
            outside = current < _this.minTime || _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-time-picker", (0, _babelHelperVueJsxMergeProps.default)([{}, start]))]), h("div", {
      "class": "fh"
    }, [" ", separatorText, " "]), h("div", {
      "class": "item"
    }, [h("opu-time-picker", (0, _babelHelperVueJsxMergeProps.default)([{}, end]))])]);
  }
};
exports.default = _default;