<template>
  <view class="cv-time-base">
    <picker
      class="cv-time-base-item"
      mode="time"
      :value="localVal"
      :start="startTime"
      :end="endTime"
      @change="bindTimeChange"
      @cancel="bindPickerCancel"
      @click="changeIconTop"
    >
      <view class="cv-time-base-text" v-if="localVal != ''">{{ localVal }}</view>
      <view class="cv-time-base-placeholder" v-else>{{ placeholder }}</view>
    </picker>
  </view>
</template>
<script>
import mpMixin from '../../libs/mixin/mpMixin.js';
import mixin from '../../libs/mixin/mixin.js';
export default {
  mixins: [mpMixin, mixin],
  name: 'cvTimeBase',
  emits: ['input', 'update:modelValue'],
  props: {
    //默认输入框
    value: {
      type: String,
      default: ''
    },
    modelValue: {
      type: String,
      default: ''
    },
    startTime: {
      type: String,
      default: ''
    },
    endTime: {
      type: String,
      default: ''
    },
    // 占位符
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  watch: {
    value(newVal) {
      this.localVal = newVal;
    },
    modelValue(newVal) {
      this.localVal = newVal;
    }
  },
  data() {
    return {
      localVal: '',
      iconTop: false
    };
  },
  created() {
    this.localVal = this.doValueGet();
  },
  methods: {
    doValueGet() {
      if (this.value === '') return this.modelValue;
      if (this.modelValue === '') return this.value;
      return this.value;
    },
    bindTimeChange: function (e) {
      this.localVal = e.detail.value;
      this.iconTop = false;
      this.$emit('input', this.localVal);
      this.$emit('update:modelValue', this.localVal);
    },
    bindPickerCancel() {
      this.iconTop = false;
    },
    changeIconTop() {
      this.iconTop = true;
    }
  }
};
</script>

<style>
.cv-time-base {
  min-height: 35px;
  display: flex;
  flex: 1;
}
.cv-time-base-item {
  height: 35px;
  line-height: 35px;
  width: 100%;
  margin-left: 20px;
}
.cv-time-base-placeholder {
  color: grey;
}
</style>
