<!-- 时间选择框 -->
<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"
    :date-type="'time'"
    :date-title="dateTitle"
    :date-confirm-button-text="dateConfirmButtonText"
    :date-cancel-button-text="dateCancelButtonText"
    :date-show-toolbar="dateShowToolbar"
    :date-loading="dateLoading"
    :date-readonly="dateReadonly"
    :date-filter="dateFilter"
    :date-formatter="dateFormatter"
    :date-columns-order="dateColumnsOrder"
    :date-item-height="dateItemHeight"
    :date-visible-item-count="dateVisibleItemCount"
    :date-swipe-duration="dataSwipeDuration"
    :min-hour="minHour"
    :max-hour="maxHour"
    :min-minute="minMinute"
    :max-minute="maxMinute"
    right-icon="arrow"
    @click="showDate"
    @confirm="confirm"
    @clear="clear"
    @change="dateChange"
  >
    <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 dateMixin from '../dateMixin'
import { i18n } from '../../../..//lib/locale/lang/index'

export default {
  name: 'CsFieldTime',
  components: { CsField },
  mixins: [mixin, dateMixin],
  props: {},
  data() {
    return {
      i18nLang: new Vue({ i18n }),
      currentValue: ''
    }
  },
  //监听属性 类似于data概念
  computed: {
    currentPlaceholder() {
      if (this.placeholder) {
        return this.placeholder
      } else {
        return this.i18nLang.$t('common.placeholderDateTime')
      }
    }
  },
  //监控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: {
    /** 弹出时间选择框 */
    showDate() {
      this.$refs.csField.showPopup = true
      this.$refs.csField.showDate = true
      if (this.currentValue) {
        this.$refs.csField.currentDate = this.currentValue
      } else {
        this.$refs.csField.currentDate = moment().format('HH:mm')
      }
    },

    /** 保存时间 */
    confirm(date) {
      this.currentValue = date
      this.$emit('input', this.currentValue)
      this.$refs.csField.showPopup = false
      this.$forceUpdate()
    },

    clear() {
      this.currentValue = ''
      this.$emit('clear')
      this.$refs.csField.showPopup = false
      this.$refs.csField.showDate = false
    },

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