<template>
  <view class="cv-date-base">
    <picker
      class="cv-date-base-item"
      mode="date"
      :value="localVal"
      :start="startDate"
      :end="endDate"
      @change="bindDateChange"
      @cancel="bindPickerCancel"
      :fields="fields"
      @click="changeIconTop"
    >
      <view class="cv-date-base-text" v-if="localVal != ''">{{ localVal }}</view>
      <view class="cv-date-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: 'cvDateBase',
  emits: ['input', 'update:modelValue'],
  props: {
    //默认输入框
    value: {
      type: String,
      default: ''
    },
    modelValue: {
      type: String,
      default: ''
    },
    startDate: {
      type: String,
      default: ''
    },
    endDate: {
      type: String,
      default: ''
    },
    fields: {
      type: String,
      default: 'day'
    },
    // 占位符
    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;
    },
    bindDateChange: function (e) {
      this.iconTop = false;
      this.localVal = e.detail.value;
      this.$emit('input', this.localVal);
      this.$emit('update:modelValue', this.localVal);
    },
    bindPickerCancel() {
      this.iconTop = false;
    },
    changeIconTop() {
      this.iconTop = true;
    }
  }
};
</script>

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