<!-- 日历选择框 -->
<template>
  <cs-field
    ref="csField"
    v-model="currentValue"
    :label="label"
    :rules="rules"
    :rules-key="rulesKey"
    :disabled="disabled"
    :readonly="true"
    :colon="colon"
    :required="required"
    :center="center"
    :error-message-align="errorMessageAlign"
    :label-class="labelClass"
    :label-width="labelWidth"
    :label-align="labelAlign"
    :input-align="inputAlign"
    :calendar-type="calendarType"
    :calendar-title="calendarTitle"
    :calendar-color="calendarColor"
    :calendar-min-date="minDate"
    :calendar-max-date="maxDate"
    :calendar-default-date="calendarDefaultDate"
    :calendar-row-height="calendarRowHeight"
    :calendar-formatter="calendarFormatter"
    :calendar-show-title="calendarShowTitle"
    :calendar-show-subtitle="calendarShowSubtitle"
    :calendar-show-confirm="calendarShowConfirm"
    :calendar-readonly="calendarReadonly"
    :calendar-confirm-text="calendarConfirmText"
    :calendar-confirm-disabled-text="calendarConfirmDisabledText"
    :calendar-first-day-of-week="calendarFirstDayOfWeek"
    :calendar-position="calendarPosition"
    :calendar-round="calendarRound"
    :calendar-close-on-popstate="calendarCloseOnPopstate"
    :calendar-close-on-click-overlay="calendarCloseOnClickOverlay"
    :calendar-safe-area-inset-top="calendarSafeAreaInsetTop"
    :calendar-safe-area-inset-bottom="calendarSafeAreaInsetBottom"
    :calendar-teleport="calendarTeleport"
    :calendar-max-range="calendarMaxRange"
    :calendar-range-prompt="calendarRangePrompt"
    :calendar-show-range-prompt="calendarShowRangePrompt"
    :calendar-allow-same-day="calendarAllowSameDay"
    right-icon="arrow"
    @click="showCalendar"
    @confirm="confirm"
    @select="calendarSelect"
    @open="calendarOpen"
    @close="calendarClose"
    @opened="calendarOpened"
    @closed="calendarClosed"
    @unselect="calendarUnselect"
    @month-show="calendarMonthShow"
    @over-range="calendarOverRange"
  >
    <div :class="disabled ? 'disabled' : ''">
      <span v-if="currentValue">{{ currentValue }}</span>
      <span class="placeholder" v-else>{{ currentPlaceholder }}</span>
    </div>
  </cs-field>
</template>
<script>
import CsField from '../Base'
import mixin from '../mixin'
import calendarMixin from '../calendarMixin'
import { i18n } from '../../../..//lib/locale/lang/index'

export default {
  name: 'CsFieldCalendar',
  components: { CsField },
  mixins: [mixin, calendarMixin],
  props: {},
  data() {
    return {
      i18nLang: new Vue({ i18n }),
      currentValue: ''
    }
  },
  //监听属性 类似于data概念
  computed: {
    currentPlaceholder() {
      if (this.placeholder) {
        return this.placeholder
      } else {
        return this.i18nLang.$t('common.placeholderDate')
      }
    }
  },
  //监控data中的数据变化
  watch: {
    value: {
      handler(val, oldVal) {
        if (this.currentValue != val && val !== oldVal) {
          this.setData()
        }
      },
      immediate: true, // 首次绑定是否执行
      deep: true // 对象内部的属性监听，也叫深度监听
    },
    currentValue: {
      handler(val, oldVal) {
        if (this.value !== val && val !== oldVal) {
          this.$emit('input', val)
        }
      },
      immediate: true, // 首次绑定是否执行
      deep: true // 对象内部的属性监听，也叫深度监听
    }
  },
  //方法集合
  methods: {
    showCalendar() {
      this.$refs.csField.showCalendar = true
    },

    /** 保存时间 */
    confirm(date) {
      if (date) {
        this.currentValue = moment(date).format('YYYY-MM-DD')
      } else {
        this.currentValue = ''
      }
      this.$emit('input', this.currentValue)
      this.$refs.csField.showCalendar = false
      this.$forceUpdate()
    },

    setData() {
      if (this.value) {
        this.currentValue = moment(this.value).format('YYYY-MM-DD')
      } else {
        this.currentValue = ''
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.setData()
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang="scss" scoped></style>
