<template>
  <view v-if="isFromReadonly" class="ml-date-picker is-readonly">
    <view class="ml-date-picker-readonly-content">{{ value }}</view>
  </view>
  <FixUniDatetimePicker v-else :class="className" v-model="selectVal" type="date" :start="minDate" :end="maxDate" :placeholder="placeholder" @change="changeEvent" :clearIcon="clearable" :border="border"/>
</template>

<script>
import FixUniDatetimePicker from '../fix-uni-datetime-picker/fix-uni-datetime-picker.vue'

export default {
  name: 'MlDatePicker',
  options: {
    virtualHost: true
  },
  components: {
    FixUniDatetimePicker
  },
  props: {
    value: String,
    border: {
      type: Boolean,
      default: false
    },
    clearable: Boolean,
    minDate: [String, Number],
    maxDate: [String, Number],
    placeholder: {
      type: String,
      default: '请选择日期'
    },
    readonly: Boolean,
    disabled: Boolean,
    className: String
  },
  inject: {
    form: {
      from: 'mlForm',
      default: null
    }
  },
  data () {
    return {
      selectVal: ''
    }
  },
  computed: {
    isFromReadonly () {
      return this.form ? this.form.readonly : false
    }
  },
  watch: {
    value (value) {
      this.selectVal = value
    }
  },
  created () {
    if (this.value) {
      this.selectVal = this.value
    }
  },
  methods: {
    changeEvent (value) {
      this.$emit('input', value)
      this.$emit('change', { value })
    }
  }
}
</script>

<style lang="scss">
.ml-date-picker-readonly-content {
  padding: 20rpx 4rpx;
}
</style>
