export default {
  methods: {
    getDateLimitOptions() {
      return this.field.options || {};
    },
    initDateLimitPickerOptions() {
      const pickerOptions = { ...(this.pickerOptions || {}) };
      if (["year", "month", "date", "datetime"].includes(this.field.options.type)) {
        if (!pickerOptions.shortcuts) {
          pickerOptions.shortcuts = [
            {
              text: this.$t1("此刻"),
              onClick(picker) {
                picker.$emit("pick", new Date());
              },
            },
          ];
        }
      }
      pickerOptions.disabledDate = (time) => this.isDateLimitDisabled(time);
      this.pickerOptions = pickerOptions;
    },
    initDateLimitWatch() {
      if (this.designState) {
        return;
      }
      const { limitStartField, limitEndField } = this.getDateLimitOptions();
      if (limitStartField) {
        this.$watch(
          () => this.getDateLimitLinkedValue(limitStartField),
          () => {
            this.initDateLimitPickerOptions();
          }
        );
      }
      if (limitEndField) {
        this.$watch(
          () => this.getDateLimitLinkedValue(limitEndField),
          () => {
            this.initDateLimitPickerOptions();
          }
        );
      }
    },
    getDateLimitDataSource() {
      return this.tableParam && this.tableParam.row
        ? this.tableParam.row
        : this.formModel;
    },
    getDateLimitLinkedValue(fieldName) {
      if (!fieldName) {
        return null;
      }
      const dataSource = this.getDateLimitDataSource();
      return dataSource ? dataSource[fieldName] : null;
    },
    parseDateLimitValue(val) {
      if (val === null || val === undefined || val === "") {
        return null;
      }
      const date = new Date(String(val).replace(/-/g, "/"));
      return isNaN(date.getTime()) ? null : date.getTime();
    },
    normalizeDateLimitDayTime(time, isEnd) {
      const date = new Date(time);
      if (this.field.options.type === "datetime") {
        return date.getTime();
      }
      date.setHours(isEnd ? 23 : 0, isEnd ? 59 : 0, isEnd ? 59 : 0, isEnd ? 999 : 0);
      return date.getTime();
    },
    isDateLimitDisabled(time) {
      const options = this.getDateLimitOptions();
      const currentTime = time.getTime();

      const minDate = this.parseDateLimitValue(options.minDate);
      if (minDate !== null && currentTime < this.normalizeDateLimitDayTime(minDate, false)) {
        return true;
      }

      const maxDate = this.parseDateLimitValue(options.maxDate);
      if (maxDate !== null && currentTime > this.normalizeDateLimitDayTime(maxDate, true)) {
        return true;
      }

      if (options.limitStartField) {
        const startTime = this.parseDateLimitValue(
          this.getDateLimitLinkedValue(options.limitStartField)
        );
        if (startTime !== null && currentTime < this.normalizeDateLimitDayTime(startTime, false)) {
          return true;
        }
      }

      if (options.limitEndField) {
        const endTime = this.parseDateLimitValue(
          this.getDateLimitLinkedValue(options.limitEndField)
        );
        if (endTime !== null && currentTime > this.normalizeDateLimitDayTime(endTime, true)) {
          return true;
        }
      }

      return false;
    },
    isDateLimitViolated(value) {
      if (value === null || value === undefined || value === "") {
        return false;
      }
      const currentTime = this.parseDateLimitValue(value);
      if (currentTime === null) {
        return false;
      }

      const options = this.getDateLimitOptions();
      const minDate = this.parseDateLimitValue(options.minDate);
      if (minDate !== null && currentTime < this.normalizeDateLimitDayTime(minDate, false)) {
        return true;
      }

      const maxDate = this.parseDateLimitValue(options.maxDate);
      if (maxDate !== null && currentTime > this.normalizeDateLimitDayTime(maxDate, true)) {
        return true;
      }

      if (options.limitStartField) {
        const startTime = this.parseDateLimitValue(
          this.getDateLimitLinkedValue(options.limitStartField)
        );
        if (startTime !== null && currentTime < this.normalizeDateLimitDayTime(startTime, false)) {
          return true;
        }
      }

      if (options.limitEndField) {
        const endTime = this.parseDateLimitValue(
          this.getDateLimitLinkedValue(options.limitEndField)
        );
        if (endTime !== null && currentTime > this.normalizeDateLimitDayTime(endTime, true)) {
          return true;
        }
      }

      return false;
    },
    getDateLimitViolationMessage() {
      const options = this.getDateLimitOptions();
      if (options.limitEndField && !options.limitStartField) {
        return "开始时间不能大于结束时间";
      }
      if (options.limitStartField && !options.limitEndField) {
        return "结束时间不能小于开始时间";
      }
      return "日期超出允许范围";
    },
  },
};
