餐饮油烟智能监测与监管一体化平台
feiyu02
6 天以前 ccc970e575ef3f3e5c67af8da210263f4ac549f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<template>
  <el-form-item :label="label" :prop="prop">
    <el-date-picker
      v-model="date"
      @change="handleChange"
      :type="type"
      placeholder="选择时间"
      start-placeholder="选择开始时间"
      end-placeholder="选择结束时间"
      style="width: 150px"
      v-bind="$attrs"
    />
  </el-form-item>
</template>
 
<script>
import dayjs from 'dayjs'
 
const MONTH = 'month'
const DATE = 'date'
const RANGE = 'datetimerange'
const RANGE2 = 'daterange'
 
export default {
  props: {
    type: {
      type: String,
      default: MONTH,
    },
    // 返回结果
    value: Date || Array,
    // 是否默认返回初始选项
    initValue: {
      type: Boolean,
      default: true,
    },
    label: {
      type: String,
      default: '时间',
    },
    prop: {
      type: String,
      default: 'time',
    },
  },
  emits: ['update:value', 'change'],
  data() {
    return {
      date: this.value,
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.date = newVal
        }
      },
      immediate: true,
    },
  },
  computed: {},
  methods: {
    handleChange(value) {
      this.$emit('update:value', value)
      this.$emit('change', value)
    },
  },
  mounted() {
    if (this.initValue) {
      switch (this.type) {
        case RANGE:
        case RANGE2:
          this.date = [dayjs().startOf('month').toDate(), dayjs().toDate()]
          break
        case MONTH:
          this.date = dayjs().startOf('month').toDate()
          break
        case DATE:
          this.date = dayjs().toDate()
          break
        default:
          break
      }
      this.handleChange(this.date)
    }
  },
}
</script>